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
0
votes
1 answer

Cron job is not triggered on Ubuntu VM

Ubuntu In my Ubuntu VM, I have configured a cronjob cat /var/spool/cron/crontabs/* MAILTO="myemail@gmail.com" * * * * * python /home/forge/web-app/database/backup_mysql.py I checked pgrep cron I got number printing out fine. It been 5 mins now,…
cyb3rZ
  • 43,853
  • 82
  • 251
  • 430
0
votes
1 answer

Cron jobs in Azure: Automation, Scheduler, WebJobs or other solution?

Azure provides different ways to automate things, such as Automation, Scheduler, Elastic Jobs and WebJobs. Comparison between WebJobs and Scheduler here. Now WebJobs supports crontab format. You can also set up crontab in Azure VM that is a solution…
hhh
  • 44,388
  • 56
  • 154
  • 251
0
votes
1 answer

Change output format into SQLplus using Crontab

I'm using crontab to run SQL script. Below is my script: #!/bin/sh export ORACLE_HOME=/opt/xxx/oracle/client date=$(date +%d.%m.%y-%T) echo "select col1, col2 from table1;" |/opt/xxx/oracle/client/bin/sqlplus…
4est
  • 2,440
  • 6
  • 25
  • 46
0
votes
2 answers

My cron job should be running only on Sunday, but it's running on other days as well?

I have a cronjob that's end goal is to make a database backup on the first Sunday of every month (and remove the previous month's backup in the process). Here's what I have it defined as: 0 1 1-7 * * test `date +\%w` -eq 0 && rm…
TrolliOlli
  • 807
  • 1
  • 7
  • 16
0
votes
1 answer

How do I restart a program on ubuntu without using loops and crontab?

I have a program running on ubuntu server, I want to restart it if it gets terminated for some reason, I did try an infinite loop however it is a very processor hungry task and using crontab is also not quite feasible as I don't want to run my…
shahnshah
  • 530
  • 4
  • 15
0
votes
3 answers

Shell script wont run in cron as root

I have the following code in my root cron file: PATH=/opt/someApp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 45 12 * * * /home/ben/MetaBackup/metabackup.sh 2>&1 >/dev/null | slacktee.sh -t "Metabase Backup Error" -a…
0
votes
5 answers

Scheduled tasks (cronjob-aternative) with Zend framework?

I have a webapplication, written in PHP (Zend Framework) and I'd like to execute a (few) script(s) every once in a while. For example once a day. I know this can be done by using crontab and cronjobs, but not all hostingproviders have these…
koenHuybrechts
  • 868
  • 4
  • 15
  • 28
0
votes
1 answer

Multithreading Perl script and crontab/init script

I have an issue with a Perl script using thread. It works fine when I launch it manually but when I launch it using crontab I have this feedback: Perl exited with active threads: 0 running and unjoined 1 finished and unjoined 0 running…
0
votes
1 answer

No attachment when using crontab to send mail

I use a really simple script to send an attachment every day in my email. When I run the script manually, it works fine, but when I run it using crontab, I receive the email, but without the attachment. Script: #!/bin/sh uuencode…
0
votes
0 answers

Ubuntu crontab doesn't find my script after many different ways of running it

I've got an Ubuntu dedicated machine and I have to create a cronjob that runs my script at 3 am every day. When I try to make the cronjob it just gives me an error that the file couldn't be found, but it's directly there... I've tried to run it with…
Tomaz Leopold
  • 65
  • 1
  • 7
0
votes
1 answer

In crontab i write 10 15 7,23 * Thu /home/ravikant/script1 . Is it run twice when 7 or 23 date is Thu?

In crontab I wrote 10 15 7,23 * Thu /home/ravikant/script1. Is it run twice when 7 or 23 date is Thu? 10 15 7,23 * Thu /home/ravikant/script1 I want to run script on 7 and 23 as well as every Thursday so I write above line to execute the script.…
0
votes
1 answer

How to manage single cron job for mutliple time zone

I want to create single cron job for UTC time and send notification to users according to their timezone time.
Love-Kesh
  • 687
  • 6
  • 14
0
votes
1 answer

Ruby Gem not found when using sudo as root

I have a VM, and on that VM I was having vnc/display issues when logging in as anything except root. Since I am the only person using it, I just do everything as root. I put together some ruby scripts I want to execute as root with crontab, and they…
Tennesseej
  • 379
  • 2
  • 3
  • 11
0
votes
0 answers

Mkdir in current directory with crontab

I need to create a folder in the current directoy of the shell script im executing. This piece of code work very well when I execute the script myself but when it get executed by crontab, it always create the folder in the top layer of the user…
Jmercier13
  • 13
  • 5
0
votes
1 answer

Crontab throwing error

I have the following content in crontab: 20 1,7,13,18 * * * /usr/sbin/automysqlbackup 15 * * * * root find /opt/activeMq/activemq-data/localhost/KahaDB/ -mtime +10 -type f -delete but I get this when I restart cron service: Feb 20 08:43:27 .…
Vivek Sadh
  • 4,109
  • 2
  • 27
  • 44
1 2 3
99
100