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

Andrei Borzenkov arvidjaar at gmail.com
Wed Nov 9 13:43:25 UTC 2016


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?

>
> [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


More information about the systemd-devel mailing list