1752795 Members
6144 Online
108789 Solutions
New Discussion юеВ

Re: Run cron job

 
ivy1234
Frequent Advisor

Run cron job

I would like to have run a script 5 times per 1 minute , I know crontab -e can edit the cronjob but the time interval at least 1 minute per job , can advise what can i do if I want run the script 5 times per every minute ? thx
11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: Run cron job

Inside your crontab script you can just loop 5 times. And if your script takes no time, you can put a "sleep 12" there.
ivy1234
Frequent Advisor

Re: Run cron job

It works , thanks.

I have another question .

Now , if I don't want to run it as a cron job ( because I don't want to change existing cron setting ), I just want to run a looping script on shell - this looping script is to run a command on every 20 seconds .


Can advise what can i do ? thx
Jose Mosquera
Honored Contributor

Re: Run cron job

Hi,

Then you need a loop process with a sleep interval of 20 secs (sleep 20).

Please remember that you should need ensure this process continuity without depend of current work session is closed.

#nohup yourloopscript.sh &

Detailed info about nohup command:
#man nohup

Character "&" executes your script in backgroud mode and "nohup" executes command with hangups and quits ignored.

It's highly recommended that you take note about the Process ID (PID) generated by system when you executes the script in background mode. The PID is the key to kill your script execution on backgroud mode.

Rgds.
Larry Klasmier
Honored Contributor

Re: Run cron job

Jose's advice is sound, you could also you the at command, this would give you the flexibility of starting your script at some specific time in the future.
ivy1234
Frequent Advisor

Re: Run cron job

thx reply ,

I have tested it , it works .

you recommend to take note the PID , I just try it in a testing server and without write down the PID , I can't find it back ..

can advise if it is really can not find the PID back , it needs to reboot the server if I want to stop the process ? thx
Jose Mosquera
Honored Contributor

Re: Run cron job

Hi,

Please execute this to find your script PID:
#ps -ef|grep |grep -v grep

And please send us the command output.

Rgds.
Jose Mosquera
Honored Contributor

Re: Run cron job

Well, you can register the script PID. Includes inside of the script, and before of loop, this command entry:
echo $$ > /tmp/.pid

echo $$ shows your current PID, then you redirect this command output to a plain text placed conveniently and with a mnemonic name.

Once catched this number (PID), you have the key to kill the process.
#kill -9 `cat /tmp/.pid`

Rgds.
Jose Mosquera
Honored Contributor

Re: Run cron job

When you get the command output:
#ps -ef|grep |grep -v grep

Look the second field (must be numeric and integer), this is the PID associated at your current script execution, then you should kill this PID:
#kill -9

If you try again, the command output do not show nothing:
#grep -ef|grep |grep -v grep

This mean that your script is killed.

After this, improve your script including the syntax explained previously.

Rgds.
Dennis Handly
Acclaimed Contributor

Re: Run cron job

>you recommend to take note the PID, I can't find it back

This should be trivial to find it if you have a unique name and/or arguments for your script.
UNIX95=EXTENDED_PS ps -C script-name

Note: This is simpler than Jose's ps/grep/grep pipeline.

You could also have your looping script check for the presence or absence of a sentinel file and stop on that.

>Jose: you have the key to kill the process.
#kill -9 `cat /tmp/.pid`

Better to eliminate that evil cat:
kill $(< /tmp/.pid)

(And only use -9 as last resort.)