LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Run cron job in every 20 second (https://www.linuxquestions.org/questions/linux-newbie-8/run-cron-job-in-every-20-second-671521/)

ust 09-22-2008 03:24 AM

Run cron job in every 20 second
 
I added a cron job , but I found that even set * * * * * ~my_cronjob , it only only it at EVERY MINUTE , if I want it run at every 20 second , can advise what can i do ? thx

billymayday 09-22-2008 03:38 AM

Why don't you run 3 cronjobs, where the second sleeps for 20 seconds and the third for 40 seconds before doing whatever you want it to.

bathory 09-22-2008 04:51 AM

You can create a script that runs your command, sleeps for 20 seconds and so on. Something like:
Code:

#!/bin/sh
while true
do
/path/to/command
sleep 20
done


billymayday 09-22-2008 05:23 AM

Quote:

Originally Posted by bathory (Post 3288045)
You can create a script that runs your command, sleeps for 20 seconds and so on. Something like:
Code:

#!/bin/sh
while true
do
/path/to/command
sleep 20
done


Except that won't work if the command takes 5 seconds (assuming the 20 seconds needs to be from the start of invocation)

Poetics 09-22-2008 11:22 AM

What about a script that sits in the background spawning off child processes every 20 seconds?

If it's coded correctly the children should exit having completed and not hang your box. However, if there is an error, you're in for a world of hurt.

anomie 09-22-2008 11:38 AM

@ust: Can you explain what it is you want to run every 20 seconds?

trickykid 09-22-2008 03:39 PM

billymayday has the right approach if you really need to run a script/command every 20 seconds with cron. It would look something like this:

Code:

* * * * * /path/to/script
* * * * * /bin/sleep 20; /path/to/script
* * * * * /bin/sleep 40; /path/to/script

I recall poking this type of scenario here: http://www.linuxquestions.org/questi...80#post3256180

i92guboj 09-22-2008 04:08 PM

Quote:

Originally Posted by billymayday (Post 3288064)
Except that won't work if the command takes 5 seconds (assuming the 20 seconds needs to be from the start of invocation)

'course it will, if programmed wisely :p

Code:

#!/bin/bash
while true
do
  /path/to/command&
  pid=$!
  sleep 20
  kill -SIGKILL $pid
done

This will also abort the process if it hasn't ended after 20 seconds.

trickykid 09-22-2008 04:18 PM

Quote:

Originally Posted by i92guboj (Post 3288587)
'course it will, if programmed wisely :p

Code:

#!/bin/bash
while true
do
  /path/to/command&
  pid=$!
  sleep 20
  kill -SIGKILL $pid
done

This will also abort the process if it hasn't ended after 20 seconds.

Just to remind ust if they take this type of approach, don't put this script in as a cron job or you'll be starting a new one every minute that sleeps every 20 seconds.. etc. You'd soon have a gazillion of these spawned off and running.. ;)

billymayday 09-22-2008 04:23 PM

Quote:

Originally Posted by i92guboj (Post 3288587)
'course it will, if programmed wisely :p

Code:

#!/bin/bash
while true
do
  /path/to/command&
  pid=$!
  sleep 20
  kill -SIGKILL $pid
done

This will also abort the process if it hasn't ended after 20 seconds.

How do you figure that?

i92guboj 09-22-2008 04:38 PM

Quote:

Originally Posted by billymayday (Post 3288595)
How do you figure that?

I don't understand the question.

The only modifications I made to the script was these:
  1. I added an ampersand at the end of the command. That automatically starts it on the background, so the stdin doesn't lock and the script continues running.
  2. I capture the pid of the process.
  3. I added a line to kill it after sleeping for 20 seconds.

I don't know if that answer your question.

billymayday 09-22-2008 05:38 PM

No, that explains it.

kram2593 09-22-2008 11:04 PM

cron job every 20 seconds
 
Quote:

Originally Posted by i92guboj (Post 3288587)
'course it will, if programmed wisely :p

Code:

#!/bin/bash
while true
do
  /path/to/command&
  pid=$!
  sleep 20
  kill -SIGKILL $pid
done

This will also abort the process if it hasn't ended after 20 seconds.

This is the correct answer !! ( I think) all the questions that I had are covered at least

vrecalde 01-15-2015 01:01 PM

execute a script every five seconds, example
 
Thanks to your information i had execute a script every 5 sec.

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
*/1 * * * * root sleep 5; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 10; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 15; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 20; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 25; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 30; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 35; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 40; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root slepp 45; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 50; echo "script ejecutado $(date)" >> /tmp/salida.txt
*/1 * * * * root sleep 55; echo "script ejecutado $(date)" >> /tmp/salida.txt


All times are GMT -5. The time now is 01:19 AM.