Execute cronjobs in lock step - cron

I have two cronjobs, each using a "*/5 * * * *" schedule.
What I really want is to execute them every ten minutes, but the second one 5 minutes later than the first one.
Is there an elegant way to do this?

How about:
*/10 * * * * firstcommand
5-55/10 * * * * secondcommand
This works with at least one cron daemon---Dillon's cron, which I'm the current dev of. Whether it works on Vixie cron, or fcron, or bcron, or whichever cron daemon you happen to be using, I can't say.

Have your first cron task call at, which allows you to schedule a one-off execution at a specific time.

Related

Run a script every few secconds [duplicate]

This question already has answers here:
Running a cron every 30 seconds
(21 answers)
Closed 4 years ago.
I have a php script that i need to run every 4 seconds for one hour a day, what is the best way to do this. I look up a way of using cron and sleep and also watch but what would be the best way of doing this over all?
Watch
watch -n10 command args
cron:
* * * * * /foo/bar/your_script
* * * * * sleep 4; /foo/bar/your_script
* * * * * sleep 8; /foo/bar/your_script
* * * * * sleep 12; /foo/bar/your_script
In short, it seems there isn't a simple way to do this using cron, since cron has a granularity of 60 seconds. Basically you would have to find a workaround.
One workaround, that you seem to have already come across, is to have multiple events with offset start times. E.g. Running a cron every 30 seconds
Another is to do more granular scheduling within the PHP script itself. E.g. In this answer
At that point it might make sense to then also make your PHP script a daemon process: Run php script as daemon process

Poll for every 45 minutes using cron

I need to poll after deployment immediately i.e 0 seconds and then for every 45 mins using cron
Should poll as follows:: 00:00, 00:45, 1:30,2:15,3:00 and so on
Why do you have to use cron?
Your best bet in this case is to not use cron, rather use Mule's in-built fixed-frequency scheduler:
Note how the the default delay is "0" which means that it will run immediately upon deployment, then will run every 45 minutes after.
Here is the configuration-xml:
...
<flow name="polling-frequency-example-flow"
processingStrategy="synchronous">
<poll doc:name="poll-every-forty-five-mins">
<fixed-frequency-scheduler frequency="45" timeUnit="MINUTES"/>
</poll>
<!-- Do Something -->
</flow>
...
I don't know how to poll in Mule, but I can help you with your cron schedule.
Cron doesn't support every-45-minutes. You'll have to break it into three cronjobs:
0,45 0-23/3 * * *
30 1-23/3 * * *
15 2-23/3 * * *
CRON expression to poll every 45 minutes, this will solve ur first problem.
0 0/45 * 1/1 * ? *
Running once immediately after the deployment can't be handled with "Poll" as far as I know. As a workaround, in addition to the Poll component above, create another flow with "QUARTZ Inbound Endpoint" and it has a repeatCount attribute which you can set it to "Zero"(this will run exactly once and won't repeat itself).
Cron expression (for every 45 mins): 0 0/45 * 1/1 * ? *
If you want to run every 45 mins(00:15,01:00 like this) use quartz.
if you used poll operation it will not run every 45 mins, it will run every 45 mins when the project or flow deployed.
Simply use the fixed frequency scheduler construct as stated by #Mooz and then get the current time, check if it is a Sunday and do not process if it is. A cron expression is not really directly intended to handle all of the constraints of running immediately, a frequency relative to starting time rather than a clock schedule, and a day scheduling even with Mule's extensions to cron. Other solutions would be to use two controllers, but this would seem cleaner to me.
run every 10 seconds
0/10 * * * * ?
run every 45 minutes
* 0/45 * * * ?
Instead of using the Cron jobs just go with fixed frequency scheduler simply.
set the value as follows:
frequency: 45
start delay as :0
Time unit as:minutes

Execute scripts through a FIFO QUEUE

I have different script executed by the crontab.
The crontab is like this:
10,20,40,50 23,00-06 * * * /tmp/script1.bash
0,30 * * * * /tmp/script1.bash
*/2 * * * * /tmp/script2.bash
I want to run the two script in a way that I'm sure that the other script is not running, so if the script1 is triggered the script2 has to wait for script1 to finish.
I can't use a wait for the other process to finish because it can cause a starvation...
I would like to find a way in which every time the cron is triggered it adds the script to a queue and then the scripts are executed sequentially.
Which is the best way using only bash?
At the moment I found a solution using flock and exclusive locking mechanism, it should assure a FIFO behavior in terms of execution – Kerby82

Perl Cron Scheduler: start at x time, execute every y minutes forever

I'm using perl cron, and I want to make a rule like this
run every xx min/hours starting at yy:yy time (until the end of time)
How would I put this into a cron string? perl:cron seems to use the same syntax as regular cron so a regular cron string should work
TIA!
The short answer is that you will either need to write this yourself or find a different third-party package, due to your requirements. There's two things you're asking for that cron doesn't do:
Run every X minutes.
Say you want to run every 40 minutes, and you write this */40 * * * *. This actually runs every 60 minutes at 1:40, 2:40, etc.
Start/Stop at time Y/Z.
There's simply no cron syntax for this. You could use a couple more cronjobs to add/remove the main cronjob at the specified times, but this smells a lot like self-modifying code. Given the complexity (read: unreliability), it's probably better to find a different system.
You can specify intervals with a slash. Here's every 5 minutes:
*/5 * * * *
This is every 2 hours:
0 */2 * * *
You cannot give a start/ end time in cron.

Specifying "all odd values" in crontab?

In crontab, I can use an asterisk to mean every value, or "*/2" to mean every even value.
Is there a way to specify every odd value? (Would something like "1+*/2" work?)
Depending on your version of cron, you should be able to do (for hours, say):
1-23/2
Going by the EXTENSIONS section in the crontab(5) manpage:
Ranges can include "steps", so "1-9/2" is the same as "1,3,5,7,9".
For a more portable solution, I suspect you just have to use the simple list:
1,3,5,7,9,11,13,15,17,19,21,23
But it might be easier to wrap your command in a shell script that will immediately exit if it's not called in an odd minute.
Every odd minute would be:
1-59/2 * * * *
Every even minute would be:
0-58/2 * * * *
I realize this is almost 10 years old, but I was having trouble getting 1-23/2 for an every two hour, odd hour job.
For all you users where, exact odd hour precision is not needed. I did the following which suited my teams needs.
59 */2 * * *
Execute the job every two hours, at the 59th Minute.
Try
1-23/2
From your question, I'm assuming Vixie Cron. I doubt this will work with any other cron.
As I read the manual "1-23/2" (for hours) would do the trick.
Works on Cronie
Even with 5 minutes interval e.g.
3-58/5 * * * * /home/test/bin/do_some_thing_every_five_minute

Resources