How to Setup Cron Jobs in Linux

Setting up cron jobs can be done by placing your file in the scheduling folders or by running crontab -e from a terminal.

740 views
d

By. Jacob

Edited: 2020-09-04 10:59

It is possible schedule a command to run repeatedly using cron in Linux, which is useful for scheduling backups and deleting files in your personal Downloads folder.

There are a number of ways to setup a cron job, the easiest is probably to just place a .sh script in the folder that corresponds with the time you want the script to run; but finer control is provided through the crontab -e command as well.

The cron scheduling folders are self-explanatory, and can be found at:

  1. /etc/cron.monthly
  2. /etc/cron.weekly
  3. /etc/cron.daily
  4. /etc/cron.hourly

Note. dots are not allowed in file names located in these folders for technical reasons. A file name should only contain the following characters: [a-z0-9_-], otherwise it will not be executed.

Apparently this restriction is to avoid run-parts executing files left by the Debian package manager ending in .dpkg-old, .dpkg-new, .dpkg-dist, and .dpkg-orig; this behavior is inconsistent and bad, since it is generally a good practice to use proper file extensions.

To have a command run hourly, you would just need to create a .sh script file containing your command in the /etc/cron.hourly folder. For example, using nano:

sudo nano /etc/cron.hourly/my-script-sh

This will edit the /etc/cron.hourly/my-script-sh file; remember to hit CTRL + o (o as as in other) when you are done editing.

To test if the script works, you could fill the file with something like:

#!/bin/sh
echo 'hallo' > '/home/k/cron-test.txt';

This would write the string "hallo" to the /home/k/cron-test.txt file every hour.

Remember to also make the script executable:

sudo chmod +x /etc/cron.hourly/my-script-sh

Setup a cron job in Linux

To setup cron jobs, we can place our scripts within the monthly, weakly, daily, and hourly folders, but if we need finer control — as in down to the minute — over when our task should run, we can also call crontab -e from a terminal.

When crontab -e is called from a terminal, it should automatically ask you which editor you want to use to edit the crontab file; once the file is opened, it should also include an explanation.

The contents of the crontab file may look like this:

# 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

To add a .sh script that will be run every 30 minutes, we may write something like this:

*/30 * * * * /path/to/script.sh

Yay! Dots are allowed in file names when setting up cron jobs this way.

Remember, the syntax is as follows: minute hour day weak month

If an asterisk * is used instead of a numeric value, it means the command or script will be run at any minute/hour/day/weak/month — dependent on the used combination. To run a script at exactly 01:00 (24 hour format), we can write this:

0 1 * * * /path/to/script.sh

Note that this syntax defines the exact time when we want a cron job to run. If we wanted to repeatedly run the job at every minute or hour, we need to use the */[value] syntax; for example, we could write the below to have a job run every other minute:

*/2 * * * * /path/to/script.sh

Or, if we wanted to run the job every 30th minute:

*/30 * * * * /path/to/script.sh

Tell us what you think:

  1. Understanding file permissions in Unix / Linux based systems, and how to make files immutable.
  2. How to search a directory and subdirectories for a given string of text in Linux with the grep command.
  3. Worth knowing in order to make a bootable USB memory stick with Windows on from Linux.
  4. This is why I decided to encrypt my new Flash Drive, and why I think you should too.
  5. About the problem with using sudo with graphical programs in Linux.

More in: Linux Tutorials