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:

  1. Starting custom software if it's not running
  2. 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.

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:

*/5 * * * * ~/.cronscript.sh

Once you're done hold ctrl + x to save. Press y to confirm.

So, as an example, if you wanted to start up Jackett and Sonarr if they weren't running, your script would look like this:

#!/bin/bash
[[ $(pgrep -fu $(whoami) 'JackettConsole.exe') ]] || screen -dmS jackett ~/bin/mono --debug ~/Jackett/JackettConsole.exe
[[ $(pgrep -fu $(whoami) 'NzbDrone.exe') ]] || screen -dmS sonarr /bin/bash -c 'export TMPDIR=~/tmp; ~/bin/mono --debug ~/NzbDrone/NzbDrone.exe'

That example should be easily amended to run your own software as well as showing you which parts of the startup command are necessary.

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