1

Been pulling my hair out over this one for the last two weeks. I can't for the life of me figure out how to get this working:

The situation

I'm running a Debian 9 server with a couple of VirtualBox virtual machines which I want to 'save state' when the server going down for reboot of poweroff. After the server is restarted I want those vm's to start again.

The script to save and then start the vm's is working like a charm. No problem there. The part where they are started after boot is also fine. The problem lies in the shutting down part. So I have simplified things a bit.

The remaining issue

I have a script: /home/vorkbaard/goingdown.sh. It sends out an e-mail to myself stating the server is going down. The script works fine. On poweroff however it starts to execute but is cut short by the network going down before it is finished.

What I want is to have a script executed after the poweroff or reboot command is given but before anything else is set in motion, like network or filesystems going down. The process needs to wait until the script is successfully finished.

I tried using Systemd and SysV but while I am quite familiar with SysV it does not let me pick my own execution order anymore, I guess because Systemd is replacing is, and anyway I would like to switch to Systemd completely because SysV is being phased out in Debian. I would like to stay with Debian proper, not switch to Devuan or RHEL.

I know creating unit files and moving them to the proper location is the way to go but as I said - I can't for the life of me figure out the proper way to do this. There are countless examples but none seem specific to my situation.

The actual questions

Which unit files should I create, where do I put them and how do I tell my server to run them before anything else once a reboot or poweroff is issued?


Also tried to adapt Execute simple script before shutdown and reboot:

In /etc/systemd/system/test.service:

[Unit]
Description=Send an email on shutdown and reboot

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash (also tried with /bin/true)
ExecStop=/root/rebootscript (also tried ExecStop=sh /root/rebootscript. Then the script does nothing on reboot.)
[Install]
WantedBy=multi-user.target

Then rebooted, did systemctl status test.service and it said active (exited). However on reboot a message appears saying A stop job is running for Send an e...utdown and reb oot (34s / 1min 30s)_ and after 1.5 minutes it just times out, no mail is sent and the server reboots.

/var/log/syslog sais:

Stopping Send an email on shutdown and reboot...
Creating SSL connection to host
SSL connection using RSA_AES_128_CBC_SHA1
Sent mail for [my email address here] (221. 2.0.0 closing connection q45sm79107etc.53 - gsmtp) uid=0 username=root outbytes=509
Stopped Send an email on shutdown and reboot

But the mail is not sent and no mail is in the mailqueue. Also no errors appear in the account I'm sending the mail from. Again, the script works fine when run manually.

In /root/rebootscript is:

/bin/bash
NOW=$(/bin/date +"%H:%M")
/bin/echo "Rebooting $NOW" | /usr/bin/mailx -s "Rebooting $NOW" [my email address here]

Interestingly, if I do systemctl stop test.service I receive a mail saying the server is rebooting, so the service itself seems to work.

  • With SysV init you can simply install your shutdown/reboot init.d script as /etc/rc0.d/S00mygoingdownscript and /etc/rc6.d/S00mygoingdownscript, and that will be executed as the first script (together with any other scripts that start with S00). – wurtel Feb 2 '18 at 11:45
  • Thats the point: they all start with S00. – Forkbeard Feb 2 '18 at 11:52
  • Hmm, actually they should start with K. And I don't see all scripts in rc0.d start with K00, I have 40 with K01, 3 with K02, 1 with K03, etc. You can use a header in the init.d script to determine the order, # Required-Stop: $network means it runs (and completes) before the network is stopped. Check the manpage for insserv(8). – wurtel Feb 2 '18 at 12:02
  • 1
    Yes, they start with K. Still I gather Systemd would be the way to go so I would really like to know how to do that. – Forkbeard Feb 2 '18 at 12:33
  • Copied the test script to /etc/rc6.d/K00shutdownscript and /etc/rc0.d/K00shutdownscript. The script works but not on reboot/poweroff. - same when using S00 instead of K00. – Forkbeard Feb 2 '18 at 13:17
1

Ok, I figured it out. The script in my original posting was ok but it was executed after the network went down which explains why the mail wasn't sent.

Details: https://serverfault.com/questions/785127/how-to-stop-systemd-services-in-specific-order

The script that made it work:

[Unit]
Description=Send an email on shutdown and reboot
Wants=network-online.target   <---- ADDED THIS
After=network-online.target   <----/

[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/bin/bash (also tried with /bin/true)
ExecStop=/root/rebootscript (also tried ExecStop=sh /root/rebootscript. Then the script does nothing on reboot.)
[Install]
WantedBy=multi-user.target

There's probably too much in the above file but this definitely works.

Thank you all :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.