Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to create a cronjob that will execute some code from 8:30AM to 8:14PM.

share|improve this question
1  
Do you want to run a script repeatedly between 8:30AM and 8:14PM, or do you want a script to run once at 8:30AM, keep running, and then STOP running at 8:14PM? –  Jim Salter Nov 22 '12 at 17:53
add comment

3 Answers

As a coder I would suggest the following method:

1 line inside your cron to start the script

30 8 * * * /code/to/execute1

execute1 starts your script. You script has a while that ends when time passes 20:14 so it keeps running until 20:14 is reached (something like while current_time < 20:14).

Killing a script could have side effects: just maybe the script is currently doing something important. Killing it will without any mercy end it. Gracefully exiting a program is always better.

share|improve this answer
add comment

This link has some good info. Crontabs support ranges in a very straight forward way.

14-30 8 * * * /code/to/execute

This will run once a minuet from 8:14 to 8:30

Check the Man page with man crontab next time it has some very good examples, including this one.

Cron is only used to start programs though, so make sure you don't overlap running instances of your program.

share|improve this answer
    
What if I want to execute the a code continuously from 8:30AM to 12:14PM. –  joystick Nov 22 '12 at 7:26
    
This is not what cron is for, you can "hack" it by running a start cron at 30 8 * * * and a kill command 14 12 * * * –  coteyr Nov 22 '12 at 7:28
    
I didn't understand. –  joystick Nov 22 '12 at 7:32
2  
30 8 * * * prog-to-run 14 12 * * * killall prog-to-run –  coteyr Nov 22 '12 at 7:47
add comment

Ubuntu documentation CronHowto

https://help.ubuntu.com/community/CronHowto

Also

Webmin has a gui for cron jobs (System --> Scheduled Cron Jobs)

http://www.iasptk.com/ubuntu/8429-how-to-install-webmin-on-ubuntu-1204-lts-server

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.