Scheduling a script

Hi,

I have my script in below path in UNIX

/storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh

I want to schedule it to run every 30 secs.
Please let me know the steps to do it.

Thanks.

What shell are you using?

What operating system are you using?

What does /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh do?

How long does /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh run?

Do you want /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh to run, then sleep for 30 seconds, and run it again; or do you want to run /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh every 30 seconds even if each invocation runs for an hour?

Im using BASH.
my script completes in few minutes.

I want my script to run every 30 secs even if each invocation runs for an hour?

You could try something like:

#!/bin/bash
while [ 1 ]
do	/storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh &
	wait $! &
	sleep 30
done

Since you chose not to show us your script, we have no idea how many processes it runs nor whether or not it is designed to allow multiple copies of itself to run simultaneously without destroying each other's data. Depending on how many minutes few minutes is and how many processes each invocation of /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh starts, you could run out of process table slots allowed for your user ID. And, if the system you're running on has other users that want to do something, a script like the above will be put to sleep to allow other users (or other things you want to run) to run if you become a CPU hog or a disk hog, or a network hog, etc. and some of your sleep 30 commands may take (a little or a lot) more than 30 seconds to complete.

Depending on what operating system you're using and what your script does there may be utilities provided by your operating system that can watch files (individually, in groups, all files in a directory, or all files in a file hierarchy) that will be MUCH more efficient than running 2 * few copies of a process simultaneously.

My gut feeling tells me that you are not really wanting to have a new instance of the script started every 30 seconds regardless of how long it runs, but here you go:

Simply put two lines into your crontab:

* * * * *            /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh >/dev/null 2>&1
* * * * * sleep 30 ; /storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh >/dev/null 2>&1

I hope this helps.

bakunin

2 Likes

Hi bakunin,
I agree that running multiple copies of a script (that from its name) we assume watches for filesystem changes seems like a bad idea.

Hi prats_7678,
Note that cron also places limits on the number of cron , at , and batch jobs it will run simultaneously. If you run into this limit (and you aren't the only user on your system), you may have groups of irate users asking your sysadmin to kick you off of your system or to lower the priority of all processes that you run.

And, there is no guarantee that jobs scheduled by cron won't be delayed. If system load is high, cron won't start jobs until the load drops. If that happens, cron might end up starting several of your delayed jobs within milliseconds of each other.

We would much prefer that you explain to us what you are trying to do and give us details about the system you're using so we can help you find an efficient way to get the job done rather than keeping us in the dark and asking us to teach you how to be a resource hog.

2 Likes

Thanks for your help!!
It worked :slight_smile:

I'm not sure which suggested you're using, but I'm glad it is working for you.

If you used my suggestion, it has a bug that will eventually fill up your system's process table with zombies. To fix it, change the line in red:

#!/bin/bash
while [ 1 ]
do	/storage/sas_source/SDTM-Development/FileWatcher/filewatcher.sh &
	wait $! &
	sleep 30
done

to just be:

	wait &