keeping a process alive ?

Hello guys, I have one script running that I need to keep it running 24x7 so I'd like to know how can I implement a sort of monitoring process I mean if for some reason this process dies somehow it gets automatically started again.

Thanks.

You can create SMF service for that script. Then the SMF itself will monitor availability of that script, and will restart it if needed. Take a look at: BigAdmin Feature Article: Using Service Management Facility (SMF) in the Solaris 10 OS: A Quick Example

thanks for answering, I took a look at it and it sounds a lot complex, there is no simpler way ?

Thanks once again.

Yes. use crontab to schedule a 'check_it_is_running' script every x minutes. The check it script will restart the other script if it is no longer running.

Every 5 minutes

5 * * * * /path/to/myscript/check.sh 2&>1 >> /path/to/logs/check.log

Note: your script has to recreate your environment fully by sourcing /etc/profile and .profile
for the user. Bartus solution is definitely preferred.

In order to know, if a process is running or gone, I can suggest you two methods:
1) Use pidof <name of your program> and check its return value which should be any number as the pid of your process but should not be zero. So if you get zero as return value, means the process is gone.

2) You may use ls /proc/<pid_of_your_process> should be sucessful. Meaning check the value of $? immediately and the decide, if your process is running or gone.

Now write a parent script which loops for ever (like while (1) ...) and checks the process state everytime, if your process is found to be not running then invoke the process again and sleep for a reasonable time to allow the process up and running before your this parent script loops to check the status of your process for the next time.

Also invoke your process, from this parent script, using nohup <your_process_name> &, as you would be knowing to allow your process run in the background and asynchronously to this script.

Cheers!!!!!!!!!!!! :slight_smile:

man inittab

This is exactly what it's for. Why reinvent it?

Inittab is not really designed for non system applications. It also lacks most SMF features which is the way to go for Solaris 10 and newer.

Let's see, add this

abcd:3:respawn:/path/to/some/script

to /etc/inittab, or deal with the dependencies and complexities of SMF?

One takes 10 seconds, the other could take several days depending on how familar one is with SMF.

That's true but hopefully no Solaris service is following that path. That would be unmanageable.