[systemd-devel] how to run a script which takes about 30 seconds before shutdown

Kai Krakow hurikhan77 at gmail.com
Sun Nov 13 07:19:35 UTC 2016


Am Thu, 10 Nov 2016 13:07:52 +0800
schrieb zerons <sironhide0null at gmail.com>:

> On 11/09/2016 09:43 PM, Andrei Borzenkov wrote:
> > On Wed, Nov 9, 2016 at 4:11 PM, zerons <sironhide0null at gmail.com>
> > wrote:  
> >> Hi everyone.
> >>
> >> Everyday, I need to do something like `git pull` after system
> >> bootup and `git push` before shutdown. I am using Ubuntu 16.04.
> >> I have tried to put some script into /etc/rc0.d/, /etc/rc6.d/,
> >> each time the script runs, the network has been stopped, so I
> >> turn to systemd.
> >>
> >>
> >> === Here is a test .service file.
> >> [Unit]
> >> Description=test systemd
> >> Conflicts=reboot.target
> >> After=network-online.target
> >> Wants=network-online.target  
> > 
> > network-online.target itself does not do anything. You need some
> > service that actually does waiting, or at least orders itself
> > correctly on startup and shutdown. If you are using NetworkManager,
> > it is NetworkManager-wait-online.service. Is it enabled?
> >   
> No, that doesn't work. Then I realize the `ping` error message, that
> is not unknown host, so I put `ifconfig wlp9s0` into `test1.sh`, the
> result shows that at that moment, the net interface has already been
> shut off.
> 
> I change the "After=" and "Wants=" to
> "After=NetworkManager-wait-online.service"
> "Wants=NetworkManager-wait-online.service"
> and, yes, it is enabled.

Shouldn't it be "Requires" instead of "Wants" for your case? Your
service cannot run without network being online... "Wants" is just an
optional dependency. If network cannot be started, there's no point in
running your script.

If in consequence your service doesn't run at all then, the transaction
is incomplete.

You could also try switching to systemd-networkd instead. It provides
systemd-networkd-wait-online.service also. I'm not sure if it supports
wifi interfaces, tho.

> >> [Service]
> >> Type=oneshot
> >> RemainAfterExit=yes
> >> ExecStart=-/home/zerons/.bin/test.sh
> >> ExecStop=/home/zerons/.bin/test1.sh
> >>
> >> [Install]
> >> WantedBy=multi-user.target
> >>
> >>
> >> === and test.sh script, please ignore the destination
> >> #!/bin/bash
> >>
> >> echo "bootup" >> /home/zerons/.bin/test
> >> timeout 9 ping -c 1 www.baidu.com >/dev/null 2>&1
> >> while [ $? -ne 0 ]
> >> do
> >>         sleep 1
> >>         timeout 9 ping -c 1 www.baidu.com >/dev/null 2>&1
> >> done
> >>
> >> ping -c 4 www.baidu.com >> /home/zerons/.bin/test 2>&1
> >>
> >>
> >> === test1.sh
> >> #!/bin/bash
> >>
> >> echo "before shutdown"`date +%T` >> /home/zerons/.bin/test
> >> ping -c 8 www.baidu.com >>/home/zerons/.bin/test 2>&1
> >>
> >>
> >>
> >> === the result, on my laptop
> >> before shutdown20:04:35
> >> PING www.a.shifen.com (61.135.169.125) 56(84) bytes of data.
> >> 64 bytes from 61.135.169.125: icmp_seq=1 ttl=56 time=9.57 ms
> >> ping: sendmsg: Network is unreachable
> >> ...
> >> ping: sendmsg: Network is unreachable
> >>
> >> --- www.a.shifen.com ping statistics ---
> >> 8 packets transmitted, 1 received, 87% packet loss, time 7048ms
> >> rtt min/avg/max/mdev = 9.579/9.579/9.579/0.000 ms
> >>
> >>
> >>
> >> === some other infomation
> >> I reboot several times, but the `test1.sh` always got 87% packet
> >> loss,,,
> >>
> >> When I take these steps in a Ubuntu16.04 virtual machine, it works
> >> fine, the `test1.sh` gets 0% packet loss before shutdown. I also
> >> test on a laptop with SSD, `test1.sh` gets 87% packet loss.
> >>
> >> How could I make this work? Is there a way, when `test1.sh` runs,
> >> all the other services could not be stopped until `test1.sh`
> >> returns?
> >>
> >> _______________________________________________
> >> systemd-devel mailing list
> >> systemd-devel at lists.freedesktop.org
> >> https://lists.freedesktop.org/mailman/listinfo/systemd-devel  
> _______________________________________________
> systemd-devel mailing list
> systemd-devel at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/systemd-devel



-- 
Regards,
Kai

Replies to list-only preferred.



More information about the systemd-devel mailing list