Prev: root can't ls(1) a directory: Permission denied
Next: how to automatically run a script as long as an input file is available
From: Kun Liu on 12 Mar 2010 04:58 Hi, I need to write a shell script (or perl script if possible) to do the following: Every day starting from 10:00am, the shell script should periodically check if an input file has been created by another process. If the file is not created yet, nothing happens. As long as the file has been created, the shell script will launch another program to analyze the file and then exits. If at 5:00pm, the input file is still missing, the shell script stops monitoring and exits. This process repeats on a daily basis. I was thinking about using "cron" to do this. But it seems that 'cron" does not allow me to specify the start and end time that the process should monitor the input file. Any suggestions? Thanks a lot. Kun
From: Janis Papanagnou on 12 Mar 2010 05:08 Kun Liu wrote: > Hi, > > I need to write a shell script (or perl script if possible) to do the > following: > > Every day starting from 10:00am, the shell script should periodically > check if an input file has been created by another process. If the > file is not created yet, nothing happens. As long as the file has > been created, the shell script will launch another program to analyze > the file and then exits. If at 5:00pm, the input file is still > missing, the shell script stops monitoring and exits. > > This process repeats on a daily basis. > > I was thinking about using "cron" to do this. But it seems that 'cron" > does not allow me to specify the start and end time that the process > should monitor the input file. > > Any suggestions? With cron you can defined the start time of your program every day. Your script program will implement a loop, in the loop a check for the file and conditionally an action, then do a sleep interval, and reiterate the loop until the end time has been reached, then exit the script program. Well, that sounds quite like your description. Just implement it in any POSIX shell. As commands use date(1), sleep(1), [ -f ... ] (the test operator for files) and a while/do/done shell loop with a conditional break. See man pages of the commands for the details how to call them. Janis > > Thanks a lot. > Kun
From: Kun Liu on 12 Mar 2010 13:12
On Mar 12, 5:50 am, Ben Bacarisse <ben.use...(a)bsb.me.uk> wrote: > Kun Liu <royl...(a)hotmail.com> writes: > > I need to write a shell script (or perl script if possible) to do the > > following: > > > Every day starting from 10:00am, the shell script should periodically > > check if an input file has been created by another process. If the > > file is not created yet, nothing happens. As long as the file has > > been created, the shell script will launch another program to analyze > > the file and then exits. If at 5:00pm, the input file is still > > missing, the shell script stops monitoring and exits. > > > This process repeats on a daily basis. > > > I was thinking about using "cron" to do this. But it seems that 'cron" > > does not allow me to specify the start and end time that the process > > should monitor the input file. > > It may be more helpful than you think: > > */5 10-17 * * * my-program > > runs my-program every 5 minutes between 10am and 5pm. If you have an > old cron that does not understand / for a step value and - for a range > you will have to write it out: > > 0,5,10,15,20,25,30,35,40,45,50,55 10,11,12,13,14,15,16,17 * * * ... > > If you literally want cron to start and then stop the program, you > need two entries: one that starts it at 10 and another that stops it > at 5pm. A common mechanism is for the script to put its process ID > into a file like /var/run/my-program.pid so that it can be killed with > a signal. > > -- > Ben. Hi Janis and Ben, Thank you very much. The information you provided is very helpful. -- Kun |