This tutorial is out of date and no longer maintained.
One of the most standard ways to run tasks in the background on Linux machines is with cron jobs. They’re useful for scheduling tasks on the VPS and automating different maintenance-related jobs. “Cron” itself is a daemon (or program) that runs in the background. The schedule for the different jobs that are run is in a configuration file called “crontab.”
Almost all distros have a form of cron installed by default. However, if you’re using a system that doesn’t have it installed, you can install it with the following commands:
For Ubuntu/Debian:
sudo apt-get update
sudo apt-get install cron
For Cent OS/Red Hat Linux:
sudo yum update
sudo yum install vixie-cron crontabs
You’ll need to make sure it runs in the background too:
sudo /sbin/chkconfig crond on
sudo /sbin/service crond start
Here is an example task we want to have run:
5 * * * * curl http://www.google.com
The syntax for the different jobs we’re going to place in the crontab might look intimidating. It’s actually a very succinct and easy-to-parse if you know how to read it. Every command is broken down into:
The command can be virtually any command you would normally run on the command line. The schedule component of the syntax is broken down into 5 different options for scheduling in the following order:
Here is a list of examples for some common schedules you might encounter while configuring cron.
To run a command every minute:
* * * * *
To run a command every 12th minute on the hour:
12 * * * *
You can also use different options for each placeholder. To run a command every 15 minutes:
0,15,30,45 * * * *
To run a command every day at 4:00am, you’d use:
0 4 * * *
To run a command every Tuesday at 4:00am, you’d use:
0 4 * * 2
You can use division in your schedule. Instead of listing out 0,15,30,45, you could also use the following:
*/4 2-6 * * *
Notice the “2-6
” range. This syntax will run the command between the hours of 2:00am and 6:00am.
The scheduling syntax is incredibly powerful and flexible. You can express just about every possible time imaginable.
Once you’ve settled on a schedule and you know the job you want to run, you’ll have to have a place to put it so your daemon will be able to read it. There are a few different places, but the most common is the user’s crontab. If you’ll recall, this is a file that holds the schedule of jobs cron will run. The files for each user are located at /var/spool/cron/crontab
, but they are not supposed to be edited directly. Instead, it’s best to use the crontab
command.
You can edit your crontab with the following command:
crontab -e
This will bring up a text editor where you can input your schedule with each job on a new line.
If you’d like to view your crontab, but not edit it, you can use the following command:
crontab -l
You can erase your crontab with the following command:
crontab -r
If you’re a privileged user, you can edit another user’s by specifying crontab -u <user> -e
For every cron job that gets executed, the user’s email address that’s associated with that user will get emailed the output unless it is directed into a log file or into /dev/null. The email address can be manually specified if you provide a “MAILTO” setting at the top of the crontab. You can also specify the shell you’d like run, the path where to search for the cron binary and the home directory with the following example:
First, let’s edit the crontab:
crontab -e
Then, we’ll edit it like so:
SHELL=/bin/bash
HOME=/
MAILTO=”example@digitalocean.com”
#This is a comment
* * * * * echo ‘Run this command every minute’
This particular job will output “Run this command every minute.” That output will get emailed every minute to the “example@digitalocean.com” email address I specified. Obviously, that might not be an ideal situation. As mentioned, we can also pipe the output into a log file or into an empty location to prevent getting an email with the output.
To append to a log file, it’s as simple as:
* * * * * echo ‘Run this command every minute’ >> file.log
Note: “>>
” appends to a file.
If you want to pipe into an empty location, use /dev/null
. Here is a PHP script that gets executed and runs in the background.
* * * * * /usr/bin/php /var/www/domain.com/backup.php > /dev/null 2>&1
Restricting access to cron is easy with the /etc/cron.allow
and /etc/cron.deny
files. In order to allow or deny a user, you just need to place their username in one of these files, depending on the access required. By default, most cron daemons will assume all users have access to cron unless one of these file exists. To deny access to all users and give access to the user tdurden, you would use the following command sequence:
echo ALL >>/etc/cron.deny
echo tdurden >>/etc/cron.allow
First, we lock out all users by appending “ALL
” to the deny file. Then, by appending the username to the allow file, we give the user access to execute cron jobs.
There are several shorthand commands you can use in your crontab file to make administering a little easier. They are essential shortcuts for the equivalent numeric schedule specified:
@hourly
- Shorthand for 0 * * * *
@daily
- Shorthand for 0 0 * * *
@weekly
- Shorthand for 0 0 * * 0
@monthly
- Shorthand for 0 0 1 * *
@yearly
- Shorthand for 0 0 1 1 *
and @reboot
, which runs the command once at startup.
Note: Not all cron daemons can parse this syntax (particularly older versions), so double-check it works before you rely on it.
To have a job that runs on start up, you would edit your crontab file (crontab -e
) and place a line in the file similar to the following:
@reboot echo "System start up"
This particular command would get executed and then emailed out to the user specified in the crontab.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Here is a web service that makes setting up cron schedules easier: http://www.corntab.com/pages/crontab-gui
Nice, it almost made my other comment unessarry :O
Thanks :)
Would you be willing to expand more on using /etc/cron.daily, /etc/cron.weekly, and others??
Another crontab syntax generator is at http://www.easycron.com/generator/cronjob
I have scheduled cron job but its running on different at different time, how can i check what timezone cron job running, so i can set offset?
sorry rephrasing my last statement: I have scheduled cron job but its running at different time from which i have scheduled, how can i check what timezone cron job running, so i can set offset?
@Sandeep: You can get the system’s timezone by running:
<pre> cat /etc/timezone </pre>
I have a droplet [LAMP on Ubuntu 12.04] and just created a crontab to run a php script by using “crontab -e” and paste this line to editor:
But it’s seem like not running. How to check cronb and fix?
@nducphu: You can start by having it log its output to a file, then you can find out what’s wrong:
@kamaln7 Me too having, difficulty creating cron job to execute php file I wrote 2 cron job
the first cron running well, it writes ‘hello world’ to hello.txt the second cron never outputted anything on log.txt
the /var/www/write.php consists:
and “write.php” is working fine everytime I execute via putty by issuing
What is probably wrong with the cron?
I also already made write.php, log.txt, test.txt to chmod 777 but still nothing is happened
I am running 2GB Ram 40GB SSD Disk San Francisco 1 Ubuntu 12.04.4 x64
thanks
I’m having an issue when trying to run the service in the background using Ubuntu. When I run the following:
I get the following error: “sudo: /sbin/chkconfig: command not found”.
I was able to update and install without any issue, and when I use “crontab -l” I’m able to see the cron I’ve created. It just seems that I can’t start the service itself.
Thanks in advance for any assistance.
The chkconfig command is CentOS/RHEL-specific. On Debian/Ubuntu, it’s:
However the service is enabled and started automatically once it’s installed so you don’t need to run it.
I’ve set up a cron task which works great, but it’s generating a report every time it runs. What would you recommend as far as disabling reports that are being generated?
The cron I’m running (with sample URL):
*/5 * * * * wget “http://website.com/cronfile” >/dev/null 2>&1
Thanks in advance!
Still having issues with a cron log being saved everytime it runs, even when including “>/dev/null 2>&1”. The cron itself is:
Anyone else run into this issue?
Thanks.
add space after > and before /dev/null */5 * * * * wget “http://website.com/cronfile” > /dev/null 2>&1
Really the best tutorial on cron. all your tutorials on unix are extremely clear and many. Thank you
Thanks for this awesome tutorial, as always. I also had some success adding the following two lines to my cron task:
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DEBIAN_FRONTEND=noninteractive
The first one sets the path - the jobs I was running wouldn’t work without this (apt-get update, apt-get upgrade etc). The second sets the session to be non-interactive, which squashes the warnings such as: “debconf: unable to initialize frontend: Dialog debconf: (TERM is not set, so the dialog frontend is not usable.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (This frontend requires a controlling tty.) debconf: falling back to frontend: Teletype”
Thanks again and happy cronning!
“Authentication token is no longer valid; new one required”
As a side note: if you’re getting the “Authentication token is no longer valid; new one required” error in your syslog, then the problem is normally due to password of the user whose account the cron is running under having expired.
For example, you may be logging in as a normal user and the sudo’ing to root. If you edit the crontab as root, you could get this problem because of some hassle with the root password.
In that case, just create the cron as the user you logged in as.
To check your syslog go
tail -100 /var/log/syslog | grep cron
This will check the last 100 lines of your syslog for the term “cron”.
To check whether the password of a given user has expired use the command
chage -l <username>
(That is a lowercase L)I had this problem on two of my sites where I was trying to run the Wordpress wp-cron.php files from a cron. Fixed now.
http://www.fairtattoo.com http://www.wirelessdomination.com
Drop me a note when you’re there if this was helpful :)
How do I put this crontab into version control? I have my git files in /src.
Yet another crontab editor is at crontab.guru.
So, i actually hate crontab -e and the setup, so i began searching for other ways, and found a easy way to use crontab -e
Normally you write some difficult setup like 10 * * * * Command, and i’ve never really found it easy to use.
So a little fixed and neet thing to do, is running it constantly
* * * * *
and add sleep 10; for 10 seconds.Like this:
And wow, im amazed. I hope it helps some of thoes who are confused, or just dont have the energy… Also, from BASH, you can sometimes write sleep 10m (m = minuts), and it might just work on crontab -e also.
Regards.
Hi… what command for restart server/droplet every 2 minutes ?
Thanks
After a while trying to order a cron task following your tutorial and due my ignorance, I have figure out, users must write the command on a server file: nano /etc/crontab
then write the task on it: 0 4 * * * curl http://www.google.com
and after that restart the cron service: service cron restart
thanks for the detailed info
When I type “
sudo /sbin/chkconfig crond on
” it tells me that command not foundTry
service cron restart
What you could also do is to use systemd to enable the
cron
service to start on boot:To start the service run:
I’ve set up a cron task which works great, but it’s generating a report every time it runs. What would you recommend as far as disabling reports that are being generated?
The cron I’m running (with sample URL):
*/5 * * * * wget “http://website.com/cronfile” >/dev/null 2>&1
Thanks in advance!
Really the best tutorial on cron. all your tutorials on unix are extremely clear and many. Thank you
Thanks for this awesome tutorial, as always. I also had some success adding the following two lines to my cron task:
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DEBIAN_FRONTEND=noninteractive
The first one sets the path - the jobs I was running wouldn’t work without this (apt-get update, apt-get upgrade etc). The second sets the session to be non-interactive, which squashes the warnings such as: “debconf: unable to initialize frontend: Dialog debconf: (TERM is not set, so the dialog frontend is not usable.) debconf: falling back to frontend: Readline debconf: unable to initialize frontend: Readline debconf: (This frontend requires a controlling tty.) debconf: falling back to frontend: Teletype”
Thanks again and happy cronning!
“Authentication token is no longer valid; new one required”
As a side note: if you’re getting the “Authentication token is no longer valid; new one required” error in your syslog, then the problem is normally due to password of the user whose account the cron is running under having expired.
For example, you may be logging in as a normal user and the sudo’ing to root. If you edit the crontab as root, you could get this problem because of some hassle with the root password.
In that case, just create the cron as the user you logged in as.
To check your syslog go
tail -100 /var/log/syslog | grep cron
This will check the last 100 lines of your syslog for the term “cron”.
To check whether the password of a given user has expired use the command
chage -l <username>
(That is a lowercase L)I had this problem on two of my sites where I was trying to run the Wordpress wp-cron.php files from a cron. Fixed now.
http://www.fairtattoo.com http://www.wirelessdomination.com
Drop me a note when you’re there if this was helpful :)
How do I put this crontab into version control? I have my git files in /src.
Yet another crontab editor is at crontab.guru.
So, i actually hate crontab -e and the setup, so i began searching for other ways, and found a easy way to use crontab -e
Normally you write some difficult setup like 10 * * * * Command, and i’ve never really found it easy to use.
So a little fixed and neet thing to do, is running it constantly
* * * * *
and add sleep 10; for 10 seconds.Like this:
And wow, im amazed. I hope it helps some of thoes who are confused, or just dont have the energy… Also, from BASH, you can sometimes write sleep 10m (m = minuts), and it might just work on crontab -e also.
Regards.
Hi… what command for restart server/droplet every 2 minutes ?
Thanks
After a while trying to order a cron task following your tutorial and due my ignorance, I have figure out, users must write the command on a server file: nano /etc/crontab
then write the task on it: 0 4 * * * curl http://www.google.com
and after that restart the cron service: service cron restart
thanks for the detailed info
When I type “
sudo /sbin/chkconfig crond on
” it tells me that command not found