Wassim Sboui
Wassim Sboui

Reputation: 1840

Execute crontab twice daily at 00h and 13:30

I want to execute a script twice daily at 00:00 and 13:30 so I tried:

0,30 0,13 * * *

It seems wrong for me, because like this, the script will fire at 00:00, 00:30, 13:00 and 13:30. Any idea?

Upvotes: 82

Views: 181600

Answers (10)

Muhammad Hamza
Muhammad Hamza

Reputation: 31

You can use second modules Condition to run at single time

const currentTime = moment();
      if (currentTime.second() % 10 == 0)

Upvotes: 0

Angel115
Angel115

Reputation: 2274

You CAN NOT do that with cron on a single line. You have to create 2 separate lines like so:

# Will run "YourCommand" at 00:00 every day of every months
#Min   Hours   D of the M       Month   D of the Week   Command
0      0       *                *       *               YourCommand

# Will run "YourCommand" at 13:30 every day of every months
30     13      *                *       *               YourCommand

Or, as a single line, you can run a command every x hours, like so:

# Will run "YourCommand" every 12 hours
0   */12   *   *   *   YourCommand

or

# Will run "YourCommand" at 1am and 1pm every day
0   1,13   *   *   *   YourCommand arg1 arg2

Upvotes: 32

Sabir Hussain
Sabir Hussain

Reputation: 2922

Try this, Only if you have the same minutes for each schedule. This will run your job twice a day at 1:00 & 13:00

0 1,13 * * * 

You can try quickly more variations here: https://crontab.guru/

Upvotes: 0

Ankit Jindal
Ankit Jindal

Reputation: 4050

Try this out:

0 1,13 * * *

What the above code means:

Cron will run at minute 0 past hour 1 and 13

Sharing a screenshot from crontab.guru

crontab-guru

Upvotes: 4

malinda sanjaka
malinda sanjaka

Reputation: 21

try this,

0 10 9/12 ? * *

At second :00, at minute :10, every 12 hours starting at 09am, of every day

Upvotes: 0

Nimish
Nimish

Reputation: 1066

Try this out: 0 6,18 * * *

it will run at minute 0 past hour 6 and 18

Or you can try it out on cronguru

Upvotes: 11

Brian Agnew
Brian Agnew

Reputation: 272417

You can't do what you want in one entry, since the two minute definitions will apply for both hour definitions (as you've identified).

The solution is (unfortunately) use two cron entries. One for 00:00 and one for 13:30.

An alternative is perhaps to execute one script at 00:00. That script would execute your original script, then wait 13.5 hours and then execute that script again. It would be easy to do via a simple sleep command, but I think it's unintuitive, and I'm not sure how cron manages such long running processes (what happens if you edit the crontab - does it kill a spawned job etc.)

Upvotes: 64

Red Cricket
Red Cricket

Reputation: 10480

try ...

00,30 00,13 * * * [ `date +%H%M` == 1330 ] || [ `date +%H%M` == 0000 ] && logger "its time"

Upvotes: 8

David Jeffery
David Jeffery

Reputation: 117

30 0,13 * * * somecommand.sh

This is just purely an example, but you will see that this is a cron entry that will run at 0:30AM and then 1:30PM (13 is 1 in military time). Just comma separate the hours, or comma separate whatever section of the cron.

Upvotes: 1

Ankush
Ankush

Reputation: 1874

Try this-: 00 01,13 * * *

it will run at 1 A.M and 1 P.M

Upvotes: 159

Related Questions