ni_hao
ni_hao

Reputation: 428

systemd timer to start .sh script daily at 2 different hours

I have a bourne shell script (at my NAS) that handles the ffmpeg recording of all my ipcams. For switching the record time (etc) of some cams, that sh script should be restarted daily at 2 different hours (07:00am and 10:00pm), which is configured in the bash script and works well.

  1. To start the sh script, I make use of systemd with the following .service file:

    [Unit] Description=record ipcams After=tmp.mount network.target Requires=network.target RequiresMountsFor=/media/USB2

    [Service] Type=forking PIDFile=/var/run/cams_record.pid ExecStart=/bin/bash -c '/media/USB2/movie/cams/cams_record.sh' TimeoutStopSec=1 Restart=always

    [Install] WantedBy=multi-user.target

So far so good. Now what I actually want, is to restart that script file daily at 07:00am and 10:00pm (or restart the previous mentioned .service at those two times) thus I thought to make use of a systemd timer. I created such a timer for 07:00 am (with the option: OnCalender=07:00)

Question is: having a (permanently running) service, how do I restart that service (and thus the script file) at 07:00am and 22:00pm. I can of course make use of 2 systemd timers (1 for 07:00am and 1 for 10:00pm), but is there a possibility to combine these; i.e. using 1 systemd timer for both times.

Upvotes: 4

Views: 3210

Answers (2)

Dwight Schrute
Dwight Schrute

Reputation: 41

You can use several OnCalendar in one timer, see documentation https://www.freedesktop.org/software/systemd/man/systemd.timer.html#OnCalendar=

[Unit]
Description=test

[Timer]
OnCalendar=07:00
OnCalendar=10:00
Unit=test.service

[Install]
WantedBy=timers.target

Upvotes: 4

papey
papey

Reputation: 4134

With a templates timers, you can do something like this

cat [email protected] 
[Unit]
Description=test

[Timer]
OnCalendar=%i:00
Unit=test.service

[Install]
WantedBy=timers.target

Then :

systemctl daemon-reload

and

systemctl start [email protected]
systemctl start [email protected]

Source : https://fedoramagazine.org/systemd-template-unit-files/ and https://jason.the-graham.com/2013/03/06/how-to-use-systemd-timers/

Upvotes: 3

Related Questions