48

I'm trying to figure out how to set cron to run every day at 6 p.m. Is this correct?

The reason I'm asking is this is for a production server, so I need to be sure.

* 18 * * *
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
David Custer
  • 621
  • 1
  • 7
  • 11
  • There are many things that can go wrong with cron jobs on a production server. Be sure to test it, maybe with a "dry run" command first. – Thilo Dec 01 '14 at 03:54
  • 1
    @Thilo I have a duplicate virtual machine I was planning on testing it on. Thank you for your advice!! – David Custer Dec 01 '14 at 04:03
  • see https://crontab.guru/#0_18_*_*_* (_I am not related to the site_) – ccpizza Dec 23 '17 at 20:34
  • https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm will help us understand cron expressions clearly – Amulya Koppula Mar 14 '22 at 08:53

2 Answers2

133
0 18 * * * command to be executed
^ you need to set the minute, too. Else it would be running every minute on the 18th hour

How to setup a cronjob in general:

 # * * * * *  command to execute
 # │ │ │ │ │
 # │ │ │ │ │
 # │ │ │ │ └───── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
 # │ │ │ └────────── month (1 - 12)
 # │ │ └─────────────── day of month (1 - 31)
 # │ └──────────────────── hour (0 - 23)
 # └───────────────────────── min (0 - 59)

What does Asterisk (*) mean

The asterisk indicates that the cron expression matches for all values of the field. E.g., using an asterisk in the 4th field (month) indicates every month.

Sidenote

Other special characters in cronjobs

Slash ( / )

Slashes describe increments of ranges. For example 3-59/15 in the 1st field (minutes) indicate the third minute of the hour and every 15 minutes thereafter. The form "*/..." is equivalent to the form "first-last/...", that is, an increment over the largest possible range of the field.

Comma ( , )

Commas are used to separate items of a list. For example, using "MON,WED,FRI" in the 5th field (day of week) means Mondays, Wednesdays and Fridays.

Hyphen ( - )

Hyphens define ranges. For example, 2000-2010 indicates every year between 2000 and 2010 AD, inclusive.

Percent ( % )

Percent-signs (%) in the command, unless escaped with backslash (), are changed into newline characters, and all data after the first % are sent to the command as standard input.

(source: https://en.wikipedia.org/wiki/Cron)

baao
  • 71,625
  • 17
  • 143
  • 203
20

You should use:

0 18 * * *

This would execute the cron at the 0th minute at 6 PM. You can use a tool like this one in the future.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
pshah
  • 2,052
  • 1
  • 21
  • 40