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

Due to the way cron works, prior to setting up any tasks you should add the following to the top of the crontab:

SHELL=/bin/bash

You can then set up the crontab to run a command that will check a job is running and if not, run it. The example below would check process every 5 minutes. Naturally, replace process with the actual process name you want to check and run.

*/5 * * * * [[ $(pgrep -f -u $(whoami) 'process') ]] || process

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