Hi all,
I am very new to shell scripting.I have the requirement like
one program is there, if it is running leave like that only and if it is stopped it has to be restart and once again keep watching and it is stopped we a have to restart once agian.I want a shell script for this.Please help me in this.

This is an urgent one.Thanks in advance.

What distribution or operating system are you referring to? In linux the init.d files do exactly this by writing out PID files to see if the program is still alive when it re-executes.

Please provide more information.

What distribution or operating system are you referring to? In linux the init.d files do exactly this by writing out PID files to see if the program is still alive when it re-executes.

Please provide more information.

Hi i am working on unix environment.I have the requirement like.
If i have a program.c and it is running.Different signals comes into picture.Like below
The program may be like

#include <stdio.h>

int main()
{
   printf("Message 1\n");
   sleep(10);
}

The signals are,

switch(signum){
case SIGHUP: return "JVM hangup";
break;
case SIGINT: return "JVM interrupted";
break;
case SIGQUIT: return "JVM Quit";
break;
case SIGILL: return "Illegal instruction";
break;
case SIGABRT: return "JVM aborted";
break;
case SIGFPE: return "Floating point exception";
break;
case SIGKILL: return "JVM killed";
break;
case SIGBUS: return "Bus error";
break;
case SIGSEGV: return "Segmentation violation";
break;
case SIGSYS: return "Bad argument to system call";
break;
case SIGTERM: return "Software termination signal from kill";
break;
case SIGCHLD: return "Child process terminated or stopped";
break;
default: return "JVM abnormally terminated";
break;
}

If the above signals comes also the program has to run.
This is what I required.But i dont know how to code these signlas to trap.
I think I am clear from my end.If you didnt get my requirement please let me know.

i,
Thank you very much for your valuable reply.But i have some doubt.
what is pidfile.Where it is created.I am new to unix please clarify my doubt.

#!/bin/bash
pidfile=/var/run/sync.pid
PROGRAM="/home/test/vijay/vijay1"
$PROGRAM 
if [ -e $pidfile ]; then
pid=`cat $pidfile`
if kill -0 &>1 > /dev/null $pid; then
echo "Already running"
exit 1
else
rm $pidfile
fi
fi
echo $$ > $pidfile
 
#do your thing here

rm $pidfile

Thanks,
Vijay,

When you create a process it is assigned a 'process id' or PID. In the PID file you write out the process ID .. that way you can see if that process is still running so you dont start up a new instance every time. you can change the location of the pid file by changing the pidfile= line

Thank you very much Sknake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.