Score:1

Systemd ExecStart parameter is not accessible inside the script

in flag

I have a very strange situation.

I have a systemd unit

[Unit]
Description=Nightly snapshot backup job for [%i] volume

[Service]
Type=simple
KillMode=process
EnvironmentFile=/etc/systemd/schedule-backup_%i.conf
ExecStart=/usr/local/bin/backup.sh -s '$SOURCE' -b '$BACKUP' -t '$TITLE' -l 'backup_%i.log'

When I execute that with the service instance: sudo systemctl start [email protected]

the backup.sh script does not receive the last parameter at all. It should get "backup_projects.log"

I am reading the parameters with

while getopts s:b:t:l: flag
do
    case "${flag}" in
        s) source_path_root="${OPTARG}";;
        b) backup_path_root="${OPTARG}";;
        t) email_title="${OPTARG}";;
        l) log_name="${OPTARG}";;
    esac
done

when I check the service logs, it seems the script is called with the appropriate parameters:

systemctl -l status [email protected] -n50

○ [email protected] - Nightly snapshot backup job for [projects] volume
     Loaded: loaded (/etc/systemd/system/[email protected]; static)
     Active: inactive (dead) since Tue 2021-12-14 12:47:01 EET; 16min ago
TriggeredBy: ● schedule-backup_projects.timer
    Process: 8161 ExecStart=/usr/local/bin/backup.sh -s $SOURCE -b $BACKUP -t $TITLE -l backup_projects.log (code=exited, status=0/SUCCESS)
   Main PID: 8161 (code=exited, status=0/SUCCESS)
        CPU: 3.122s

Dec 14 12:46:56 fallen-robot systemd[1]: Started Nightly snapshot backup job for [projects] volume.
Dec 14 12:46:56 fallen-robot backup.sh[8161]: /usr/local/bin/backup.sh: line 37: /var/log/: Is a directory
Dec 14 12:46:56 fallen-robot backup.sh[8161]: Log file set to /var/log/

When I execute the same script from the console the parameter is accepted fine. What am I doing wrong?

EDIT: Seems like it worked once I put the -l parameter in the beginning instead of at the end. Could be some problem with getopts. I will accept an answer from whoever can explain this

Score:1
in flag

I found the issue, systemd is having its own rules for variables from EnvironmentFiles

https://fedoraproject.org/wiki/Packaging:Systemd#EnvironmentFiles_and_support_for_.2Fetc.2Fsysconfig_files

It says

You may then refer to variables set in the /etc/sysconfig/httpd file with ${FOOBAR} and $FOOBAR, in the ExecStart= lines (and related lines). (${FOOBAR} expands the variable into one word, $FOOBAR splits up the variable value at whitespace into multiple words)

After I changed my line to

ExecStart=/usr/local/bin/backup.sh -s ${SOURCE} -b ${BACKUP} -t ${TITLE} -l backup_%i.log

My problems went away, including one with TITLE that contained white spaces. Without the {} TITLE was expanded to 3 words and since the second and third one appear without '-' to getopts, it will stop further parameter processing and thus ignore the -l backup_%i.log part

getopts implements the standard option processing, which means that it stops looking for options when it either sees an argument that's not an option ...

courtesy of https://unix.stackexchange.com/a/666748/499351

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.