Results 1 to 7 of 7

Thread: systemd - run a script during boot with network connection and before login screen appears

  1. #1

    Default systemd - run a script during boot with network connection and before login screen appears

    I want to have a script run during boot. The script needs a working network connection and the login screen must not display until this script is finished. The script could start the network itself if necessary and determine when it is up. The network is controlled by 'traditional with ifup'. This was easy to do with sysv init but is starting to seem impossible with systemd. Getting a script to run during boot is easy but getting it to do what I want is another matter entirely. The closest I've got so far is this for my service file /etc/systemd/system/mine.service
    Code:
    [Unit]
    Description=mine
    DefaultDependencies=no
    After=sysinit.target local-fs.target
    Before=basic.target
    
    [Service]
    Type=oneshot
    ExecStart=/opt/mine/blahblahblah
    
    [Install]
    WantedBy=basic.target
    I got this from http://lists.freedesktop.org/archive...er/000225.html

    /opt/mine/blahblahblah looks like
    Code:
    #!/bin/bash
    out="/dev/shm/$(basename $0)_out";
    > "${out}";
    date >> "${out}";
    #systemctl start network 
    for i in {1..30};do
    sleep 1;
    ifconfig -a >> "${out}";
    done
    date >> "${out}";
    This works in as much that the script runs during boot and holds up the boot process for 30 seconds whilst it runs. But I can't get the network up. Nothing I add to the Unit section results in the network coming up. You can see in the script I've commented out a line 'systemctl start network'. If that's uncommented the boot process never finishes, it gets stuck trying to run my script seemingly for ever. The system is unusable until I boot from alternative media, mount the relevant partition and edit the script.

    I also want to have a script run during shutdown with a working network connection, but I figure if I can get the boot script working as desired getting the shutdown script working should be easy enough to figure out.

    For a while I've had in my mind that if I found I was unable to make systemd do what I want my fall back option was to use /etc/init.d/boot.local, but as someone has recently pointed out in another thread, systemd makes a lie of the comments in /etc/init.d/boot.local. boot.local gets called by /usr/lib/systemd/system/rc-local.service and that contains
    Code:
    After=network.target
    And that it seems could mean pretty much anything and also you're wrong if you want to do things after you know the network is up:
    http://www.freedesktop.org/wiki/Soft...NetworkTarget/

  2. #2
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    23,698
    Blog Entries
    1

    Default Re: systemd - run a script during boot with network connection and before login screen appears

    I don't pretend to understand your exact requirements, but you mention that the script needs to run after the network is up. Wouldn't something like the following be okay?

    Code:
      [Unit]
      Description=mine...
      After=network.target
    
      [Service]
      ExecStart=/opt/mine/blahblahblah
    
      [Install]
      WantedBy=multi-user.target
    but I'm confused by this

    This works in as much that the script runs during boot and holds up the boot process for 30 seconds whilst it runs. But I can't get the network up. Nothing I add to the Unit section results in the network coming up. You can see in the script I've commented out a line 'systemctl start network'. If that's uncommented the boot process never finishes, it gets stuck trying to run my script seemingly for ever. The system is unusable until I boot from alternative media, mount the relevant partition and edit the script.
    Well, I don't think you should be trying to issue systemd commands while systemd is trying to initialise - I can see potential for failure right there.

    For a while I've had in my mind that if I found I was unable to make systemd do what I want my fall back option was to use /etc/init.d/boot.local, but as someone has recently pointed out in another thread, systemd makes a lie of the comments in /etc/init.d/boot.local. boot.local gets called by /usr/lib/systemd/system/rc-local.service and that contains
    Code:
    After=network.target
    And that it seems could mean pretty much anything and also you're wrong if you want to do things after you know the network is up:
    http://www.freedesktop.org/wiki/Soft...NetworkTarget/
    ??? I don't quite get your drift here....
    Last edited by deano_ferrari; 17-Mar-2014 at 17:58.

  3. #3
    Join Date
    Jun 2008
    Location
    Auckland, NZ
    Posts
    23,698
    Blog Entries
    1

    Default Re: systemd - run a script during boot with network connection and before login screen appears

    BTW, regarding the use of 'Type=oneshot' it is mentioned

    Behavior of oneshot is similar to simple; however, it is expected that the process has to exit before systemd starts follow-up units. RemainAfterExit= is particularly useful for this type of service.
    http://www.freedesktop.org/software/...d.service.html

  4. #4
    Join Date
    Sep 2012
    Posts
    7,093

    Default Re: systemd - run a script during boot with network connection and before login screen appears

    Quote Originally Posted by arizonagroovejet View Post
    The script needs a working network connection and the login screen must not display until this script is finished.
    Code:
    [Unit]
    After=network.target
    Before=systemd-user-sessions.service
    
    [Service]
    Type=oneshot
    But the obvious question in such cases is - what should happen if network connection is not available. System should not boot?

  5. #5

    Default Re: systemd - run a script during boot with network connection and before login screen appears

    Quote Originally Posted by deano_ferrari View Post
    I don't pretend to understand your exact requirements, but you mention that the script needs to run after the network is up. Wouldn't something like the following be okay?

    Code:
      [Unit]
      Description=mine...
      After=network.target
    
      [Service]
      ExecStart=/opt/mine/blahblahblah
    
      [Install]
      WantedBy=multi-user.target
    No, that doesn't work. I'm assuming for reasons described at
    http://www.freedesktop.org/wiki/Soft...NetworkTarget/
    The script runs, but the login screen comes up regardless of whether the script is finished running.

    Quote Originally Posted by deano_ferrari View Post
    Well, I don't think you should be trying to issue systemd commands while systemd is trying to initialise - I can see potential for failure right there.
    Interesting. Can you expand on why?

    Quote Originally Posted by deano_ferrari View Post
    ??? I don't quite get your drift here....
    The comments in boot.local say that things in there "...should happen directly after booting before we're going to the first run level". The comments were true for sysv init but they're not true since the switch to systemd. I should probably raise that as a bug. I can't just see an open one that covers it.

  6. #6

    Default Re: systemd - run a script during boot with network connection and before login screen appears

    Quote Originally Posted by arvidjaar View Post
    Code:
    [Unit]
    After=network.target
    Before=systemd-user-sessions.service
    
    [Service]
    Type=oneshot
    This does what I want! Thank you! I wasn't sure what to put in the [Install] section to I put WantedBy=multi-user.target.


    Quote Originally Posted by arvidjaar View Post
    But the obvious question in such cases is - what should happen if network connection is not available. System should not boot?
    I neglected to mention that the script I want to run does give up after 30 seconds if it thinks there's no network connection. It's a script I've been using for years, but with systemd.

  7. #7
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: systemd - run a script during boot with network connection andbefore login screen appears

    On 2014-03-18 21:46, arizonagroovejet wrote:

    > deano_ferrari;2631091 Wrote:
    >>
    >> Well, I don't think you should be trying to issue systemd commands while
    >> systemd is trying to initialise - I can see potential for failure right
    >> there.
    >>

    > Interesting. Can you expand on why?


    I also see it as a big problem, but I can not explain it. Well, it is a
    kind of recursion.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 13.1 x86_64 "Bottle" at Telcontar)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •