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
#!/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
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?
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
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.
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.
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.
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.
> 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)