System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

execute cronjob @ 10 second intervals ?

 
SOLVED
Go to solution
Jerry L. Sims
Frequent Advisor

execute cronjob @ 10 second intervals ?

Hello All,

*********************************
HP-UX B.11.11 U 9000/800/N4000-75
*********************************
I'm trying to find a way to execute a cronjob
with 10 seconds intervals ? I know it can be
executed at minute intervals.

The (2) commands I have in a script gather information related to "free memory & cpu info" are as follows:
1)/usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'

2)/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'

Please advised if this is possible, as always
thanks.
5 REPLIES 5
Sundar_7
Honored Contributor
Solution

Re: execute cronjob @ 10 second intervals ?

Hi,

I dont think it is possible with cron.

Alternate way is to write a script

#!/usr/bin/sh
while true
do
/usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }' >> /tmp/vmstat1.out
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}' >> /tmp/vmstat2.out
sleep 10
done

run the script with nohup and in the backup

# nohup /usr/local/bin/
Sundar
Learn What to do ,How to do and more importantly When to do ?
Mel Burslan
Honored Contributor

Re: execute cronjob @ 10 second intervals ?

crons granularity is in minutes. if you want to run something more frequent than that, you will need to build the functionality yourself.

simplest is to use "sleep n" command where machine will wait doing nothing in a a script for n-seconds when it hits this command, but it does not actually make running your command every 10 seconds. it will be 10 seconds between two command executions plus how ever many seconds the command needs to run.

to accomplish running your commands every 10 seconds you can use a construct like this :


while true
do

chkstr=`date +%S` # will give you the seconds from the date string

case "$chkstr" in

00) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

10) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

20) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

30) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

40) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

50) /usr/bin/vmstat 1 2 | tail -1 | awk '{printf "%d%s\n", ($5*4)/1024, "MB" }'
/usr/bin/vmstat -n 1 1 | tail -9 | awk '{print $1,$2,$3}'
break ;;

*) sleep 1
break ;;

esac
done

Hope this helps
________________________________
UNIX because I majored in cryptology...
Sundar_7
Honored Contributor

Re: execute cronjob @ 10 second intervals ?

You can rather simplify the construct as follows

while true
do
SECS=$(date +%S)
case "$SECS" in
[0-5]0)
sleep 1
;;
*) continue ;;
esac
done

I am not sure if a loop will executed only once in a second !! , so I included a sleep right after the case statement and not in the default section.
Learn What to do ,How to do and more importantly When to do ?
Sundar_7
Honored Contributor

Re: execute cronjob @ 10 second intervals ?

OK. I just realized I was testing my construct with some simple echo commands and interestingly my loop executed more than once in a second :-)

Since you have vmstat commands, you dont need a sleep in the case statements and can aswell have it in the default section.
Learn What to do ,How to do and more importantly When to do ?
Rory R Hammond
Trusted Contributor

Re: execute cronjob @ 10 second intervals ?


Looks like what you really want is realtime vmstat information.

You could do what you want by changing your method.

Let say you want 10 second data between 8 and 5 daily or 9 hours of 10 second data during prime time.

kick off you cron shellb at 08:00

The shell would look something like:


/usr/bin/vmstat 10 3240 |awk '($0 !~ /p/) {printf "%d%s\n", ($5*4)/1024, "MB" }' >> logfile

the 3240 is how many 10 second intervals in 9 hours. You cauld tail or look at the logfile. and get the most current value.
Also this will give you less overhead of trying to start a cron job a bunch of time.
Why sleep 10 when vmstat has the parm
the $0 !~ /p/ is a way to remove the header
with thought you probably think of something better.

Rory
There are a 100 ways to do things and 97 of them are right