Running a cron job at 2:30 AM everyday
Asked Answered
N

7

383

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.

Neediness answered 5/2, 2013 at 14:48 Comment(2)
Beware: in many jurisdictions that switch from standard time to daylight time, the local time hour between 02:00 and 03:00 does not exist on the "spring forward" switchover day, and happens twice on the "fall back" day. Weird but true. If your machine's OS is configured for a local time zone in one of those jurisdictions (most of the USA, for example) your job won't run on the spring forward day, and will run twice on the fall back day. Use UTC, or avoid the strange hour.Nerissanerita
For double checking, this site (crontab.guru) is very helpful, and I also found GitHub CoPilot would add a comment stating what the cron entry meant, which was a nice way to double check.Doolie
C
614
crontab -e

add:

30 2 * * * /your/command
Computer answered 5/2, 2013 at 14:51 Comment(0)
C
184
  1. To edit:

    crontab -e
    
  2. Add this command line:

    30 2 * * * /your/command
    
    • Crontab Format:

      MIN HOUR DOM MON DOW CMD

    • Format Meanings and Allowed Value:
    • MIN Minute field 0 to 59
    • HOUR Hour field 0 to 23
    • DOM Day of Month 1-31
    • MON Month field 1-12
    • DOW Day Of Week 0-6
    • CMD Command Any command to be executed.
  3. Restart cron with latest data:

    service crond restart
    
Confined answered 21/2, 2016 at 8:17 Comment(1)
use "service cron restart" for ubuntuConfined
T
80

As seen in the other answers, the syntax to use is:

  30 2 * * * /your/command
# ^  ^
# |   hour
# minute

Following the crontab standard 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

It is also useful to use crontab.guru to check crontab expressions.

The expressions are added into crontab using crontab -e. Once you are done, save and exit (if you are using vi, typing :x does it). The good think of using this tool is that if you write an invalid command you are likely to get a message prompt on the form:

$ crontab -e
crontab: installing new crontab
"/tmp/crontab.tNt1NL/crontab":7: bad minute
errors in crontab file, can't install.
Do you want to retry the same edit? (y/n) 

If you have further problems with crontab not running you can check Debugging crontab or Why is crontab not executing my PHP script?.

Tichonn answered 12/8, 2016 at 9:56 Comment(1)
crontab.guru is a very useful resourceHaruspex
H
9

An easy way to write cron is to use the online cron generator It will generate the line for you. One thing to note is that if you wish to run it each day (not just weekdays) you need to highlight all the days.

Herc answered 1/4, 2016 at 1:54 Comment(0)
R
4

As an addition to the all above mentioned great answers, check the https://crontab.guru/ - a useful online resource for checking your crontab syntax.

What you get is human readable representation of what you have specified.

See the examples below:

Ryeland answered 1/10, 2018 at 13:42 Comment(0)
B
1

30 2 * * * wget https://www.yoursite.com/your_function_name

The first part is for setting cron job and the next part to call your function.

Baird answered 6/2, 2018 at 13:45 Comment(0)
W
1

30 2 * * * Every Day at 2:30 Am

30-31 2 * * * Every Day at 2:30 -31 am

Along with he answers its important to understand the cron expressions , i face a lot of difficulty in understanding . But an intuitive way to understand is given here .

Woodbridge answered 20/2, 2023 at 6:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.