cron is the job scheduler in Linux systems that can execute commands or scripts at regular intervals. Each task scheduled in cron is called a cron job. The utility used for scheduling these jobs is called crontab.
A common cron job that Linux admins use on their systems is to execute a command or script every 5 minutes. In this guide, we’ll show you how to use crontab to setup a cron job that runs every 5 minutes.
In this tutorial you will learn:
- How to set crontab to execute every 5 minutes
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Any Linux distro |
Software | cron |
Other | Privileged access to your Linux system as root or via the sudo command. |
Conventions |
# – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires given linux commands to be executed as a regular non-privileged user |
Configure cron job for every 5 minutes
Open a terminal on your system and type the following commands to access crontab and setup the cron job.
Each Linux user has their own crontab, and the scheduled jobs will be executed as that user. For example, if you access crontab as root, the jobs you schedule will be executed as root.
- Open crontab (the cron editor) with the following command.
$ crontab -e
- If this is your first time accessing crontab, your system will likely ask you which editor you’d prefer to use. In this example, we’ll go with nano (type
1
and thenEnter
) since it’s the easiest to understand.$ crontab -e no crontab for linuxconfig - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/nano <---- easiest 2. /usr/bin/vim.basic 3. /usr/bin/vim.tiny 4. /bin/ed Choose 1-4 [1]:
- Make a new line at the bottom of this file and insert the following code. Of course, replace our example script with the command or script you wish to execute, but keep the
*/5 * * * *
part as that is what tells cron to execute our job every 5 minutes.*/5 * * * * /path/to/some-script.sh
- Exit this file and save changes. To do that in nano, you’d need to press
Ctrl + X
,Y
, and thenEnter
.
Conclusion
That’s all there is to it. Scheduling jobs in cron is very simple as long as you know the syntax for telling cron how often to run the job, which is */5 * * * *
as we’ve shown in this guide.