Score:0

Systemd service supposed to run on shutdown does not execute bash script

us flag

bash script is in /opt/CleanUp.sh

find /home/*/Downloads/* -type f -exec shred -uz {} \;
rm -r /home/*/Downloads/*
find /home/*/Documents/* -type f -exec shred -uz {} \;
rm -r /home/*/Documents/*
find /home/*/Pictures/* -type f -exec shred -uz {} \;
rm -r /home/*/Pictures/*
find /home/*/Videos/* -type f -exec shred -uz {} \;
rm -r /home/*/Videos/*
find /home/*/Music/* -type f -exec shred -uz {} \;
rm -r /home/*/Music/*
exit 0

.service file in /etc/systemd/system/CleanUp.service

[Unit]
Description=CleanUp Service
DefaultDependencies=no
Before=halt.target shutdown.target reboot.target

[Service]
Type=oneshot
ExecStart= /opt/CleanUp.sh
TimeoutStartSec=0

[Install]
WantedBy=halt.target shutdown.target reboot.target

The script does not execute on shutdown. When I try to start it via

sudo service CleanUp start

I get this error

× CleanUp.service - CleanUp Service
     Loaded: loaded (/etc/systemd/system/CleanUp.service; enabled; preset: disabled)
     Active: failed (Result: exit-code) since *****************; 7s ago
    Process: 21217 ExecStart=/opt/CleanUp.sh (code=exited, status=203/EXEC)
   Main PID: 21217 (code=exited, status=203/EXEC)
        CPU: 1ms

*********** *hostname* systemd[1]: Starting CleanUp.service - CleanUp Service...
*********** *hostname* (eanUp.sh)[21217]: CleanUp.service: Failed to locate executable /opt/CleanUp.sh: Permission denied
*********** *hostname* (eanUp.sh)[21217]: CleanUp.service: Failed at step EXEC spawning /opt/CleanUp.sh: Permission denied
********** *hostname* systemd[1]: CleanUp.service: Main process exited, code=exited, status=203/EXEC
********** *hostname* systemd[1]: CleanUp.service: Failed with result 'exit-code'.
********** *hostname* systemd[1]: Failed to start CleanUp.service - CleanUp Service.

System info: Fedora Linux 37 (Workstation Edition) x86_64 Kernel: 6.2.9-200.fc37.x86_64 DE: GNOME 43.4

jm flag
What are the permissions on the script? And are you sure it is being executed by `bash` since it does not have a shebang? You might also take out the space after the equals sign for ExecStart.
nitro64 avatar
us flag
The shebang is there, just didn't copy it. Removed space- still doesn't work. Permissions: -rw-r--r-- root root for CleanUp.service; -rwxr-xr-x root admin for CleanUp.sh.
jm flag
Does the script possibly have `CRLF` line endings? Does `journalctl -u CleanUp` give any extra info?
nitro64 avatar
us flag
No CRLF line endings. cat -A returns only $ line endings. No extra info in journal. Just the 203/EXEC error.
nitro64 avatar
us flag
Solved the problem. Just created a new .sh file as root and pasted the content. Works like a charm. I think the problem was that I incorrectly set file ownership after moving the script to /opt. Ty for your input.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.