How to run cron job every 2 hours?

UbuntuCron

Ubuntu Problem Overview


How can I write a Crontab that will run my /home/username/test.sh script every 2 hours?

Ubuntu Solutions


Solution 1 - Ubuntu

Just do:

0 */2 * * *  /home/username/test.sh 

The 0 at the beginning means to run at the 0th minute. (If it were an *, the script would run every minute during every second hour.)

Don't forget, you can check syslog to see if it ever actually ran!

Solution 2 - Ubuntu

The line should read either:

0 0-23/2 * * * /home/username/test.sh

or

0 0,2,4,6,8,10,12,14,16,18,20,22 * * * /home/username/test.sh

Solution 3 - Ubuntu

0 */2 * * *

The answer is from https://crontab.guru/every-2-hours. It is interesting.

Solution 4 - Ubuntu

0 */1 * * * “At minute 0 past every hour.”

0 */2 * * * “At minute 0 past every 2nd hour.”

This is the proper way to set cronjobs for every hr.

Solution 5 - Ubuntu

To Enter into crontab :

crontab -e

write this into the file:

0 */2 * * * python/php/java yourfilepath

Example : 0 */2 * * * python ec2-user/home/demo.py

and make sure you have keep one blank line after the last cron job in your crontab file

Solution 6 - Ubuntu

first do crontab -l to see your existing crontab and jobs if you don't anything then do crontab -e

check you editor maybe VI or nano or anything like that.. go to insert mode by 'i', command should be like (cron expression)[space](program execution address from home)[space](your script address from home)

example (0 /2 * * * /conda///bin/python3 ///USERNAME/TEST_PYTHON_SCRIPT.py >> execution_log.txt)

>>execution_log.txt will have the execution log of your script.

once you have your command correctly placed exit the editor by saving the file for nano -> ctrl + x for vi -> :wq!

check your scripts with some email/print statement.

Solution 7 - Ubuntu

If one needs a simple cron schedule generator, this one can do that well: https://crontab.guru/#_/2___*

* */2 * * *

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVigneshView Question on Stackoverflow
Solution 1 - UbuntuAdamView Answer on Stackoverflow
Solution 2 - UbuntuJames AndersonView Answer on Stackoverflow
Solution 3 - Ubuntug10guangView Answer on Stackoverflow
Solution 4 - UbuntuBibin JosephView Answer on Stackoverflow
Solution 5 - UbuntuSuraj KulkarniView Answer on Stackoverflow
Solution 6 - Ubuntuuser15058224View Answer on Stackoverflow
Solution 7 - Ubuntusci9View Answer on Stackoverflow