ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
(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"
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.