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
980
votes
25 answers

How do I list all cron jobs for all users?

Is there a command or an existing script that will let me view all of a *NIX system's scheduled cron jobs at once? I'd like it to include all of the user crontabs, as well as /etc/crontab, and whatever's in /etc/cron.d. It would also be nice to see…
yukondude
  • 24,013
  • 13
  • 49
  • 58
572
votes
12 answers

Restarting cron after changing crontab file?

Do I have to restart cron after changing the crontable file?
bArmageddon
  • 8,088
  • 6
  • 22
  • 40
531
votes
22 answers

How to create a cron job using Bash automatically without the interactive editor?

Does crontab have an argument for creating cron jobs without using the editor (crontab -e)? If so, what would be the code to create a cron job from a Bash script?
Raúl Roa
  • 12,061
  • 13
  • 49
  • 64
512
votes
31 answers

How to run a cron job inside a docker container?

I am trying to run a cronjob inside a docker container that invokes a shell script. Yesterday I have been searching all over the web and stack overflow, but I could not really find a solution that works. How can I do this?
C Heyer
  • 5,123
  • 3
  • 10
  • 8
469
votes
9 answers

How do I get a Cron like scheduler in Python?

I'm looking for a library in Python which will provide at and cron like functionality. I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron. For those unfamiliar with…
jamesh
  • 19,863
  • 14
  • 56
  • 96
468
votes
19 answers

How to pass in password to pg_dump?

I'm trying to create a cronjob to back up my database every night before something catastrophic happens. It looks like this command should meet my needs: 0 3 * * * pg_dump dbname | gzip > ~/backup/db/$(date +%Y-%m-%d).psql.gz Except after running…
mpen
  • 272,448
  • 266
  • 850
  • 1,236
436
votes
20 answers

Running a cron every 30 seconds

Ok so I have a cron that I need to run every 30 seconds. Here is what I have: */30 * * * * /bin/bash -l -c 'cd /srv/last_song/releases/20120308133159 && script/rails runner -e production '\''Song.insert_latest'\''' It runs, but is this running…
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
378
votes
7 answers

Running a cron job at 2:30 AM everyday

How to configure a cron job to run every night at 2:30? I know how to make it run at 2, but not 2:30.
user1856596
  • 7,027
  • 10
  • 41
  • 63
369
votes
10 answers

How do I schedule jobs in Jenkins?

I added a new job in Jenkins, which I want to schedule periodically. From Configure job, I am checking the "Build Periodically" checkbox and in the Schedule text field added the expression: 15 13 * * * But it does not run at the scheduled time. Is…
Sangram Anand
  • 10,526
  • 23
  • 70
  • 103
365
votes
20 answers

Where can I set environment variables that crontab will use?

I have a crontab running every hour. The user running it has environment variabless in the .bash_profile that work when the user runs the job from the terminal, however, obviously these don't get picked up by crontab when it runs. I've tried setting…
James
  • 15,085
  • 25
  • 83
  • 120
332
votes
3 answers

Run Cron job every N minutes plus offset

*/20 * * * * Ensures it runs every 20 minutes, I'd like to run a task every 20 minutes, starting at 5 past the hour, is this possible with Cron? Would it be: 5/20 * * * * ?
AJP
  • 26,547
  • 23
  • 88
  • 127
329
votes
11 answers

How to run crontab job every week on Sunday

I'm trying to figure out how to run a crontab job every week on Sunday. I think the following should work, but I'm not sure if I understand correctly. Is the following correct? 5 8 * * 6
dev_fight
  • 3,345
  • 3
  • 14
  • 5
321
votes
2 answers

Using crontab to execute script every minute and another every 24 hours

I need a crontab syntax which should execute a specific PHP script /var/www/html/a.php every minute. The execution on every minute must start at 00:00. The other task which must execute a script at 00:00 /var/www/html/reset.php (once every 24…
Michael
  • 6,377
  • 14
  • 59
  • 91
318
votes
20 answers

Cron and virtualenv

I am trying to run a Django management command from cron. I am using virtualenv to keep my project sandboxed. I have seen examples here and elsewhere that show running management commands from within virtualenv's like: 0 3 * * * source…
John-Scott
  • 3,213
  • 3
  • 16
  • 7
306
votes
21 answers

A cron job for rails: best practices?

What's the best way to run scheduled tasks in a Rails environment? Script/runner? Rake? I would like to run the task every few minutes.
jes5199
  • 18,324
  • 12
  • 36
  • 40
1
2 3
99 100