Questions tagged [cron]

Cron is a time-based job scheduler running as a daemon process in Unix-like computer operating systems. Questions about configuring cron for systems or administration are OFF TOPIC.

The name "cron" comes from the word "chronos", Greek for "time". Cron enables users to schedule jobs (cronjobs, commands or shell scripts) to run periodically at certain times or dates.

It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the internet and downloading emails.

Format

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed

Basic commands

  • crontab -e Edit crontab.
  • crontab -l Show crontab current information.

What can cron do?

Cron can run any command that can normally stand for itself in the command line. For example, you could program cron to run a java program at a certain interval (java main.java && java main).

Popular questions

Troubleshooting

There are several reasons for why a cron job wouldn't run as expected:

  1. Using percent signs, as in date +%F

  2. Incorrect timespec

  3. Adding or omitting username, depending

  4. Cron isn't running

  5. Problems with syntax or permissions

  6. Making assumptions about the environment

These are all described below.

Percent signs

In crontab, percent signs are replaced by line feeds. This is an exceedingly common problem in backup jobs that try to archive with timestamp, e.g.

14 3 0 0 0 tar cfz /backup/file-$(date +%F).tar.gz /home

The % can be escaped by a backslash, but the backslash is not removed. This can cause additional confusion since escaping works for date +\%F, but not for wget "http://host/My\%20File.jpg".

Instead, put the command in a script file and run the script from crontab.

Percent signs may be useful to pass constant data to your command. A simple example:

59 23 * * * cat >$HOME/cron.out%foo%bar%baz

will create ~/cron.out with the 3 lines "foo", "bar", "baz".

Incorrect timespec

People frequently put 1 * * * * mycommand in crontab, wait a few minutes, and wonder why their job didn't run. In this case, it's because the timespec means one minute past every hour, rather than every minute. Try a tool like crontab.guru to sanity check your timespec.

To test your cron job, use * * * * * if you want to run it frequently (every minute) while testing.

Adding or omitting username

On some systems, there's an /etc/crontab. In this file, a username is expected after the timespec, e.g.

57 1 * * * root rkhunter -c -sk

In regular crontabs as seen with crontab -l, for both normal users and root, no username is permitted.

A job will fail to run if a username is added in crontab -e or omitted in /etc/crontab.

Make sure you know which kind of crontab you're editing.

Cron might not be running

Not all systems have cron installed and running by default, and use e.g. anacron instead for periodic scheduling.

ps aux | grep [c]ron will list the cron daemon if it's running.

If it appears to be running, add an entry * * * * * touch /tmp/my_cronjob_ran and check whether the file is created after a minute. You can also check syslog to see a list of recently executed cron jobs.

Problems with syntax or permissions

It's quite common to try to avoid any shell and symbol issues by putting a command in a file, but forgetting to chmod +x before adding the crontab entry.

Ensure the command works in an interactive shell before adding it to crontab.

Do not discard a problematic command's output! If you have a problem with * * * * * command >/dev/null 2>&1 then remove the redirections and examine the output before asking us for help.

(If your command is not writing output to a file, the cron daemon will attempt to deliver any output by mail. Of course, this requires that you have mail set up and properly configured.)

See also the section about user names above; the syntax for the crontab owned by root is different from the syntax of the crontab of regular users.

Making assumptions about the environment

Graphical programs (X11 apps), Java programs, ssh and sudo are notoriously problematic to run as cron jobs. This is because they rely on things from interactive environments that may not be present in cron's environment.

To more closely model cron's environment interactively, run

env -i sh -c 'yourcommand'

This will clear all environment variables and run sh which may be more meager in features than your current shell.

Common problems uncovered this way:

  • foo: Command not found or just foo: not found.

Most likely $PATH is set in your .bashrc or similar interactive init file. Try specifying all commands by full path (or put source ~/.bashrc at the start of the script you're trying to run).

  • Inexplicable shell syntax errors

If your interactive shell is bash, it's easy to write a script which uses Bash features, and which appears to work from the command line. But cron does not run bash, it runs sh, which has a different set of features (even when /bin/sh is a symlink to /bin/bash)!

If your script file has a proper shebang line #!/bin/bash then it will be run by the requested interpreter instead of sh.

  • unable to open display

You are trying to run a graphical program, but don't specify where (Unix never shows anything on "the screen", it only shows things on "a screen"). Put export DISPLAY=:0 at the start of your script if you want to try to open the program on the first display. This will fail if nobody is logged in at the time, and might bring up oddities on a colleague's display if somebody is logged in on the first display, but it's not you.

A common architectural workaround is to split your service into a server running as a headless daemon and a userland component. Have the userland client listen to the server's events via some IPC mechanism (log file, socket, shared memory, shared bus, what have you) and then any or every user can follow what the server is doing, with fairly minimal runtime requirements for the server; and if they are running a graphical display, the client will run in a correctly configured session which trivially has access to the user's display, as well as window manager preferences, individualized client configuration settings, etc. (and if they want to, they can run a simpler command-line client instead, or as well, or neither when they don't want to be distracted, etc. etc. etc.).

  • Any kind of password prompt

If ssh or sudo asks for a password, it will fail in cron since the jobs run in the background with no user to interact with them. Make sure these are set up for automatic, non-interactive operations.

For ssh, this means setting up a key pair, and doing so without a pass phrase (since ssh-agent isn't there to unlock keys). Trying to echo the password to ssh does not work.

For sudo, this means adding entries to sudoers to allow running a command without password, and without requiring a tty. The Unix & Linux Stack Exchange has some posts on how.

It still doesn't work!

So you can run simple commands like touch /tmp/my_cronjob_ran just fine?

And your desired command contains no %s and runs fine with env -i sh -c 'yourcommand'?

Does it still run fine with env -i sh -c 'nohup yourcommand' or env -i sh -c 'yourcommand </dev/null'? If not then it has a problem when it doesn't have standard input and/or a controlling terminal.

Yet it still fails from cron?

Add logging to your command with * * * * * yourcommand >> /tmp/mylog 2>&1 then examine /tmp/mylog which will log output and errors to /tmp/mylog. Don't forget that because you are using >> (append redirection), the newest entries will be at the end of the file. You may have errors at the top, and only discover later that half the way down you corrected the problem. If you only look at the top of the file, you'll miss the changes happening at the bottom.

If after reading /tmp/mylog you still don't know what's wrong, it's time to post a question. Make sure to include relevant output from this file in your post.

References

19423 questions
305
votes
10 answers

How do I write a bash script to restart a process if it dies?

I have a python script that'll be checking a queue and performing an action on each item: # checkqueue.py while True: check_queue() do_something() How do I write a bash script that will check if it's running, and if not, start it. Roughly the…
Tom
  • 42,844
  • 35
  • 95
  • 101
303
votes
10 answers

How to log cron jobs?

I want to know how I can see exactly what the cron jobs are doing on each execution. Where are the log files located? Or can I send the output to my email? I have set the email address to send the log when the cron job runs but I haven't received…
Adrian M.
  • 7,183
  • 15
  • 46
  • 54
297
votes
15 answers

What is the Windows version of cron?

A Google search turned up software that performs the same functions as cron, but nothing built into Windows. I'm running Windows XP Professional, but advice for any version of Windows would be potentially helpful to someone. Is there also a way to…
Thomas Owens
  • 114,398
  • 98
  • 311
  • 431
293
votes
6 answers

Spring cron expression for every day 1:01:am

I'm trying to have my code execute on a fixed schedule, based on a Spring cron expression. I would like the code to be executed every day at 1:01:am. I tried the following expression, but this didn't fire up for me. What's wrong with the syntax…
d-man
  • 57,473
  • 85
  • 212
  • 296
291
votes
6 answers

How to run cron job every 2 hours?

How can I write a Crontab that will run my /home/username/test.sh script every 2 hours?
Vignesh
  • 8,319
  • 6
  • 22
  • 11
278
votes
3 answers

Crontab Day of the Week syntax

In crontab does the Day of the Week field run from 0 - 6 or 1 -7? I am seeing conflicting information on this. wikipedia states 0-6 and other sites I have seen are 1-7. Also what would be the implication or either using 0 or 7 incorrectly? i.e.…
Marty Wallace
  • 34,046
  • 53
  • 137
  • 200
275
votes
14 answers

How to simulate the environment cron executes a script with?

I normally have several problems with how cron executes scripts as they normally don't have my environment setup. Is there a way to invoke bash(?) in the same way cron does so I could test scripts before installing them?
Jorge Vargas
  • 6,712
  • 7
  • 32
  • 29
271
votes
6 answers

How would I get a cron job to run every 30 minutes?

I'm looking to add a crontab entry to execute a script every 30 minutes, on the hour and 30 minutes past the hour or something close. I have the following, but it doesn't seem to run on 0. */30 * * * * What string do I need to use? The cron is…
Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
261
votes
11 answers

Test a weekly cron job

I have a #!/bin/bash file in cron.week directory. Is there a way to test if it works? Can't wait 1 week I am on Debian 6 with root
dynamic
  • 46,985
  • 55
  • 154
  • 231
243
votes
2 answers

Crontab - Run in directory

I would like to set a job to run daily in the root crontab. But I would like it to execute it from a particular directory so it can find all the files it needs, since the application has a bunch of relative paths. Anyway, can I tell crontab to run…
user333746
  • 2,605
  • 3
  • 18
  • 11
233
votes
5 answers

How to run cron once, daily at 10pm

I had entered: * 22 * * * test > /dev/null However, I am being notified via email that this is running every minute. I am confused I guess because I thought this was correct for what I am wanting.
user420095
224
votes
14 answers

How do I create a crontab through a script

I need to add a cron job thru a script I run to set up a server. I am currently using Ubuntu. I can use crontab -e but that will open an editor to edit the current crontab. I want to do this programmatically. Is it possible to do so?
stocked
  • 2,889
  • 4
  • 21
  • 18
189
votes
5 answers

How to specify in crontab by what user to run script?

I have few crontab jobs that run under root, but that gives me some problems. For example all folders created in process of that cron job are under user root and group root. How can i make it to run under user www-data and group www-data so when i…
arma
  • 4,084
  • 10
  • 48
  • 63
176
votes
16 answers

Run cron job only if it isn't already running

I'm trying to set up a cron job as a sort of watchdog for a daemon that I've created. If the daemon errors out and fails, I want the cron job to periodically restart it... I'm not sure how possible this is, but I read through a couple of cron…
LorenVS
  • 12,597
  • 10
  • 47
  • 54
176
votes
25 answers

What is the curl error 52 "empty reply from server"?

I have a cron job setup on one server to run a backup script in PHP that is hosted on another server. The command I've been using is curl -sS http://www.example.com/backup.php Lately I've been getting this error when the Cron runs: curl: (52) Empty…
Paul Sheldrake
  • 7,505
  • 10
  • 38
  • 50