LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   crontab - schedule job to run every minute (https://www.linuxquestions.org/questions/linux-general-1/crontab-schedule-job-to-run-every-minute-802052/)

Hi_This_is_Dev 04-14-2010 05:27 PM

crontab - schedule job to run every minute
 
I am setting crontab to run a script every minute irrespective of the current hour's value:

Code:

[root@localhost bin]# ls
sec.php  showTime.sh
[root@localhost bin]# crontab -l
1 * * * * /root/bin/showTime.sh

[root@localhost bin]#

But it is not working. :(

Any solutions? Does the line mean: "Run this script every day at hour:01"?

kbp 04-14-2010 05:42 PM

Yes it does, for every minute use "*" in the first position

dnaqvi 04-14-2010 06:48 PM

Quote:

Originally Posted by kbp (Post 3935735)
Yes it does, for every minute use "*" in the first position

What should be for every hour?

and

What should be for after every 12 hour?

kbp 04-14-2010 07:02 PM

Probably easier if you check out some examples : http://adminschoice.com/crontab-quick-reference

cheers

Hi_This_is_Dev 04-14-2010 09:31 PM

Quote:

Originally Posted by dnaqvi (Post 3935788)
What should be for every hour?

and

What should be for after every 12 hour?


Before I would check my post here, I had got the solution. I asked an Admin of UNIX here and he told me that *would be used "for every minute".

*/5 means every 5 minutes.

Code:

*/5 * * * * /root/bin/showTime.sh
So, for every hour it should be:

Code:

  */60 * * * * /root/bin/showTime.sh
and for every 12 hour:

Code:

  */60 */12 * * * /root/bin/showTime.sh
/n is the step value

I am going to put those lines in my crontab and will check the output when I come to office tonight. My shift is over. I am going now!

Hi_This_is_Dev 04-15-2010 12:13 PM

For jobs to be run every 12 hour, this crontab entry didn't execute at all:

Code:

*/60 */12 * * * /root/bin/showTime.sh
So, now I am setting this one:

Code:

* */12 * * * /root/bin/at12.sh

\\.\ 04-15-2010 02:58 PM

If this is on your local machine and not a remote server, you could be lazy and use a GUI called Gnome Schedule to set up the task for you.

According to the GUI, you need * * * * * 's to run a task every minute.

TB0ne 04-15-2010 03:53 PM

Quote:

Originally Posted by Hi_This_is_Dev (Post 3935728)
I am setting crontab to run a script every minute irrespective of the current hour's value:

Code:

[root@localhost bin]# ls
sec.php  showTime.sh
[root@localhost bin]# crontab -l
1 * * * * /root/bin/showTime.sh

[root@localhost bin]#

But it is not working. :(

Any solutions? Does the line mean: "Run this script every day at hour:01"?

That's a dangerous thing to do, since CRON will have to fire up, and run your script. If it runs more than 1 minute, you'll then have TWO running...etc, .etc......

Why not just run the script one time (system startup?), and put a "sleep 60" statement in it, and loop back to the beginning?? That way, even if it takes 5 minutes to execute, it'll finish ONE loop, sleep 60 seconds, then run again, instead of cranking off 5 fresh copies...

Hi_This_is_Dev 04-15-2010 04:32 PM

Quote:

Originally Posted by \\.\ (Post 3936826)
If this is on your local machine and not a remote server, you could be lazy and use a GUI called Gnome Schedule to set up the task for you.

According to the GUI, you need * * * * * 's to run a task every minute.


GUI is for kids! No matter how many key strokes a command or task may require, I would prefer the old command line interface or console.

Well, I am using PUTTY to login remotely on the server.

Hi_This_is_Dev 04-15-2010 04:37 PM

Quote:

Originally Posted by TB0ne (Post 3936888)
That's a dangerous thing to do, since CRON will have to fire up, and run your script. If it runs more than 1 minute, you'll then have TWO running...etc, .etc......

Why not just run the script one time (system startup?), and put a "sleep 60" statement in it, and loop back to the beginning?? That way, even if it takes 5 minutes to execute, it'll finish ONE loop, sleep 60 seconds, then run again, instead of cranking off 5 fresh copies...

"system startup": that would require the system or server to bed rebooted at least once after a script has been scheduled to be executed at Startup.

Well, in production servers we cannot reboot servers like that. Besides, running a job every minute is not what we (or at least I) would do. It is just a problem!

Anyways, I have done it.


Code:

*/59 * * * * /root/bin/at60.sh
That would run the script every minute (or 59 seconds.) In fact, I tried

Code:

*/60 * * * * /root/bin/at60.sh
as well. It too worked. But the value that can come there should be 0-59.

TB0ne 04-15-2010 04:53 PM

Quote:

Originally Posted by Hi_This_is_Dev (Post 3936936)
"system startup": that would require the system or server to bed rebooted at least once after a script has been scheduled to be executed at Startup.

Was a *SUGGESTION*, and NOT the only way to run a script.
Quote:

Well, in production servers we cannot reboot servers like that. Besides, running a job every minute is not what we (or at least I) would do. It is just a problem!
You don't have to...it's a SCRIPT, just run it. However you choose to...command line, rc.local, whatever.
Quote:

Anyways, I have done it.
Congratulations...the first time the script takes over 60 seconds to process, enjoy the results.

\\.\ 04-16-2010 01:02 AM

Quote:

Originally Posted by Hi_This_is_Dev (Post 3936931)
GUI is for kids! No matter how many key strokes a command or task may require, I would prefer the old command line interface or console.

Well, I am using PUTTY to login remotely on the server.

So I'M a 42 year old kid then, thanks for that.

We all have to start somewhere and if Linux was not so fiddly and picky, it may have a chance at kicking windows off the top slot... but it isn't.

As for the comment, it was only a suggestion to solve a problem quickly, sometimes the method or means of performing a task are less important than the actual outcome.

Hi_This_is_Dev 04-19-2010 03:25 PM

Quote:

Originally Posted by \\.\ (Post 3937209)
So I'M a 42 year old kid then, thanks for that.

We all have to start somewhere and if Linux was not so fiddly and picky, it may have a chance at kicking windows off the top slot... but it isn't.

As for the comment, it was only a suggestion to solve a problem quickly, sometimes the method or means of performing a task are less important than the actual outcome.



I respect your age and experience, sir. Please, don't mind my comment. I am sometimes a kind of "showing-humour".

Well, I agree to what you have said:

Quote:

"Sometimes the method or means of performing a task are less important than the actual outcome."

colucix 04-19-2010 03:46 PM

Quote:

Code:

*/59 * * * * /root/bin/at60.sh
That would run the script every minute (or 59 seconds.)
Do you mean "every 59 minutes"!?! :confused:

Hi_This_is_Dev 04-22-2010 06:24 PM

Quote:

Originally Posted by colucix (Post 3940734)
Do you mean "every 59 minutes"!?! :confused:

Yes!

colucix 04-23-2010 02:37 AM

Ok. Indeed, I never considered an interval which is not a common divisor of 60. Just out of curiosity I tried the crontab on my system and it runs at 00 and at 59 minutes of every hour! It looks like cron starts the count at 00 minutes in any case and applies as many intervals as they fit in one hour. Does it work like this on your system? Thanks.

Hi_This_is_Dev 04-26-2010 01:40 PM

Quote:

Originally Posted by colucix (Post 3944928)
Ok. Indeed, I never considered an interval which is not a common divisor of 60. Just out of curiosity I tried the crontab on my system and it runs at 00 and at 59 minutes of every hour! It looks like cron starts the count at 00 minutes in any case and applies as many intervals as they fit in one hour. Does it work like this on your system? Thanks.

Yes!

Besides, those who think

Code:

<time fields> sleep 30; someOtherCommandOrScript
would execute the command every 30 seconds, don't seem to have tried it really. Because, when sleep 30 would cause the next command to be delayed for 30 seconds! So, there would be delay of 90 seconds!

So, logically, I have not yet figured out how to shcedule a job to be executed every 30 seconds.

Dralnu 04-28-2010 03:20 AM

Quote:

Originally Posted by Hi_This_is_Dev (Post 3948350)
Yes!

Besides, those who think

Code:

<time fields> sleep 30; someOtherCommandOrScript
would execute the command every 30 seconds, don't seem to have tried it really. Because, when sleep 30 would cause the next command to be delayed for 30 seconds! So, there would be delay of 90 seconds!

So, logically, I have not yet figured out how to shcedule a job to be executed every 30 seconds.

My bash is a little rusty, so here is one method:

Write up a script/function to do what you want, then execute said script in the background, store the PID to a variable, then run sleep. At the end of of sleep run wait PID to see if the process finished. If it has not it will wait until that script has executed before continuing. You could also use pgrep to check to see if the process exist anymore, which would allow you to simply loop over the sleep command in a way such as (again, rusty from memory bash here):

Code:

while true; do
  sleep 60
  a=$(pgrep PID)
  if (( a == "1" )); then # 1 is the return value of pgrep when no processes match
      run_script_again
      Store_pid_again
  fi
done

That is just an example of course, but you should get the idea. That should prevent the process running several times at once while timing the process to run every 60 seconds. You could do something similar with cron, simply checking for the process before executing it via pgrep or some other method.

EDIT:

Wouldn't
<time fields> sleep 30; command
execute sleep 30; command every second, simply delaying the execution of the command but actually running the line every second?

Hi_This_is_Dev 04-28-2010 12:52 PM

Quote:

Wouldn't
<time fields> sleep 30; command
execute sleep 30; command every second, simply delaying the execution of the command but actually running the line every second?
Well, I think, when I checked it it was executing every minute instead.


All times are GMT -5. The time now is 08:05 AM.