LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-16-2006, 11:33 PM   #1
Rinish
Member
 
Registered: Apr 2005
Location: Bangalore
Distribution: Redhat, CentOS, AIX
Posts: 93

Rep: Reputation: 15
Seting cron to run a script every second


HI All,
I wana set a cron to run a script every second. Please reply back if any one knows how to do this.

Thanks & regards,
 
Old 03-17-2006, 01:52 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

The minimum timeframe in cron is 1 minute, most cron daemons check every 30 seconds to see if anything needs to be done.

You don't tell what the script does, but isn't it possible to make a loop in the script itseelf (use sleep 1 to pause one second). Loop 59 times. This way you can strat the script every minute (or 5 minutes if you increase the loop.

Hope this helps.
 
Old 03-18-2006, 03:15 AM   #3
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
the reply describing a polling of 60 or 30 seconds by cron is contradicting,

based on the entries - cron entries
job list is constructed
and depending on the time-set for next job, timer is set to wake the cron
if cron entries are edited, job list is rebuild.

Check the following link,
http://www.linuxquestions.org/questi...d.php?t=422832
 
Old 03-18-2006, 05:37 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Sorry I was unclear.

In crontab (the one the user can edit) the smallest timeperiod is 1 minute. The crontab deamon, which checks the crontab file, runs every 30 seconds.

Hope this clears things up.
 
Old 03-18-2006, 05:42 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Still things are not clear.

The link that I had posted in the previous post had the discussion how cron is implemented.

2 concepts were proceeding:
a) Whether cron implemented polling of 30 or 60 seconds.
(or)
b) job list is constructed and based on the time the next job in the list to be executed, cron would be signalled accordingly.
When cronentry is edited job list is rebuild again.

Hope I have made myself clear.
 
Old 03-20-2006, 03:26 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
cron every second????

whatever you are trying to do - this is wrong.

you should tell people what you are trying to achieve not how you think
it should be done.

Last edited by bigearsbilly; 03-20-2006 at 03:29 AM.
 
Old 03-26-2006, 08:23 AM   #7
doc.nice
Member
 
Registered: Oct 2004
Location: Germany
Distribution: Debian
Posts: 274

Rep: Reputation: 34
(Copied from duplicate thread, to get them all in one...
Other thread was closed by moderator.)


maybe you could use this scritp running in background:

Code:

#!/bin/bash

###
### Sample background worker script
### for linuxquestions.org written
### by Florian Harbich (user doc.nice)
###
### Free for use or modification, even if
### useless in this variant...
###

TERMINATORFILE="/var/run/backgroundworker.ctl"
GOON=1
while [ $GOON ]; do
[ -f "$TERMINATORFILE" ] && GOON=0
# do your repeated stuff instead of logger syslog sample here
logger -t BGWorker -- "hi! I'm happy to tell you i'm still alive"
sleep 1
done
rm -f "$TERMINATORFILE"
 
Old 03-26-2006, 01:40 PM   #8
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
cron every second????

whatever you are trying to do - this is wrong.

you should tell people what you are trying to achieve not how you think
it should be done.
Bigearsbilly is right - I'd listen to him.

And even if you did find some clever, tricky way to get it to work - it's still probably going to be a poor solution to an ill-defined problem...

PS:
doc.nice is right, too. If you really want some action repeated every second, your best bet is to create some kind of "daemon" background process or service that loops 1x/second. doc.nice showed you an example in a shell script; you could easily do the same thing in any programming language.

Last edited by paulsm4; 03-26-2006 at 04:05 PM.
 
Old 10-23-2008, 11:13 AM   #9
rkane
Member
 
Registered: May 2003
Location: Ohio
Distribution: Slackware
Posts: 47

Rep: Reputation: 15
Since it was hard for me to find a solution, I thought I'd post here.
Here is how I figured out how to do it on 80 seconds.

*/4 * * * * /scripts/80_second_script
*/4 * * * * sleep 80; /scripts/80_second_script
*/4 * * * * sleep 160; /scripts/80_second_script

Line 1 runs at 0,240,480,etc...
Line 2 runs at 80,320,560, etc...
Line 3 runs at 160,400,640, etc...

Put that all together you get
0,80,160,240,320,400,480,560,640, etc...

You can do this with any number, but the more often the multiples of your number cross the multiples of 60 the less lines you have to write.

Hope this helps someone.
 
Old 10-23-2008, 11:27 AM   #10
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by rkane View Post
Since it was hard for me to find a solution, I thought I'd post here.
Here is how I figured out how to do it on 80 seconds.

*/4 * * * * /scripts/80_second_script
*/4 * * * * sleep 80; /scripts/80_second_script
*/4 * * * * sleep 160; /scripts/80_second_script

Line 1 runs at 0,240,480,etc...
Line 2 runs at 80,320,560, etc...
Line 3 runs at 160,400,640, etc...

Put that all together you get
0,80,160,240,320,400,480,560,640, etc...

You can do this with any number, but the more often the multiples of your number cross the multiples of 60 the less lines you have to write.

Hope this helps someone.
Wow....or you could have just put a "sleep 1" at the end of your script, and looped back around, until you killed the PID or CTRL-C'ed it.
 
Old 10-23-2008, 07:00 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Unless cron can create a new process that then does all its work and shuts down in less than 1 second, the processes will overrun each other.
They'd better be operating on separate and independent bits of data.!
I think the OP should tell us exactly why he'd want to do this.
I suspect that writing your own daemon that pauses for 1 second at the bottom of the processing loop is more appropriate.
If you need it to do its work unit in <1 sec, I'd suggest Perl or C. Shell is not going to be fast enough.
 
Old 10-23-2008, 08:39 PM   #12
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Maybe the OP has solved his problem during the past 2 1/2 years...

jlinkels
 
Old 09-22-2010, 09:30 AM   #13
Li0nHeart
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Even though this thread is 2 years old, it still comes up in a google search for this topic. I didn't like the solution, so I thought I'd post what I've come up with, for future google searches:

Purpose : run /usr/local/bin/scriptname every 30 seconds, where 30 is configurable


wrapper script (/usr/local/bin/wrapper)
tabs lost in posting
Quote:
#!/bin/bash
# Wrapper

# Initialisation
if [ "$1" == "" ]; then
exit 1
fi

interval=$1


next=$(date +%M | awk -v interval=$interval '{ print int($0 * 60 % interval) }')

if [ $next -gt 0 ]; then
next=$(echo $next | awk -v interval=$interval '{print interval - $0}')
fi


for i in $(seq $next $interval 59)
do
(sleep $i; /usr/local/bin/scriptname) &
done
crontab:
Quote:
*/1 * * * * /usr/local/bin/wrapper 30

Last edited by Li0nHeart; 09-23-2010 at 02:43 AM. Reason: changed %m to %M .. thanx.. good call.
 
Old 09-22-2010, 07:07 PM   #14
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,704

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Li0nHeart,
Welcome to LinuxQuestions.

I can maybe see the idea of the script but it doesn't work. %m is the variable for month so not sure what that calculation is actually trying to compute? Using the variable $i in your sleep statement for anything other then 30 seconds doesn't work. i.e. 20 second intervals the sequence would be x x+20 x+40.
 
Old 09-23-2010, 02:48 AM   #15
Li0nHeart
LQ Newbie
 
Registered: Sep 2010
Posts: 2

Rep: Reputation: 0
Except for the %m, which of course is kinda dumb, it works.

the value of next is the number of seconds since the last time the command ran.

If it's greater than zero, that means the next run is in (interval-next) seconds, so we calculate that
If it's zero, it runs now.

The for loop is next run to 59 seconds with 'interval' steps.

For instance, if interval is 7 (just for arguments sake, it's a too short interval, 30 or greater would start to make sense), the output of the for loop would be, depending on the moment in time:

1
8
15
22
29
36
43
50
57
So it would set backgroup sleep jobs for those numbers.

So 20 seconds would indeed be, x, x+20, x+40, where x would be 0, so 0, 20, 40
So a job every 20 seconds.

Now it's debatable if you would want to do this, but I've got a user asking to run a job every 30 seconds. It's not live yet, so not sure how the impact on the system will be.

Last edited by Li0nHeart; 09-23-2010 at 02:51 AM.
 
  


Reply



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
cannot get tu run a php script in cron alain Linux - General 6 02-06-2006 02:54 AM
FTP Script will not run from Cron grhansen Programming 12 01-13-2006 04:13 AM
Script creats temporary files... Cron won't run it spiffytech Linux - Software 5 01-11-2006 10:03 AM
run shell script on cron varunbihani Linux - Newbie 5 07-08-2005 01:50 AM
can't get cron to run my script. dr_zayus69 Linux - Software 6 04-12-2005 06:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:19 PM.

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