Setting cron to run a script every second

Setting cron to run a script every second
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

explaination is bellow :

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.


(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"


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.