I'm trying to write a script that makes a) the system update before shutting down (not at reboot) and shows the progress to the user on plymouth.
So far I, well, managed to make it start the script (sometimes?) on shutdown, but it also starts on reboots (to be fixed).
[Unit]
Description=Update on Shutdown
Before=poweroff.target halt.target shutdown.target
After=network-online.target multi-user.target
[Service]
Type=oneshot
ExecStart=/bin/true
RemainAfterExit=true
ExecStop=/usr/share/services/shutdown_update
TimeoutSec=infinity
[Install]
WantedBy=poweroff.target halt.target shutdown.target
The actual script is this one:
#!/bin/sh
fun_update() {
plymouth display-message --text "Aktualisierungen werden installiert..."
plymouth change-mode --updates
pkcon update -p -y > /tmp/update_status.txt
update=0
plymouth change-mode --boot-up
plymouth display-message --text "Aufräumen..."
apt-get autoclean
apt-get autoremove
plymouth change-mode --shutdown
}
fun_plymouth() {
while [ "$update" = 1 ]
do
plymouth system-update --progress $(cat /tmp/update_status.txt | grep -oP "(?<=Percentage: ).*" | tail -1)
plymouth display-message --text "$(cat /tmp/update_status.txt | grep -oP "(?<=Percentage: ).*" | tail -1)%"
sleep 1
done
}
if echo systemctl list-jobs | egrep -q 'reboot.target.*start';
then
exit 0
fi
plymouth change-mode --boot-up
update=0
plymouth display-message --text 'Aktualisierung wird vorbereitet...'
plymouth display-message --text "Es wird nach Aktualisierungen gesucht..."
pkcon refresh
update_list=$(pkcon get-updates)
echo $update_list
if ! echo "$update_list" | grep -q "There are no updates available at this time."; then
update=1
fun_update & fun_plymouth & wait
else
plymouth display-message --text "Keine Aktualisierungen gefunden!"
sleep 3
fi
plymouth display-message --text ""
What these functions do (or what I want them to do) is to a) inform the user that his system is going to be updated, b) start the update using pkcon and c) show the plymouth offline update splash (the splash that is shown when offline updates are being installed) and get the progress of pkcon to show it to the user (that's why the grep and tail -1, to only get the last match).
But for some reason, I manage to start the script, but no plymouth messages are shown and pkcon doesn't start or when it does, it fails.
There are probably a lot of issues in this script, I didn't manage to debug, as plymouth and systemd isn't really helpful.
Every advice is welcome!
Thank you very much!
EDIT: I installed plymouth-x11 to test my script; interestingly, commands sent by the script are ignored here as well when run (even in a root shell), but these exact commands manually entered in the root shell work. I also added this line to check if plymouth is running:
plymouth --ping && echo plymouth is running || echo plymouth NOT running
and it returns it is running.
This does not make sense
EDIT2: With plymouth-x11 I found out that removing (deleting the lines of) the update() and plymouth() function, plymouth shows the message. Re-adding the functions breaks it again.
Also, placing the plymouth command above the function declarations, makes them work again.
EDIT3: I removed the functions and placed the code in an extra file. Now it works as expected (at least in my test environment).
So the question is: Why does my script break when I add the functions?
EDIT4: Don't name your functions like a command you want to call -_-
EDIT5: So, the script should no nothing anymore when I reboot, but still: plymouth doesn't show any messages and the script seems not to be called at shutdown. These are the issues I know of right now, testing a script that's not called is pretty hard. The script works when I start plymouthd manually with plymouth-x11.
I'm running Ubuntu 21.04