LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Setting CRON Job to Start at 6Pm and Stop at 6AM (https://www.linuxquestions.org/questions/linux-newbie-8/setting-cron-job-to-start-at-6pm-and-stop-at-6am-4175530114/)

chrisscholbe 01-05-2015 02:10 PM

Setting CRON Job to Start at 6Pm and Stop at 6AM
 
I see that you can set CRON jobs to run hourly etc, but I don't see any exapmles of setting one up to start and stop at specified times.

The job I want while run continuously, once started at 6pm, until all of the records are processed or until 6am.

Can this be done?

If so, how?

PS. 6 months ago I couldn't even spell LINUX.

jlinkels 01-05-2015 05:36 PM

Cron cannot stop any jobs, only execute them. So you have to create to Bash scripts.

The first one to start your job and record the PID in a file.
The second one to read the PID and kill the job.
You should check if the PID exists before you start another instance. And take appropriate action.
The normal location to store PID files is /var/run/

Both are started from Cron but have a different purpose.

Alternatively you can follow a more crude way if the application's name is unique. You would start it normally and end it with:
Code:

pkill -f foo
The -f makes kill to terminate any program containing foo.

Then you don't have to write the Bash files. You don't need to record the PID and the kill command can be configured in cron. But it is not very elegant. If you issue the kill command as root you'll kill the programs for all users.

jlinkels

jpollard 01-05-2015 05:49 PM

Quote:

Originally Posted by chrisscholbe (Post 5295798)
I see that you can set CRON jobs to run hourly etc, but I don't see any exapmles of setting one up to start and stop at specified times.

The job I want while run continuously, once started at 6pm, until all of the records are processed or until 6am.

Can this be done?

If so, how?

PS. 6 months ago I couldn't even spell LINUX.

You don't use one cron job. I used this technique to cause a long running process to checkpoint.

1. the script that runs has to set a trap (I was using perl so I could save state). The trap handler can save state, delete a pid file and exit. If no state need be saved, it can just delete the pid file and exit.

2. A second cron job then uses the pid file to decide if it is running. If the pid file doesn't exist, then it can assume it isn't running.

3. The second cron job gets the pid, and sends a user signal to the specified process.

If nothing else is required, then the second cron job can exit.

In my case, the second process was running the same script as the first - when the pid file was removed (I just polled), then the script would then perform the same actions starting with #1.

chrisscholbe 01-13-2015 08:37 AM

Thanks guys.

I think I know what to do now.


All times are GMT -5. The time now is 06:27 AM.