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:
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:
[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.
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.
/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.