2

I was hoping to run a command every 85 days using systemd. I have created a timer and a service for the command, but I can't get the timer started. I am hoping to get the command to run every 85, not at boot and to run even if the server is restarted during the 85 day period.

/etc/systemd/system/my.service

[Unit]
description=Do my task

[Service]
Type=oneshot
ExecStart=/usr/local/sbin/myTask

/etc/systemd/system/my.timer

[Unit]
Description=Timer to do my task

[Timer]
OnUnitActiveSec=85d
Unit=my.service
Persistent=true

After attempts to start it using systemctl start my.timer, the error messages I have been getting are:

my.timer: Refusing to start, unit to trigger not loaded.
Failed to start Timer to do my task.

2 Answers 2

0

Change description in your service file to Description. Otherwise, it'll be printing errors in the logs like below:

Unknown lvalue 'description' in section 'Unit'

Also, try using these commands to troubleshoot problems with your timer file:

systemctl status my.timer
journalctl -xe

Update your post if you see any errors in the logs and don't know how to fix them.

0

The issue was the script I was trying to run didn't have a bang at the start.

I diagnosed it by:

  1. looking at the status of the time Andril mentioned above
# systemctl status my.timer
● my.timer - Timer to update stuff
   Loaded: loaded (/etc/systemd/system/my.timer; enabled; vendor preset: enabled)
   Active: active (elapsed) since Tue 2020-01-28 07:14:54 UTC; 2 weeks 6 days ago
  Trigger: n/a

Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
  1. Looking at the list of timers (to get more stats about it)
# systemctl list-timers
NEXT                         LEFT        LAST                         PASSED                UNIT                         ACTIVATES
...
n/a                          n/a         Tue 2018-03-20 14:13:46 UTC  1 years 10 months ago my.timer            my.service
  1. Tried running the service to activate the service (as the timer is based on the last time the service was activated
# systemctl status my.service
● my.service - Renew Lets Encrypt certificates
   Loaded: loaded (/etc/systemd/system/my.service; static; vendor preset: enabled)
   Active: failed (Result: exit-code) since Mon 2020-02-17 15:53:18 UTC; 20s ago
  Process: 19875 ExecStart=/usr/local/sbin/script (code=exited, status=203/EXEC)
 Main PID: 19875 (code=exited, status=203/EXEC)

Feb 17 15:53:18 server systemd[1]: Starting Update stuff...
Feb 17 15:53:18 server systemd[19875]: my.service: Failed to execute command: Exec format error
Feb 17 15:53:18 server systemd[19875]: my.service: Failed at step EXEC spawning /usr/local/sbin/script: Exec format error
Feb 17 15:53:18 server systemd[1]: my.service: Main process exited, code=exited, status=203/EXEC
Feb 17 15:53:18 server systemd[1]: my.service: Failed with result 'exit-code'.
Feb 17 15:53:18 server systemd[1]: Failed to start Update stuff.
  1. By trial and error looked at the script and saw it didn't have a bang, so added one
# vim /usr/local/sbin/script
#!/bin/bash
echo Update!
  1. Tried running the service again
# systemctl start my.service
# systemctl status letsencrypt.service
● my.service - Update stuff
   Loaded: loaded (/etc/systemd/system/my.service; static; vendor preset: enabled)
   Active: inactive (dead) since Mon 2020-02-17 16:05:45 UTC; 4s ago
  Process: 31781 ExecStart=/usr/local/sbin/script (code=exited, status=0/SUCCESS)
 Main PID: 31781 (code=exited, status=0/SUCCESS)

Feb 17 16:05:45 server systemd[1]: Starting Update stuff...
Feb 17 16:05:45 server test[31781]: Update!
Feb 17 16:05:45 server systemd[1]: Started Update stuff.

(woohoo)

  1. Checked the timers again
# systemctl list-timers
NEXT                         LEFT                  LAST                         PASSED                UNIT                         ACTIVATES
...
Tue 2020-05-12 16:05:45 UTC  2 months 24 days left Tue 2018-03-20 14:13:46 UTC  1 years 10 months ago my.timer            my.service

x timers listed.
Pass --all to see loaded but inactive timers, too.

(now the timer shows that it is going to run again)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .