1820879 Members
3605 Online
109628 Solutions
New Discussion юеВ

Cron every 2 mins

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Cron every 2 mins

Hi All,

Sorry to ask a basic question. How can we run cron every 2 mins? Should list such 2,4,6,8,etc * * * *? Is there any better way?

Pls help.
Thanks and Best regards,
Negara
Santos
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Cron every 2 mins

That's the way it is done. Some flavors of "improved" UNIX allow more flexibility but stick with the standard syntax and your crontab entries will work everywhere. Now have you considered what happens if for some reason the previous cron job is still running when the new one fires off?
If it ain't broke, I can fix that.
Patrick Wallek
Honored Contributor

Re: Cron every 2 mins

To run every 2 minutes, yes you need to list ALL even (or odd) numbered minutes.

Another way to do it would be via 'at'. At the end of your script you do an 'at scriptname now + 2 minutes' 'man at' for more information.

# cat script
#!/usr/bin/sh
do stuff
do more stuff
do even more stuff
at /dir/script now +2 minutes

Yogeeraj_1
Honored Contributor

Re: Cron every 2 mins

hi Negara,

the most easy and dull way to do it is:
#***************************************
00 * * * * /home/yogeeraj/myscript.sh
02 * * * * /home/yogeeraj/myscript.sh
04 * * * * /home/yogeeraj/myscript.sh
06 * * * * /home/yogeeraj/myscript.sh
..
50 * * * * /home/yogeeraj/myscript.sh
52 * * * * /home/yogeeraj/myscript.sh
54 * * * * /home/yogeeraj/myscript.sh
56 * * * * /home/yogeeraj/myscript.sh
58 * * * * /home/yogeeraj/myscript.sh
#***************************************
# min|hour |day |month|day |script
# | |of mo| |of wk|
#----|-----|-----|-----|-----|-----------


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
Victor Fridyev
Honored Contributor

Re: Cron every 2 mins

Hi,

You can run the script every minute, but to put into the script the following:

typeset -i N=0
N=$(date +%m)%2
if [ $N -ne 0 ]; then
exit
fi
#Your script etc

HTH
Entities are not to be multiplied beyond necessity - RTFM
Victor Fridyev
Honored Contributor

Re: Cron every 2 mins

Oops, sorry

There is a mistake in date format.

typeset -i N=0
N=$(date +%M)%2
if [ $N -ne 0 ]; then
exit
fi

According to the condition the script will exit every odd minute and will work every even minute.

HTH
Entities are not to be multiplied beyond necessity - RTFM
Peter Nikitka
Honored Contributor

Re: Cron every 2 mins

Hi,

you have to setup your cronjob every minute, but can prepend a one-liner to skip the add minute:
* * * * * [ $(date '+\%M 2\%\%p' | dc) -eq 1 ] || /path/2your/your-cron-script

The above construct will NOT have an exit status != 0 for skipped minutes.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dewa Negara_4
Regular Advisor

Re: Cron every 2 mins

Hi All,

It looks fine to me now. Thanks alot for all your help.

Best Regards,
Negara
Santos
Khashru
Valued Contributor

Re: Cron every 2 mins

Change in your script so that it runs in alternate minute and run that script once every hour.