# Scheduling a script

**URL:** <https://community.unix.com/t/scheduling-a-script/362037>\
**Category:** UNIX for Beginners Q & A\
**Created:** [August 18, 2016, 6:02pm UTC](https://community.unix.com/t/scheduling-a-script/362037 "2016-08-18T18:02:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![prats\_7678](https://community.unix.com/letter_avatar/prats_7678/32/5_5575768a8748004e209b776fc1b2916d.png) [@prats\_7678](https://community.unix.com/u/prats_7678)\
**Post date:** [August 18, 2016, 6:02pm UTC](https://community.unix.com/t/scheduling-a-script/362037/1 "2016-08-18T18:02:37Z")

</div>

Hi,

I have my script in below path in UNIX

```nohighlight
/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.

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 18, 2016, 6:22pm UTC](https://community.unix.com/t/scheduling-a-script/362037/2 "2016-08-18T18:22:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![prats\_7678](https://community.unix.com/letter_avatar/prats_7678/32/5_5575768a8748004e209b776fc1b2916d.png) [@prats\_7678](https://community.unix.com/u/prats_7678)\
**Post date:** [August 18, 2016, 6:25pm UTC](https://community.unix.com/t/scheduling-a-script/362037/3 "2016-08-18T18:25:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 18, 2016, 6:57pm UTC](https://community.unix.com/t/scheduling-a-script/362037/4 "2016-08-18T18:57:23Z")

</div>

You could try something like:

```nohighlight
#!/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.

---

<div class="post-metadata">

**Author:** ![bakunin](https://community.unix.com/letter_avatar/bakunin/32/5_5575768a8748004e209b776fc1b2916d.png) [@bakunin](https://community.unix.com/u/bakunin)\
**Post date:** [August 18, 2016, 7:22pm UTC](https://community.unix.com/t/scheduling-a-script/362037/5 "2016-08-18T19:22:20Z")

</div>

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:

```nohighlight
* * * * * /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

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 18, 2016, 7:41pm UTC](https://community.unix.com/t/scheduling-a-script/362037/6 "2016-08-18T19:41:21Z")

</div>

> [@bakunin](#):
>
> 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:
> 
> ```plaintext
> * * * * * /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

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.

---

<div class="post-metadata">

**Author:** ![prats\_7678](https://community.unix.com/letter_avatar/prats_7678/32/5_5575768a8748004e209b776fc1b2916d.png) [@prats\_7678](https://community.unix.com/u/prats_7678)\
**Post date:** [August 18, 2016, 7:41pm UTC](https://community.unix.com/t/scheduling-a-script/362037/7 "2016-08-18T19:41:51Z")

</div>

Thanks for your help!!  
It worked 🙂

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 18, 2016, 10:03pm UTC](https://community.unix.com/t/scheduling-a-script/362037/8 "2016-08-18T22:03:18Z")

</div>

> [@prats\_7678](#):
>
> Thanks for your help!!  
> It worked 🙂

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:

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

```

to just be:

```plaintext
	wait &

```
