LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 09-22-2008, 03:24 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,129

Rep: Reputation: 30
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
 
Old 09-22-2008, 03:38 AM   #2
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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.
 
Old 09-22-2008, 04:51 AM   #3
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,906

Rep: Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326
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
 
Old 09-22-2008, 05:23 AM   #4
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by bathory View Post
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)
 
Old 09-22-2008, 11:22 AM   #5
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,178

Rep: Reputation: 49
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.
 
Old 09-22-2008, 11:38 AM   #6
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
@ust: Can you explain what it is you want to run every 20 seconds?
 
Old 09-22-2008, 03:39 PM   #7
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
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
 
Old 09-22-2008, 04:08 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by billymayday View Post
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

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.
 
Old 09-22-2008, 04:18 PM   #9
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by i92guboj View Post
'course it will, if programmed wisely

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..
 
Old 09-22-2008, 04:23 PM   #10
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Quote:
Originally Posted by i92guboj View Post
'course it will, if programmed wisely

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?
 
Old 09-22-2008, 04:38 PM   #11
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
Quote:
Originally Posted by billymayday View Post
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.
 
Old 09-22-2008, 05:38 PM   #12
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
No, that explains it.
 
Old 09-22-2008, 11:04 PM   #13
kram2593
LQ Newbie
 
Registered: Sep 2008
Location: Carmel, NY USA
Posts: 5

Rep: Reputation: 0
Thumbs up cron job every 20 seconds

Quote:
Originally Posted by i92guboj View Post
'course it will, if programmed wisely

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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't get a cron job to run derzok Linux - General 10 12-16-2007 04:00 AM
script does not run in cron job kashyapvirgo Linux - General 8 03-20-2007 10:55 AM
how to run this cron job ashley75 Linux - General 5 05-24-2004 11:20 AM
Did my Cron job run? ryedunn Linux - Newbie 2 02-25-2004 08:59 AM
Cron job does not run brentos Linux - General 6 12-12-2003 02:37 PM


All times are GMT -5. The time now is 03:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration