=================================================
This article for Khler original, reprint must ensure that this article complete and completely retain the original author information and this article original link
Author:heyuanhui
E-mail:khler@163.com
qq:23381103
Msn:pragmac@hotmail.com
=================================================
How do I keep the service running? How to ensure that the service can be automatically restarted even if it is dead? This problem is often encountered when writing service programs. In a Linux system, a powerful shell can handle such transactions flexibly.
The following shell uses a while-do loop to check if the loader process is running and, if not, to start, so that the process of crashing the dead is restarted in time.
Two points must be noted:
1, PS |grep a process must add its road strength, otherwise easily grep to the wrong result;
2, must use-V to remove the grep command itself from the result, otherwise the result is not empty.
Copy Code code as follows:
#!/bin/sh
#=====================
#YuanHui. He
#khler @163.com
#=====================
While:
Todo
echo "Current DIR is" $PWD
stillrunning=$ (ps-ef |grep "$PWD/loader" |grep-v "grep")
If ["$stillRunning"]; Then
echo "TWS service is already started by another way"
echo "Kill it and then startup by the This shell, the other wise this shell'll loop out this message annoyingly"
Kill-9 $pidof $PWD/loader
Else
echo "TWS service is not started"
echo "Starting service ..."
$PWD/loader
echo "TWS service was exited!"
Fi
Sleep 10
Done
If you start this shell and discover that the process already exists and that the process was started in a different way instead of the shell, it will continue to remind you to find the process, either by starting the service with this shell, or by killing the service that was started in a different way. That's what the above statement does:
Kill-9 $pidof $PWD/loader