Link to home
Start Free TrialLog in
Avatar of D B
D BFlag for United States of America

asked on

How to have a cron job run until a condition is met

Is it possible to have a job that works in the following manner:
- Starts at 1 PM Mon-Fri
- Is file posted
-- Yes - Process file
-- No - run again in 30 minutes.

I suppose I could do this by starting the job at 1 and sleeping for 30 minutes in a loop if the file isn't there, but is there a way to have a job just start itself again (kind of like create a schedule on the fly)? If so, how would it be done.
Avatar of Kim Ryan
Kim Ryan
Flag of Australia image

Yes, if you want conditional scheduling it needs to be done from within a script (which you initially start from cron). Many 'keep alive' scripts work like this. They monitor a process every ? minutes to see if it has stopped, and then restart it.
Avatar of D B

ASKER

Guess I should add, I want it to only run within a specific window (e.g. start at 1 PM, run every 30 minutes until a file is available or until 5 PM.) Assume the script I am going to execute is called checkfile.sh and if a file is not available and it is not yet 5 PM, how would I schedule it to run again in 30 minutes? I am not a Linux programmer and only occasionally work with it and basically any scripting I've done is by taking an existing script and revising it to do what I want it to do, but I've not been able to find anything even close to this.
I suggest you schedule your script to start at 1pm, 5pm etc
Within the script, check if file is available, wait 30 minutes,but stop after 3h 30 minutes, or 7 loops
Shell scripting can be tricky. I normally use Perl or Python, which can do everything shell can but much more easily
Some sample keep alives here: https://github.com/search?l=Shell&q=keep+alive&type=Repositories&utf8=%E2%9C%93
Avatar of Gerwin Jansen
>> if a file is not available and it is not yet 5 PM, how would I schedule it to run again in 30 minutes?
Until what time do you want to check?

Does there appear a new file once one has been processed? Like you get a file at 1:45 PM and possbily another at 3:15 PM?
Avatar of D B

ASKER

Only one file (possibly) per day. If I check in the 1-5 PM window and there is no file, the assumption is there will not be one posted on that day and I just wait until 1 PM the following day.
ASKER CERTIFIED SOLUTION
Avatar of Gerwin Jansen
Gerwin Jansen
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of D B

ASKER

There is one problem with that, and perhaps I did not clarify. The file is left in the source directory, so it would be processed each time the job ran, if present. Once the job runs and finds a file, I don't want it to run again. I am looking for a SPECIFIC file and there can be multiple (non-matching) additional files in the directory. All of the files are archived at the end of the day by a separate job, so my moving the file out of that directory (or renaming it) is not a viable option.
Perhaps starting job once and running in a loop with sleep a given number of times is the best option, but I was looking for alternatives, and this is a method I've used in other environments, so I thought I'd explore it.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you try my suggestion?
Avatar of D B

ASKER

I am so sorry for the delay. I've had two deaths in the family within a week of each other and have been out more than I've been in the office the past 3 weeks. Please give me a day or two to get caught back up and I'll respond.
Sorry to hear that. It was the automated process that triggered me to ask for an update.

But no rush, take  all the time you need. If you can't spend the time on this now, we can always delete this question so you can ask your question again when it suits you best.
Avatar of D B

ASKER

Used flag file. Works like a charm.
I guess my first thought was resource related; why have a job run if not needed once the file was processed, thus a job that just reschedules itself if necessary, within a specific time-frame would use less resources. But, in the grand scheme of things, how many resources are consumed checking for the existence of a flag file?
Thanks.