Cron
This guide will show you how to edit the crontab via SSH. Cron is a handy way of executing tasks on a schedule or at certain points and this guide will focus on two different ways to use it:
- Starting custom software if it's not running
- Run tasks on a regular schedule
You'll need to execute some commands via SSH to use this software. There is a separate guide on how to connect to your slot via SSH.
Table of contents
Using the crontab editor
To bring up the crontab editor, simply copy-paste the following command:
crontab -e
If this is your very first time you'll be given a choice of editors. Nano is fine to use so unless you have other desires simply press enter.
You'll be presented with this text:
# Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command
This provides some basic information on crontabs - you can keep it if you like, or get rid of it if you prefer. The lines are commented out so are for information purposes only and don't affect any cron jobs added.
Starting custom software if it's not running
Setting up our script
Open up the script file for writing with the following command (via SSH):
nano ~/.cronscript.sh
Then paste the following at the top of the document to ensure the script is run in the correct shell:
#!/bin/bash
For each piece of software you wish to check and start up, add the following line. Naturally, replace check and start with the actual commands in the relevant software guide; name you want to check and run.
[[ check ]] || start
Once you're done hold ctrl + x to save. Press y to confirm.
Note that you can drop the echo part of any startup commands as no URL will get displayed to you when the cron job runs.
Setting up the cron job
Using the instructions in the section above, Using the crontab editor, open your crontab. Paste the following:
SHELL=/bin/bash */5 * * * * ~/.cronscript.sh
Once you're done hold ctrl + x to save. Press y to confirm.
Example for common software
As an example, if you wanted to auto-restart software found in our wiki such as Jackett or Sonarr your script would look like this:
#!/bin/bash
##set -eux -o pipefail
## autodl-irssi
#[[ $(pgrep -f 'irssi') ]] || screen -dmS autodl irssi
## Bazarr (replace 1234 with a number between 10000 and 30000)
#PORT=1234; [[ $(pgrep -f 'bazarr') ]] || screen -dmS bazarr /bin/bash -c "python ~/bazarr/bazarr.py -p $port"
## Emby
#[[ $(pgrep -f 'EmbyServer') ]] || screen -dmS emby ~/bin/emby
## Jacket
#[[ $(pgrep -f 'Jackett') ]] || screen -dmS jackett /bin/bash -c 'export TMPDIR=~/tmp; cd ~/Jackett; ./jackett_launcher.sh --NoUpdates'
## Lidarr
#[[ $(pgrep -f 'Lidarr') ]] || screen -dmS Lidarr /bin/bash -c 'export TMPDIR=~/tmp; mono --debug Lidarr/Lidarr.exe'
## Ombi (replace 1234 with your configured port)
#PORT=1234; [[ $(pgrep -f 'Ombi') ]] || (pushd ~/opt/Ombi; screen -dmS Ombi ~/opt/Ombi/Ombi --host http://*:$PORT; popd)
## pyLoad
#[[ $(pgrep -f 'pyLoad') ]] || screen -dmS pyLoad -- pyLoadCore --configdir=~/.pyload
## Radarr
#[[ $(pgrep -f 'Radarr') ]] || screen -dmS Radarr /bin/bash -c 'export TMPDIR=~/.config/Radarr/tmp; ~/Radarr/Radarr -nobrowser'
## Resilio Sync
#[[ $(pgrep -f '.rslsync/rslsync.conf') ]] || ~/rslsync/rslsync --config ~/.rslsync/rslsync.conf
## SABnzbd (replace 1234 with a number between 10000 and 30000)
#PORT=1234; [[ $(pgrep -f 'SABnzbd.py') ]] || screen -fa -dmS sabnzbd python ~/sabnzbd-*/SABnzbd.py --browser 0 --server $(hostname -f):$PORT
## SickChill
#[[ $(pgrep -f "python $HOME/SickChill/SickBeard.py -d") ]] || (rm -rf ~/SickChill/SickChill.pid; ~/pip/bin/python ~/SickChill/SickBeard.py -d --pidfile="$HOME/SickChill/SickChill.pid")
## Sonarr v4
#[[ $(pgrep -f 'Sonarr') ]] || screen -dmS sonarr /bin/bash -c 'export TMPDIR=~/tmp; Sonarr/Sonarr -nobrowser'
## syncthing
#[[ $(pgrep -f 'syncthing') ]] || screen -dmS syncthing ~/bin/syncthing
## Tautulli
#[[ $(pgrep -f Tautulli) ]] || screen -dmS Tautulli python ~/Tautulli/Tautulli.py
Uncomment the required software by removing the preceding # (be sure not to remove any lines with a double ## or the first two lines). Feel free to amend the example script to run your software.
Once you're done, ensure the permissions for running the script are set correctly with this command:
chmod 700 ~/.cronscript.sh
Run tasks on a regular schedule
A major use of cron jobs is to get things to run on a schedule. The various configurations are outside the scope of this quick guide so a couple of example (combined with the commented out text you get in the initial crontab, provided above) should be enough to get you going. Variables are used throughout - replace the examples with your own information!
This example will execute flexget every ten minutes:
*/10 * * * * /media/disk_id/username/flexget --cron execute
The following two are equivalent and will run a script called script.sh at midnight:
@daily /media/disk_id/username/script.sh
0 0 * * * /media/disk_id/username/script.sh