Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

systemd

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.

like image 602
ni_hao Avatar asked Oct 23 '25 04:10

ni_hao


2 Answers

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
like image 94
Dwight Schrute Avatar answered Oct 26 '25 17:10

Dwight Schrute


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/

like image 44
papey Avatar answered Oct 26 '25 15:10

papey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!