All Posts

How to use Cron Jobs on Linux

Posted by Rick on March 17th, 2022
cron-jobs.jpeg

The Cron utility in Linux is one of the most simple but widely used features of a server. Whether you're running websites and need to schedule emails to be sent, or backing up a database, Cron Jobs are super helpful.

How Cron Jobs Work

The cron daemon that runs as part of your operating system runs in the background all the time, a bit like your web server, mail server, or database server.

Every minute it scans through any cron files, including each user's Crontab file. The tasks listed in those files are then checked to see if they need running and then execute.

As part of the configuration of each job, you can set how often a task should run. The shortest timeframe is 1 minute, and if you really wanted to you could set your job to run once on a particular date.

Cron.d vs Crontab

You'll often see a mixture of terminology surrounding Cron. Some people will tell you to 'Update your crontab', another person would say 'Create a cron job'.

The confusion here comes from the naming of the cron files. On most systems you'll be able to run the command crontab -e to edit the currently logged-in users cron file. Commands in there are typically limited to only work under your user account, so if you've not got permission to run a command then neither will the cron job.

The system's main cron files which arent specifically owned by a user are usually found in the /etc/cron.d directory. The benefit of these is that they run as the system's root user, and you can specify which user to execute them as.

Each of the files in the cron.d directory can have as many cron jobs as you like inside them. A good use-case for this would be if your server hosted multiple websites, each with a selection of cron jobs. You could create individual files for each site (e.g /etc/cron.d/serverauth.com and keep all of that site's cron jobs organized into one location.

Creating a cron job

We're going to go with the assumption that you're going to want to keep things organized in the standard /etc/cron.d directory for this section. If however, you'd rather place your cronjobs into the individual user crontab files, you can do so by logging into your server via SSH and running crontab -e as the user.

To get started you'll need to be logged in as a user that has permission to write to the cron directory, either directly or by using the sudo command.

For our example, we're going to create a cron job that runs once a day to execute a backup script.

To get started, whilst logged into SSH create your file like so:
*Note: We're using the Nano editor here but if you're familiar with another editor then go ahead and use that instead!

nano /etc/cron.d/serverauth-backups

Once in the nano editor here's where things can get a little complex. You now need to build a cron job command. Here's the one we're using in this example:

*/15 * * * * serverauth /home/serverauth/scripts/backup.sh

Save the file, and that's it! Congratulations, you just created your first cron job.

Let's break this down

The line in our example doesn't explain a whole lot, and it's one of the biggest hurdles to overcome with cron jobs.

To help explain this a bit better we've put together a text-based helper that you can drop into your own cron files for future reference:

# Example of job definition:
# .---------------- Minute (0 - 59)
# |  .------------- Hour (0 - 23)
# |  |  .---------- Day of month (1 - 31)
# |  |  |  .------- Month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- Day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * username command-to-run

Run Time

The first section tells Cron how often we want our task to run, and provides a huge amount of flexibility. There are five parameters to set here:

  • Minute
  • Hour
  • Day of month
  • Month
  • Day of week

Each of these fields supports a number, an asterisk for 'all' or in the case of the Month and Day of week options you can enter a short month (e.g 'apr') or day (e.g 'wed').

You can also set offsets. For example, if you want a job to run every 15 minutes, you can do */15. There are a whole host of options to play with here, and thankfully there's some excellent free web tools that can help explain all the available options. A few good examples where you can play around with the cron options can be found below:

Advanced options

One of the benefits of cron is that the command you execute doesn't have to just link to a file on your server. You can run commands in-line. So if you want to write the current time to a file every minute, you could do this:

* * * * * root date > /tmp/date.txt

This provides some handy ways of handling responses from your scripts as well. Let us say you want to ensure your backup script is completed successfully, you could have the cron job write to a log file every time it runs:

15 * * * * /home/serverauth/scripts/backup.sh > /var/log/backups.log

Some more examples

Send an email to someone every New Year

Note: assumes you have a basic mail server running already

MAILTO="user@example.com"
0 0 1 1 * echo "Happy new year!"

Take a backup twice a day at 2am and 2pm, and write a log file

0 2,14 * * * /home/serverauth/scripts/backup.sh > /var/log/backups.log

As you can see, there are a bunch of handy things you can do with Cron Jobs to make your life easier. And whilst we've outlined how to manually manage those here, you can also do this all through a web-based GUI, making things even easier.

ServerAuth provides an easy-to-use web interface, allowing you to build and manage Cron Jobs across all of your servers.

Server Management & Security doesn't have to be a full time job.

ServerAuth provides a whole host of management tools, from controlling who can access your server, to adding cron jobs, securing your servers and installing packages. And with an ever growing suite of tools you'll always be one step ahead!

Start for free
Server Management Software Screenshot
ServerAuth
Server Management & SSH Security Software
 on Twitter
Copyright © ServerAuth Ltd
Registered in England No. 13996293
All Rights Reserved.
Solutions
Support
Customers
ServerAuth
The Legal Bits