You are not logged in.

#1 2021-12-13 13:45:22

cmonty14
Member
Registered: 2019-10-05
Posts: 32

Migration cron-job to systemd-timer failes

Hello,

I have started to migrate several cron-jobs to systemd-timer.

However there's a failure when executing the systemd-timer that is executing a python script.

This is the original cron-job:

30 1 30 3,6,9,12 * root /opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e prd -m "$(date +\%d-\%m-\%Y)"  2>&1

Migration this to a systemd service file with this content

[Unit]
Description=Automatic sync stage development cl-sle12-sp3-sap
After=sync_stage_tst_cl-sle12-sp3-sap.service

[Service]
Type=oneshot
ExecStart=/opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e dev -m "$(date +\%d-\%m-\%Y)"
Environment=HOME=/root

fails when starting the service:

systemctl status sync_stage_dev_cl-sle12-sp3-sap.service
● sync_stage_dev_cl-sle12-sp3-sap.service - Automatic sync stage development cl-sle12-sp3-sap
   Loaded: loaded (/etc/systemd/system/sync_stage_dev_cl-sle12-sp3-sap.service; static; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2021-12-13 12:32:08 CET; 5s ago
  Process: 1109 ExecStart=/opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e dev -m $(date +\%d-\e99ccced335a41ea993a71abe276b70d-\%Y) (code=exited, status=2)
 Main PID: 1109 (code=exited, status=2)

Dec 13 12:32:08 vlcspsumasrv systemd[1]: Starting Automatic sync stage development cl-sle12-sp3-sap...
Dec 13 12:32:08 vlcspsumasrv sync_stage.py[1109]: usage: sync_stage.py [-h] [-c CHANNEL] [-b] [-p PROJECT] [-e ENVIRONMENT]
Dec 13 12:32:08 vlcspsumasrv sync_stage.py[1109]:                      [-m MESSAGE] [--version]
Dec 13 12:32:08 vlcspsumasrv sync_stage.py[1109]: sync_stage.py: error: argument -m/--message: expected one argument
Dec 13 12:32:08 vlcspsumasrv systemd[1]: sync_stage_dev_cl-sle12-sp3-sap.service: Main process exited, code=exited, status=2/INVALIDARGUMENT
Dec 13 12:32:08 vlcspsumasrv systemd[1]: Failed to start Automatic sync stage development cl-sle12-sp3-sap.
Dec 13 12:32:08 vlcspsumasrv systemd[1]: sync_stage_dev_cl-sle12-sp3-sap.service: Unit entered failed state.
Dec 13 12:32:08 vlcspsumasrv systemd[1]: sync_stage_dev_cl-sle12-sp3-sap.service: Failed with result 'exit-code'.

Can you please advise how to fix this issue?

Removing the \ (backslash) in

-m "$(date +%d-%m-%Y)"

is not solving the problem.

THX

Offline

#2 2021-12-13 13:58:54

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,309
Website

Re: Migration cron-job to systemd-timer failes

ExecStart= takes a binary with arguments and does not execute it in a shell, hence "$(date +\%d-\%m-\%Y)"  2>&1 is garbage in this context.
I'd recommend putting the line in a wrapper script in /usr/local/bin.

Last edited by schard (2021-12-13 13:59:38)

Offline

#3 2021-12-13 14:03:50

cmonty14
Member
Registered: 2019-10-05
Posts: 32

Re: Migration cron-job to systemd-timer failes

I tried this

ExecStart=/bin/bash -c '/opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e dev -m "$(date +\%d-\%m-\%Y)" 2>&1'

but this fails to execute the date-function correctly:

CGroup: /system.slice/sync_stage_dev_cl-sle12-sp3-sap.service
           ├─13057 /bin/bash -c /opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e dev -m "$(date +\%d-\e99ccced335a41ea993a71abe276b70d-\%Y)" 2>&1
           └─13060 python3 /opt/susemanager/uyuni-tools/sync_stage.py -p cl-sle12-sp3-sap -e dev -m 13-e99ccced335a41ea993a71abe276b70d-2021

Offline

#4 2021-12-13 14:06:25

schard
Member
From: Hannover
Registered: 2016-05-06
Posts: 1,309
Website

Re: Migration cron-job to systemd-timer failes

You do not correctly escape the % symbols.
https://www.freedesktop.org/software/sy … Specifiers
tl;dr: It's "%%" not "\%".

Offline

#5 2021-12-13 14:41:36

cmonty14
Member
Registered: 2019-10-05
Posts: 32

Re: Migration cron-job to systemd-timer failes

schard wrote:

You do not correctly escape the % symbols.
https://www.freedesktop.org/software/sy … Specifiers
tl;dr: It's "%%" not "\%".

This solution works as expected.

THX

Offline

Board footer

Powered by FluxBB