148

How can I run command every six hours every day?

I tried the following, but it did not work:

/6 * * * * *  mycommand
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Gandalf StormCrow
  • 25,788
  • 70
  • 174
  • 263

7 Answers7

341

You forgot a *, and you've too many fields. It's the hour you need to care about

0 */6 * * * /path/to/mycommand

This means every sixth hour starting from 0, i.e. at hour 0, 6, 12 and 18 which you could write as

0 0,6,12,18 * * * /path/to/mycommand
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nos
  • 223,662
  • 58
  • 417
  • 506
  • thank your for the response, this is every 6 hours starting what time? the time when the cron is created? – Gandalf StormCrow Jul 19 '12 at 14:19
  • thanks, so if I wanted to run it starting from 15:00 every 6 hours it would be `15 15,23,05,11 * * * /path/to/mycommand` ? – Gandalf StormCrow Jul 19 '12 at 14:26
  • 6
    Yes, though the first field is the minute, which you've set to 15, so that'll mean 15:15,23:15,05:15 and 11:15. (which isn't every 6th hour btw, you might have meant `0 15,21,3,9 * * *`) – nos Jul 19 '12 at 14:31
7

You should include a path to your command, since cron runs with an extensively cut-down environment. You won't have all the environment variables you have in your interactive shell session.

It's a good idea to specify an absolute path to your script/binary, or define PATH in the crontab itself. To help debug any issues I would also redirect stdout/err to a log file.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 1
    thanks for the response, but if I declare environment variables inside my sh file like `export variable=something` I get to still use them inside that same script? and I use date command alot inside the script – Gandalf StormCrow Jul 19 '12 at 14:36
  • @Gandalf - Yes. You can define all your env variables inside the script. That's a good idea since it means your script is standalone and isolated from other stuff you may want to run within cron – Brian Agnew Jul 19 '12 at 16:01
5
0 */6 * * * command

This will be the perfect way to say 6 hours a day.

Your command puts in for six minutes!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rkoots
  • 199
  • 5
  • 4
  • 3
    @ArsenArsen I am pretty sure it was because it doesn't really add anything to the existing accepted answer, wrote 4 years before it. – Adinia Apr 03 '17 at 09:31
5

Please keep attention at this syntax:

* */6 * * *

This means 60 times (every minute) every 6 hours,

not

one time every 6 hours.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Andrea Bisello
  • 1,077
  • 10
  • 23
0
0 */6 * * *

crontab every 6 hours is a commonly used cron schedule.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

nice website for this purpose :)

https://cron.help/

enter image description here

Hamid Zandi
  • 2,714
  • 24
  • 32
-3

Try:

0 */6 * * * command

. * has to

halfer
  • 19,824
  • 17
  • 99
  • 186
rkoots
  • 199
  • 5
  • 4