Tuesday, October 14, 2014

Lesson 33: to run cron every 5 seconds or every 79 mins

There's a special case, what if you want to run a cron job not 'hourly-based' like the examples in previous post. Instead, you want to run the job in, let's say, every 5/10 seconds, or every 79 mins?
I've researched about this for today and I think one of the most commonly used solution is to create a simple shell script using bash while loop
For example if you want to create a cronjob that run every 5 seconds you can use this bash while loop:
$ cat every-5-seconds.sh
#!/bin/bash
while true
do
 /home/ramesh/backup.sh
 sleep 5
done
my friend asked me a question, "how do to create a cron job that run every 79 mins?"
personally, i dont think the ordinary crontab can handle this...
btw, what i mean with 79 mins is cron that run at these times:
00:00
01:19
02:38
03:57
05:16
etc

based on above example you just need to change the value of 5 to 4740 (the numbers are in seconds)
you can create this shell script for job that run every 79 mins.
#!/bin/bash

while(true)
do
 echo "start script"
 java -jar /opt/cronUpdateExpiry.jar
 sleep 4740;
done

That's it.

No comments:

Post a Comment