Questions tagged [crontab]

A crontab file specifies shell commands to run periodically on a given schedule. Please review the tag wiki for troubleshooting tips before posting!

Crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept.
Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) which only system administrators can edit.

Each line of a crontab file represents a job and is composed of a CRON expression, followed by a shell command to execute.

Basic 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 

Popular questions


Some implementations of cron, such as that in the popular 4th BSD edition written by Paul Vixie and included in many Linux distributions, add a sixth field to the format: an account username that the specified job will be run by (subject to user existence and permissions).

This is only allowed in the system crontabs, not in others which are each assigned to a single user to configure.
The sixth field is also sometimes used for year instead of an account username, the nncron daemon for Windows does this.

However Vixie cron does not use the sixth column as a year and if used will treat the year as the command to run and fail.

For "day of the week" (field 5), both 0 and 7 are considered Sunday, though some versions of Unix such as AIX do not list "7" as acceptable in the man page.

While normally the job is executed when the time/date specification fields all match the current time and date, there is one exception: if both "day of month" and "day of week" are restricted (not *), then either the "day of month" field (3) or the "day of week" field (5) must match the current day.

Vixie cron supports an extended time expression syntax where you can say e.g. 5,35 to run jobs at five past the hour and five past half; and */5 to run a job every five minutes/hours/days/months. This is not compatible with traditional / POSIX cron.

Debugging crontab

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.

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.)

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.

2783 questions
350
votes
17 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
  • 237,624
  • 230
  • 766
  • 1,119
336
votes
6 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
  • 6,143
  • 9
  • 38
  • 58
319
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,149
  • 14
  • 54
  • 91
295
votes
19 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
  • 13,765
  • 22
  • 80
  • 116
283
votes
10 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
  • 2,875
  • 2
  • 12
  • 5
226
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
  • 28,588
  • 50
  • 124
  • 189
192
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,085
  • 3
  • 15
  • 11
181
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
  • 3,974
  • 10
  • 42
  • 59
172
votes
12 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,341
  • 4
  • 18
  • 18
117
votes
12 answers

Use PHP to create, edit and delete crontab jobs?

Is it possible to use PHP to create, edit and delete crontab jobs? I know how to list the current crontab jobs of the Apache user: $output = shell_exec('crontab -l'); echo $output; But how to add a cron job with PHP? 'crontab -e' would just open a…
datasn.io
  • 11,652
  • 27
  • 102
  • 142
102
votes
8 answers

How do you run a crontab in Cygwin on Windows?

Some cygwin commands are .exe files, so you can run them with the standard Windows Scheduler, but others don't have an .exe extension so can't be run from DOS (it seems like). For example I want updatedb to run nightly. How do I make cron work?
user10306
  • 1,083
  • 2
  • 8
  • 8
100
votes
7 answers

How to set up a cron job to run an executable every hour?

I need to set up a cron job that runs an executable compiled using gcc once every hour. I logged in as root and typed crontab -e Then I entered the following and saved the file. 0 * * * * /path_to_executable However, the cron job does not work. I…
Anonymous
  • 3,923
  • 10
  • 29
  • 37
91
votes
18 answers

AWS Elastic Beanstalk, running a cronjob

I would like to know if there is a way to setup a cronjob/task to execute every minute. Currently any of my instances should be able to run this task. This is what I have tried to do in the config files without success: container_commands: …
Onema
  • 6,423
  • 11
  • 61
  • 97
85
votes
9 answers

gpg encrypt file without keyboard interaction

I am running next command within a crontab to encrypt a file and I don't want a keyboard interaction echo "PASSPHRASE" | gpg --passphrase-fd 0 -r USER --encrypt FILENAME.TXT but I have this answer: gpg: C042XXXX: There is no assurance this key…
coto
  • 2,150
  • 1
  • 18
  • 31
81
votes
17 answers

How to keep Laravel Queue system running on server

I recently setup a Laravel Queue system. The basics are a cronjob calls a command which adds jobs to a queue and calls a second command which sends an email. The system works when I ssh into my server and run php artisan queue:listen, but if I…
zeros-and-ones
  • 3,407
  • 3
  • 30
  • 50
1
2 3
99 100