Have cron wait for job to finish before re-launching - cron

I have a cronjob that executes every second minute that usually runs in seconds, but sometimes for several minutes.
I need cron to not execute the command if it's already running when the next minute comes.
The line looks like this
*/1 * * * * cmd
I have tried with this
* * * * * ID=job1 FREQ=1m AFTER=job1 cmd
but to no success.
Is it possible to solve with cron or do I have to implement locking?

You can make a temp file called inProgress (or whatever) and store it in a standard place, and use this to communicate to the next job if it should run or not.
What if flow of the job goes like this:
Check for a standard inProgress
file
If it exists, quit
Else, create inProgress file
Do work
Delete inProgress file.

Related

Azure Function CLI irregular trigger timing and wrong details

I m testing Azure function locally using cli.
I have noticed 2 issues:
Sometimes CLI do not shows correct time when function will be executing. For example I have cron to execute function every two mins but it shows function will be executed after a difference of seconds ? weird.
Often it do not starts execution as per time shown in CLI, few times it took much time and then respond.
Is is normal ? Please guide how I can fix these.
try [TimerTrigger("0 */2 * * * *")] see examples here
* */2 * * * * cron expression means that you want to execute it every second (the first *) of every 2nd minute, so
2:50:00
2:50:01
2:50:02
...
2:50:59
2:52:00
2:52:01
etc
The correct expression is 0 */2 * * * *: execute every 2nd minute when seconds are 0, which should give
2:50:00
2:52:00
Please check if you still have delays after this change, and it so, post it as a new question with exact description of the problem.

Distributing payload to multiple cron jobs

I have a shell script say data.sh. For this script to execute I will pass a single argument say Table_1.
I have a test file which I will get as a result of a different script.
Now in a test file I have more than 1000 arguments to pass to the script.
The file looks like below:
Table_1
Table_2
Table_3
Table_4
and..so..on
Now I want to execute the script to run in parallel.
I am doing this using cron job.
First I am splitting the test file into 20 parts Using the split command in Linux.
split -l $(($(wc -l < test )/20 + 1)) test
I will then have the test file divided to 20 parts such as xaa,xab,xac and so on.
Then run the cron job:
* * * * * while IFS=',' read a;do /home/XXXX/data.sh $a;done < /home/xxxx/xaa
* * * * * while IFS=',' read a;do /home/XXXX/data.sh $a;done < /home/xxxx/xab
and so on.
As this involves lot of manual process. I would like to do this dynamically.
Here is what I want to achieve:
1) As soon as I get the test file I would like it to be split into say 20 files automatically and store at a particular place.
2) Then I would like to schedule the cron job for every day 5 Am by passing the 20 files as arguments to the script.
What is the best way to implement this? Any answers with explanation will be appreciated.
Here is what you could do. Create two cron jobs:
file_splitter.sh -> splits the file and stores them in a particular directory
file_processer.sh -> picks up one file at a time from the directory above, does a read loop, and calls data.sh. Removes the file after successful processing.
Schedule file_splitter.sh to run ahead of file_processor.sh.
If you want to achieve further parallelism, you can make file_splitter.sh write the split files into multiple directories with a few files in each. Let's say they are called sub1, sub2, etc. Then, you can schedule multiple instances of file_processor.sh and pass the sub directory name as an argument. Since the split files are stored in separate directories, we can ensure that only one job processes the files in a particular subdirectory.
It's better to keep the cron command as simple as possible.
* * * * * /path/to/file_processor.sh
is better than
* * * * * while IFS=',' read a;do /home/XXXX/data.sh $a;done < /home/xxxx/xab
Makes sense?
I had written a post about how to manage cron jobs effectively. You may want to take a look at it:
Managing log files created by cron jobs

How to give a random delay on Cron Jobs commands

I have tried a couple of suggestions but always failed.
I want to get a php file runned almost every minute, for this I am using the command:
* * * * * wget http://link.php
How can I give that command a random delay?
For example,
1st time : working in 65th second
2nd time : working in 67th second
3rd time : working in 72nd second

Can anyone tell what this cronjob does?

I am learning about cronjob and I found this piece of code in one project which fetches record from twitter,
the code goes like this:
#0 * * * * cp /vold/www/Abcd/log/twitter_feed_item_aggregator.log vold/www/Abcd/log/twitter_feed_item_aggregator.log.backup; > /vold/www/Abcd/log/twitter_feed_item_aggregator.log
Can anyone explain what this piece of code does?
Hm... Copies a twitter agregator log each hour, and then clears it.
This part 0 * * * * means 'every 0 minutes'. Minute 0 is when a new hour starts.
This part cp /vold/www/Abcd/log/twitter_feed_item_aggregator.log vold/www/Abcd/log/twitter_feed_item_aggregator.log.backup obviously copies the log to a backup.
This part > /vold/www/Abcd/log/twitter_feed_item_aggregator.log outputs the output of no command to the file, thus clearing it.
The hash at the start of the line comments out the line so it does nothing. Without that it would do as #playcat says.

Quartz Cron Expression: Run Job Every 10 minutes starting NOW (immediately)

I am using Quartz Scheduler using Spring. I want to configure the same with following schedule:
Run Job Every 10 minutes starting NOW
I am using following expression for the same.
0 */10 * * * ?
I thought * in the minutes field would make it run the first minute, but it does not do that way. It runs the first 10th minutes from now and then every 10 minutes afterwards.
Can anybody please suggest me the reason for this behavior and the solution to my problem also?
0 0/10 * 1/1 * ? *
Please see : http://www.cronmaker.com/
check the minute your at now and add them as a list to your crontrigger. if you start the trigger at minute 12 for example add
0 2,12,22,32,42,52 * * * ?
as your cron expression
Edit:
Another solution would be to define a simpletrigger that repeats every ten minutes
SimpleTrigger trigger = new SimpleTrigger("myTrigger",
null,
new Date(),
null,
SimpleTrigger.REPEAT_INDEFINITELY,
10L * 60L * 1000L);
You can use something like
0 1-59/10 * * * ?
That will trigger the job at any minute and 10 minutes after that. I didn't try it but it looks right. :)
*/10 * * * *
Every 10 minutes starting from the moment you create the cron job, wether you prefer (user crontab, /etc/cron.d/, ...).

Resources