Skip to main content

Cron Tasks

Setting up a cron task on a Linux system involves using the cron daemon to schedule and automate the execution of tasks or scripts at specified intervals. Here's a step-by-step guide to setting up a cron task:

Open the Cron Table for Editing

The crontab command is used to manage cron jobs for individual users. To open your user's cron table for editing, open a terminal and run:

crontab -e

This will open the default text editor (usually vi or nano) with your user's cron table.

Scheduling the Cron Job

We recommend running every 1-5 minutes for the best results:

*/1  * * * * command

Syntax of a Cron Job

A cron job has the following syntax:

* * * * * sf run
  • The five asterisks represent the schedule or timing when the job should run.
  • * in each position means "every." For example, * * * * * would mean "every minute."

Logging

It's a good practice to log the output of your cron jobs. You can do this by appending >> /path/to/logfile.log 2>&1 to your cron job command. For example:

*/1 * * * * sf run >> /path/to/logfile.log 2>&1

This will capture both standard output and standard error in the specified log file.

Saving and Exiting

  • In vi, press Esc, then type :wq and press Enter to save and exit.
  • In nano, press Ctrl + O to save and Ctrl + X to exit.

Viewing and Managing Cron Jobs

You can view your scheduled cron jobs by running:

crontab -l

To edit your cron jobs again, use crontab -e. You can also remove your user's cron jobs with crontab -r.