I want to run a script before shutdown and reboot to kill a certain process. My script for testing so far:
#!/bin/bash
echo "working..."
echo "Melody"
for note in C3 F4 A4 F4 C3 F4 A4 F4 C3 F4 F4 F4 E4 D4 C3; do play -q -n synth 0.25 pluck $note; done
for Lauf in {1..3}; do
echo "waiting $Lauf ..."
sleep 5s
done
echo "done"
This is stored in /usr/lib/systemd/system-shutdown/, executable and owned by root:root. But running reboot or shutdown from the menu, I don’t see / hear anything. How to get this work?
All apps are killed anyway on shutdown. You may want to reduce the default waiting timer from the default 90 seconds though. And it is another matter if the app managed to save its data. That might be the usage of your script, to save data from a nontesponding app.
Did you test your script from a terminal or tty? Not every computer has a speaker these days, esp. Laptops.
The echo command will not show anything, unless you disabled plymoth on the boot line. Actually i am pretty sure it may still not show anything, because it might output to another tty.
Then once you’ve rebooted, any output of your script should be in the journal for the previous boot. Though whether or not it will be able to play stuff through a speaker is another question.
I’m pretty sure this will work; it’s taken from a service I was using a few years ago, but as it’s been long disabled, I’m not 100% certain.
Just a guess, but as it refers to a “slow backup script”, it could be because it might cause a big delay to a shutdown. I suppose it’s possible that if a script run at this point were to hang for some reason, shutdown would never happen.
OP asked for script executed “before everything else” on shutdown. The system-shutdown scripts are executed after everything else, immediately before killing the kernel.
You’d need to run your script early in the shutdown process, so running it first like with @beermad’s service would generally be the way to go.
I don’t think you need DefaultDependencies=no, so I’d try without it first.
What makes me wonder: Actually my script is only for testing; meanwhile I replaced the echo command by kDialog. I can run it in a konsole and hear the sound, see the kDialogs. I stored it in /usr/lib/systemd/system-shutdown/ and lib/systemd/system-shutdown/ but don’t see / hear anything at shutdown.
I’ve never used this functionality. However according to the information in the link, I would expect the sound system to be un-initialised by the time the script is run (also systemd may be throwing a spanner in the works). The echos are probably going somewhere else, or just getting dropped. kdialog won’t run at that time as the framework it relies upon is already gone.
To clarify, if your script is being run then it’s running after most software has already been shutdown so even if you somehow got your test case working - it’s still a waste of time as your actual case won’t work.
If it was me I’d open a bug report with KDE (I assume you mean akregator from the repo).
If I wanted a workaround then I’d use a service to run the script before shutdown (that approach emulates what you’re doing manually).
Keep in mind that you have the same options available as are used during shutdown (you can’t really click File → Exit), so there’s no guarantee it’ll help. Another option may be to disable/enable the icon using a service to run a script after boot (or disable before and enable after), assuming there are commands for it.
Note that if you run the script from a service, you may still have issues with the sound in your test case, however you should see the echos using systemctl status your.service
I don’t believe systemd system-services will work in this case. They are to late in the process of shutdown or reboot. The software OP wants to stop is running in the usre context and not the system context. At the time a system-services with Before=shutdown.target are started, the user session and slices are already gone. Which might mean OPs software is already kill uncleanly.
I don’t use KDE and SDDM, but GDM has the ability to run scripts at logout. Maybe SDDM can do something similar and run a script at (bevor) logout, which might be better for this problem.
For testing, do not use Audio, maybe wirte a journal entry or create somewhere a file with a timestamp.
I must admit I hadn’t realised they were run so late.
Not sure if you can have a user service run before shutdown or logout, but if so then that should work.
I’d forgotten about that (too used to using services now), yeah sounds like the best option. For KDE, you can add a login or logout script in System Settings → Autostart