scripting running every minute

Hi Experts,

below similar thread i posted earlier. Although i wish to know ur suggestion newly.

I want to run a script every 1 minute. I tried with Crontab. But the problem is cron send every 1 mins invertal- mail to the user mailbox.

meanwhile, some expert telling me it is not wise to run short interval in crontab.

What could be best way to run this type of short interval without getting any problem in systm.

//purple

If you want crontab to run without sending email, make the command not generate any output. Many commands have a -q or --quiet switch to make them not generate output. Otherwise you can redirect the output to a file or /dev/null.

some_command arg1 arg2 >/dev/null 2>/dev/null

But instead of using cron, if you are willing to accept sloppy timing, you can just keep one script running constantly and put it to sleep for a minute.

#!/bin/bash
while true; do
    some_command arg1 arg2
    sleep 60
done

This assumes you have it running in a dedicated shell and you stay logged in. But if you want to log out and let run, you can use screen (install the screen package if you don't have it--it's quite valuable).

There's no need for sloppy timing:

while :
do
    sleep 60 & pid=$!
    some_command arg1 arg2
    wait $pid
done

There's no need for screen. Use nohup and put it in the background.

cfajohnson ,
what's a 'sloppy timing' in the previous solution?

It executes the command every 60 seconds plus the time it takes to execute the command.

sorry, I must be thick-sculled - I'm having hard time parsing this sentence.
Chris, could you elaborate a little bit more and why your other suggestion is any better.
In 2 words or less.....

The command is initiated 60 seconds AFTER it last ran. If it requires 12 seconds to run, it will, in essence, run every 72 seconds.

while :
do
  : command takes XX seconds to execute
  sleep 60
done

The command will be run every 60 + XX seconds.

while :
do
  sleep 60 & pid=$!  ## sleep runs in the background
  : command takes XX seconds to execute
  wait $pid          ## wait for sleep to exit
done

So long as the command takes less than 60 seconds, it will be run exactly every 60 seconds.

my bad - you're absolutely right. Somehow I thought that the original 'some_command' was being spawned in the background as well. Sorry - too much egg-nock I reckon.

But at the same time....

while :
do
    sleep 60 & pid=$!
    some_command arg1 arg2
    wait $pid
done

if 'some_command' takes longer than 60 sec to run, this implementation is not the same as it would have been if cron-ed.
But if we put the 'some_command' in the background, it would be the same.
Not sure what the OP's intentions are though.

thanks for pointing to the obvious!

Cool! Cfajohnson, you taught me two new things here. However,

While I am pleased to learn about the nohup command, I still contend that the screen command is extremely valuable. If the command being executed spits out anything interesting, you may not want it emailed to you every minute, but if you let it go to the screen, you can view it from any computer you can ssh from.

I've used screen a couple times to start a bit-torrent download of a large .iso file on my home computer while I was at work. It's nice to be able to leave it running in the terminal while the terminal is floating unattached and then to be able to reattach it to take a look from any computer at home or work.

I could completely attach and detach a session as I wish with a 'screen' and that's not possible with nohup in background

Why? I don't quite understand that.

lets take 'some_command' takes 120 secs to run ( > 60 ) and in this case ( sample posted in the thread ), wait - waiting for $! will be activated and once again its back in the 'while' loop

same is the case for a cron-d
it doesn't check whether previous instance instantiated has completed or not, its job is to spawn instance every 'n'th minute.

end of 1st minute - one instance will be running
end of 2nd minute - first instance will be terminated
                            second instance started
end of 3rd minute - second instance still running
                           third instance spawned

and so on ...

why do you think there is a difference ?

first of all i would like congratulate for participating this thread and made your own valuble opinoin. I am really impressed and heading to get a proper solution.

I just want to know in the below code- pid=$i --> what exactly means by that? I will then implement it in my systme.

while :
do
    sleep 60 & pid=$!
    some_command arg1 arg2
    wait $pid
done

as the above code work as same like cron-d means if som_command take more than 60 second to execute meanwhile whithin 60 seconds some_command will again execute.

dear experts,

i run the below whileloop script--
$whileloop.sh &

#!/usr/bin/bash --> i did Not use this noe as header

while :
do
  sleep 60 & pid=$!
 /export/home/scripts/finding_txt_TXT_files_and_execute.sh
  wait $pid
done

for time being i now wanted to stop the whileloop.sh procss. I tried get the process id to kill it. becuase every 60 seconds it comes live...

i tried ps -ef | grep whileloop but did not find. Is there any other command to find the background process.

Its not pid = $i

its pid = $!

which means assign the process id of the 'last' run background process to pid which is then used by wait call for the return of sleep command initiated

you should be able to find that with the process name with which it was spawned from shell.

Background jobs can be identified by issuing jobs from the terminal where they were initiated

and use

 kill %%

or

 kill %1

or

 kill %2

as appropriate