Is it possible to setup a cron job to work on certain 开发者_JAVA技巧days only at night, for example, the first day of each month from 03pm to 09pm?
I want to do an email campaign only at night when the server load is low.
Is it possible to run a cron job at a certain time and stop it at a certain time?
You could use a second cron job at 09pm to start a second program that tells the first program to terminate.
There are a number of ways to do this. One of the easiest might be to have the second program touch terminate.txt
in a convenient place. On each loop of the first program, it could check for this file. If it exists, it could delete the file and gracefully exit.
Set up your cron job to run at desired time every day. Then add a test command to check if it is a first day of the month.
- Make sure your script only processes certain chunk of work and then exits on its own.
- Make sure that the script verifies that only one instance of itself is running.
So your cron job entry would be something like this:
* 15-20 * * * test `date +\%d` == 01 && script_name.sh
The script will be started at every minute between 3:00 pm and 8:59 pm (inclusive).
Not really. You could probably set something up to terminate a running script after six hours, but that usually wouldn't be a good solution because your script has no chance of cleaning things up.
The best thing to do would be to do a clean exit from inside the script when the time limit has been reached.
You can schedule cron to start your program on the first day of the month at 3PM. Your program will have to take care of killing itself at 9PM.
http://en.wikipedia.org/wiki/Cron
精彩评论