-
Notifications
You must be signed in to change notification settings - Fork 0
Set up the Script to Run Periodically
The goal of the page is to outline some basic steps and provide recommendations in order to configure your system to run the script based on time.
You have to be familiar with cron daemons configuration. I'll remind some steps here, assuming Vixie Cron or its fork is installed.
To run the script once every two days, edit crontab for your user with a
command like crontab -e
. You may want to define SHELL
and PATH
variables
there. If you want color output, define TERM
variable. Consider the
following example:
SHELL=/bin/bash
TERM=xterm-256color
CHECK_SITE=/tmp/simon/simon.sh -avd -i /tmp -s /tmp/simon.old
* * */2 * * $CHECK_SITE >>/tmp/simon.log 2>&1
Output redirection is used to send all output to a log file.
To disable the cron-job, comment it out in the crontab:
#* * */2 * * $CHECK_SITE >>/tmp/simon.log 2>&1
Note: the instruction above implies that your system is running continuously. Don't forget to test the cron-job by making it fire 1-2 minutes into the future.
If your distributive uses systemd, you may decide to set up a timer. Examples of unit files shown below have a benefit of being able to catch up on the missed run of a task. That means if the task was scheduled at a period of time when your system was not running, the task will be executed on a system's startup.
There are two files: simon.timer
and simon.service
. You need to place them
under one of the valid user unit search paths (see systemd.unit(5)
).
E.g., let's place these files in ~/.config/systemd/user/
directory.
The unit file for the timer is self-explanatory:
# simon.timer
[Unit]
Description=Run simon once every two days
[Timer]
Persistent=true
RandomizedDelaySec=90
OnCalendar=*-*-01/2 16:00:01
[Install]
WantedBy=default.target
The service unit file clears TERM variable because the color output
seems to be unsupported by systemd-journlad
. Note: %h
is a variable
that points to your home path (just like echo $HOME
). Also, it's not a good
idea to use -v
: error messages in the system logs should be enough
to get a picture of what's happening.
# simon.service
[Unit]
Description=Check simonstalenhag.se and download new artworks
[Service]
Type=simple
# TERM=dumb by default
Environment=TERM=''
# Disable color output, log only errors to systemd journal
ExecStart=%h/Downloads/simon-master/simon.sh -ac \
-i %h/Downloads/simon-master/ -s %h/Downloads/simon-master/simon.old
# e.g, if there's no internet access, restart the service after 25m
Restart=on-failure
RestartSec=25m
Systemd doesn't track these files and you have to reload the daemon in order
to apply your new units: systemctl --user daemon-reload
. Reloading is also
required if you changed anything in the files and want to use updated
units.
Enable the timer and activate it systemctl --user enable --now simon.timer
.
To see logs, use journalctl --user -u simon.service
.
Disable the timer, run systemctl --user disable --now simon.timer
.