Questions tagged [cron]

Cron is a job scheduler that allows users to run commands periodically.

Cron is a daemon that runs periodic scheduled jobs.

Use the crontab command to edit the table of scheduled jobs.

Use to schedule a job for one execution only at a specific date.

Common pitfalls

If a command works when you type it in a terminal but not from a crontab, here are some common reasons:

  • Cron provides a limited environment, e.g., a minimal $PATH.

    • Try adding this entry to your crontab to see what cron's environment is:

      * * * * * { date; pwd; echo "env:"; env; echo "set:"; set; } > ~/cron.env 
      
  • Cron uses /bin/sh, which may not be the shell you normally use.

  • Cron treats the % character specially (it is turned into a newline in the command).

    • This will affect the date command especially:

      * * * * * date "+%F %T"     # bad: cron sees `date "+`
      * * * * * date "+\%F \%T"   # good
      

Further reading

2314 questions
311
votes
11 answers

how can I make cron run a job right now, for testing/debugging? without changing the schedule!

I have a cron job that is scheduled to run everyday, other than changing the schedule, is there any other way to do a test run of the command right now to see if it works as intended? EDIT: (from the comments) I know the command works fine when…
Ali
  • 6,693
  • 6
  • 32
  • 37
245
votes
5 answers

What is the 'working directory' when cron executes a job?

I have a script that works when I run it from the command line, but when I schedule it with cron I get errors that it cannot find files or commands. My question is twofold: When I schedule a cron job using crontab -e, does it use my user ID as the…
ProfessionalAmateur
  • 3,115
  • 3
  • 18
  • 17
243
votes
4 answers

How can get a list of all scheduled cron jobs on my machine?

My sysadmin has set up a bunch of cron jobs on my machine. I'd like to know exactly what is scheduled for what time. How can I get that list?
Frank
  • 2,771
  • 4
  • 17
  • 9
240
votes
11 answers

Where are cron errors logged?

If I setup cron jobs incorrectly they appear to silently fail. Where should I look for an error log to understand what went wrong?
Brian Lyttle
  • 2,661
  • 3
  • 18
  • 9
210
votes
6 answers

How can I execute `date` inside of a crontab job?

I want to create a log file for a cron script that has the current hour in the log file name. This is the command I tried to use: 0 * * * * echo hello >> ~/cron-logs/hourly/test`date "+%d"`.log Unfortunately I get this message when that…
cwd
  • 44,479
  • 71
  • 146
  • 167
176
votes
3 answers

How to redirect output to a file from within cron?

I have a backup script which I need to run at a particular time of a day so I am using cron for this task and from within cron am also trying to redirect the output of backup script to a logfile. crontab -e */1 * * * * /home/ranveer/backup.sh &>>…
RanRag
  • 5,675
  • 6
  • 20
  • 15
175
votes
6 answers

What does '>/dev/null 2>&1' mean in this article of crontab basics?

I am reading an article about crontab There is something about disabling automatically sending emails. Disable Email By default cron jobs sends an email to the user account executing the cronjob. If this is not needed put the following command…
AGamePlayer
  • 7,415
  • 16
  • 46
  • 55
160
votes
11 answers

How can I run a cron command with existing environmental variables?

How can I run a cron command with existing environmental variables? If I am at a shell prompt I can type echo $ORACLE_HOME and get a path. This is one of my environmental variables that gets set in my ~/.profile. However, it seems that ~/.profile…
cwd
  • 44,479
  • 71
  • 146
  • 167
141
votes
4 answers

Cron vs systemd timers

It was recently pointed out to me that an alternative to cron exists, namely systemd timers. However, I know nothing about systemd or systemd timers. I have only used cron. There is a little discussion in the Arch Wiki. However, I'm looking for a…
Faheem Mitha
  • 34,649
  • 32
  • 119
  • 183
124
votes
4 answers

Day of week {0-7} in crontab has 8 options, but we have only 7 days in a week

Day-of-week: Allowed range 0 – 7. Sunday is either 0 or 7. I found this after Googling, my question is why should both values (0,7) correspond to Sunday?
Ruban Savvy
  • 8,409
  • 8
  • 29
  • 43
109
votes
1 answer

How to change cron shell (sh to bash)?

Is it possible to make commands in crontab run with bash instead of sh? I know you can pass commands to bash with -c, but that's annoying and I never use sh anyway.
Fluffy
  • 2,047
  • 3
  • 15
  • 18
102
votes
6 answers

As root, how can I list the crontabs for all users?

I have a script being run automatically that I can't find in the crontab for the expected users, so I'd like to search all users' crontabs for it. Essentially I want to run a crontab -l for all users.
Highly Irregular
  • 2,455
  • 6
  • 23
  • 24
99
votes
5 answers

Recover cron jobs accidently removed with crontab -r

I entered crontab -r instead of crontab -e and all my cron jobs have been removed. What is the best way (or is there one) to recover those jobs?
Teerath Kumar
  • 1,095
  • 1
  • 7
  • 8
97
votes
2 answers

Location of the crontab file

as many (most?) others, I edit my crontab via crontab -e, where I keep all routine operations such as incremental backup, ntpdate, various rsync operations, as well as making my desktop background christmas themed once a year. From what I've…
Jarmund
  • 1,068
  • 1
  • 8
  • 11
94
votes
3 answers

Open a window on a remote X display (why "Cannot open display")?

Once upon a time, DISPLAY=:0.0 totem /path/to/movie.avi after ssh 'ing into my desktop from my laptop would cause totem to play movie.avi on my desktop. Now it gives the error: No protocol specified Cannot open display: I reinstalled Debian…
justin cress
  • 1,311
  • 2
  • 10
  • 14
1
2 3
99 100