LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2015, 03:10 PM   #1
chrisscholbe
LQ Newbie
 
Registered: Jan 2015
Posts: 2

Rep: Reputation: Disabled
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.
 
Old 01-05-2015, 06:36 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
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
 
Old 01-05-2015, 06:49 PM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,600

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by chrisscholbe View Post
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.
 
Old 01-13-2015, 09:37 AM   #4
chrisscholbe
LQ Newbie
 
Registered: Jan 2015
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks guys.

I think I know what to do now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to stop emails sent by daily cron job sneakyimp Linux - Newbie 8 01-03-2013 01:52 PM
Can't get cron job to start von Stalhein Linux - Desktop 9 11-29-2011 04:22 AM
Cannot stop/start services in cron job Sanford Stein Linux - Newbie 4 08-11-2008 05:41 PM
problem in cron job setting gauravdream Linux - General 1 02-23-2004 01:08 AM
need help setting up a cron job !! J.I.L. Linux - Software 1 10-19-2003 09:54 PM


All times are GMT -5. The time now is 10:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration