So I have a Raspberry Pi 3B+ running Raspberry OS on Xfce GUI.
I'm trying to run a script on shutdown using the advice given on this thread.
[Unit]
Description=Screensaver
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/image.sh
[Install]
WantedBy=multi-user.target
The script is placed at /etc/systemd/system/
It works on reboot but it doesn't on shutdown. I can't understand why.
Running systemctl cat img.service
gives me this output:
[Unit]
Description=Screensaver
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/image.sh
[Install]
WantedBy=multi-user.target
Basically the same thing. I'm a new Linux user so I'm pretty much lost.
I've also tried placing the script on the system-shutdown folder as suggested on this thread.
I don't know where to start to solve this, but any guidance would be of great help. I've tried researching on this forum but I don't see any other possible answers to this issue. Hope someone can help.
EDIT: I've tried changing the .service file to this:
[Unit]
Description=Screensaver
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/image.sh
[Install]
WantedBy=halt.target
WantedBy=reboot.target
Following the provided answer didn't work. In fact, my e-ink screen stopped working (the script I'm using basically sends a screensaver image to a e-ink Waveshare screen) until I disabled the systemctl. n I ran systemctl status and this is the output I got. Which means it worked but not in time before shutdown, I guess? Maybe it just keeps running continuously and that's why any interfering/parallel commands don't work anymore?
● img.service - Screensaver
Loaded: loaded (/etc/systemd/system/img.service; enabled; vendor preset: enabled)
Active: activating (start) since Thu 2024-01-25 21:42:11 CET; 4min 56s ago
Main PID: 989 (image.sh)
Tasks: 3 (limit: 1595)
CPU: 6.686s
CGroup: /system.slice/img.service
├─ 989 /bin/bash /usr/local/bin/image.sh
├─ 998 sudo papertty_venv/bin/papertty --driver IT8951 image --rotate 0 --stretch --image ./Pictures/sleep.bmp
└─1025 /home/javierdepascual/papertty_venv/bin/python3 papertty_venv/bin/papertty --driver IT8951 image --rotate 0 --stretch --image ./Pictures/sleep.bmp
Jan 25 21:42:11 raspberrypi systemd[1]: Starting Screensaver...
Jan 25 21:42:11 raspberrypi sudo[998]: root : PWD=/home/javierdepascual ; USER=root ; COMMAND=papertty_venv/bin/papertty --driver IT8951 image --rotate 0 --stretch --image ./Pictures/sleep.bmp
Jan 25 21:42:11 raspberrypi sudo[998]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=0)