Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to write a shell script that never dies i.e, Shell script will repeatedly check whether Database is alive or not for every five seconds. The problem is At times the shell script stops abnormally / killed by any one.

I want a script that will call itself even if it stopped / killed Is there any way to do that ???

share|improve this question
I think you should use a supervisor mechanism to monitor your script (via another script or a Unix process) and check if the script died then rerun it! –  coffeMug Oct 24 '13 at 15:07
@coffeMug using another script will again add overload to monitor that script . what do u mean by Unix process ?? i don't have idea about that .. –  Ram Oct 24 '13 at 15:11
Power outage lengthy enough makes every script die at last. :-P –  poige Oct 24 '13 at 15:33
@Ram neither do I! :P I think you can also define a cron job to start the script in an interval if the script is died. –  coffeMug Oct 24 '13 at 15:36
This might help:stackoverflow.com/questions/2354019/monitor-a-process –  coffeMug Oct 24 '13 at 15:40

4 Answers 4

up vote 8 down vote accepted

Use a crontab job to call your script.

crontab -e

Add the following line

* * * * * /path/to/your/script

This wil execute your script every minute. You can check here how to customize for other execution times. Crontab man page

Sorry that I missed the requirement to run every 5 secs. But like Jonnhy mentioned in the comments, you can have the cronjob verifying if the script is running or not.

Script for crontab:

ps -ef|grep -v grep |grep script_name
if [ $? != 0 ]; then

In your script:

while true; do
    #perform the test        
    sleep 5

Or if you are using a linux that is still based on init scripts, you can perhaps a entry to /etc/inittab


Although I'm not really sure if it is a best practice to have this in inittab.

If your system is already with systemd, there is also a way to perform this, but I don't have experience with it to put it here.

share|improve this answer
+1 on this. There's no way to trap a kill -9 so getting the script name out of the process table when its not needed is pretty much the only other way to minimize the risk that someone will ever try to kill it (which is what the OP is asking about). –  Joel Davis Oct 24 '13 at 16:01
Since he wants the script to run every 5 seconds, Cron won't run it often enough. But he could invoke the script every minute and have it check to see if it's already running and exit immediately if it is. That way the script would only be dead for at most a minute before cron starts it up again. –  Johnny Oct 24 '13 at 16:46
The crontab man page mentioned by @BitsOfNix lists the basic usage for the crontab command but the customizing options can be found by running man 5 crontab or by navigating here. –  Timothy Martin Oct 24 '13 at 18:25
@Johnny thanks, I amended the answer. –  BitsOfNix Oct 24 '13 at 18:36
@BitsOfNix Thanks . –  Ram Nov 4 '13 at 2:20

Rather than using a script with an endless loop, use a script that performs the task once but first sleeps for 5 seconds then calls itself in the background:

sleep 5
$0 &
# do whatever

That presumes the script is in $PATH, since it is invoked with just the name ($0).

If the script dies/is killed during "do whatever", the next iteration will still run.

share|improve this answer
If the "do whatever" task hangs, this runs the risk of running out of processes or memory on the machine. And if the script is killed while it's sleeping, it won't have a chance to invoke itself again so it will stop running. –  Johnny Oct 24 '13 at 16:43
@Johnny I completely agree; this is really a sketch of a tactic that could be used if there is a specific problem whereby the script is dying in "do whatever". That's an assumption that includes the OP being a somewhat poor or lazy communicator, meaning, if this isn't applicable, s/he'll realize that, but if it is, s/he might not have thought about things this way and it is an option. –  goldilocks Oct 24 '13 at 18:16

If you have root privileges you can put your script into either /etc/inittab or /etc/init/script.conf depending on which your system uses. Use the entries for /sbin/getty as an example of a process that is always restarted when it dies.

share|improve this answer
The inittab suggestion is a feasible one for SysV style systems that use it, but /etc/init/script.conf is NOT equivalent and will not work in the way you suggest. –  goldilocks Oct 24 '13 at 18:40

As long as the process isn't dying via SIGKILL, you can use trap, e.g.:



while ((1)); do
    echo $count
    sleep 2;

You can't Ctrl-C (SIGTERM) that. If you want it to stop, you need to use kill -9 [pid].

share|improve this answer
SIGQUIT (^) could also be sent to terminate instead of using the shouldn't-be-used SIGKILL. –  Arcege Oct 24 '13 at 20:03
@Arcege Are you sure? Try it ;O –  goldilocks Oct 24 '13 at 20:10
I didn't notice the bash shebang; I only use Bourne semantics myself and never noticed the difference with trap in Bash. –  Arcege Oct 25 '13 at 1:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.