LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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,130

Rep: Reputation: 31
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
LQ 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
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,158
Blog Entries: 1

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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
LQ 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,181

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
Posts: 3,935
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
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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
 
1 members found this post helpful.
Old 09-22-2008, 04:08 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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
LQ 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,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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
LQ 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
 
Old 01-15-2015, 01:01 PM   #14
vrecalde
LQ Newbie
 
Registered: Dec 2014
Posts: 8

Rep: Reputation: Disabled
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
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:02 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration