How do I keep the service running? How can I ensure that the service is automatically restarted even if it is hung up? This is often a problem when you write a service program. In a Linux system, a powerful shell can handle such transactions with great flexibility.
The following shell through a while-do loop, with Ps-ef|grep to check whether the loader process is running, if not run, then start, so that the crash hangs the process of re-started in time.
Two points must be noted:
1, PS |grep a process must add its road strength, or easily grep to the wrong result;
2, you must use-V to remove the grep command itself from the results, otherwise the result is not empty.
Copy the Code code as follows:
#!/bin/sh
While:
Do
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"
echo "Kill it and then the startup by this shell, and the other wise this shell would 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 is started in another way instead of the shell, it will continue to alert you to the process, either by starting the service only with this shell, or by killing a service that is started otherwise. This is how the above statement is done:
Kill-9 $pidof $PWD/loader
A shell script that automatically restarts after a Linux monitoring process crashes and hangs