How to use shell script to control the program?

I searched, but did not find what I was looking for. In a nutshell, I created a bash script to work in an endless while loop, sleeping and checking if the process is running. The only problem is that even if the process is running, it says that it is not, and opens another instance.

I know that I should check the process name, not the process identifier, since another process can enter and accept the identifier. However, all perl programs are called Perl5.10.0 on my system, and I intend to open multiple instances of the same perl program.

The following "if" always returns false, what am I doing wrong here?

while true; do

 if [ ps -p $pid ]; then
  echo "Program running fine"
  sleep 10

 else
  echo "Program being restarted\n"
  perl program_name.pl &
  sleep 5
  read -r pid < "${filename}_pid.txt"
 fi

done
+5
source share
5

. :

if ps -p $pid; then

test. ps:

if test ps -p $pid; then

, "- bash: [: -p: , ", .

+9

, , .

, ; script , .

-, , - , , ( ) - script ? , . , , . .

:

, , , OP OP, :

, , .

, PID script, "". , script, , ,

  • "" .
  • emacs, PID
  • script ,

script , , , PID . , script. - perl script, script , . :

while true ; do
    if perl program_name.pl ; then
         echo "program_name terminated normally, restarting"
    else
         echo "oops program_name died again, restarting"
    fi
done

, , : perl. script - (PID) . script , , , .

+7

, PID . while true ; do ... done script , , ,

  • ( PID)
  • , .

daemontools runit. . . : init , ( , PID , ).

+3

, , . , script, , . , script ? ( , ps -p $pid. ps -p $pid. test.)

:

  • cron "" script, , , , , , . PID . PID .

  • If the program you control provides a service on a specific port, make it an inetd service. Thus, it does not work at all until there is a request on this port. If you configured it correctly, it will stop when there is no need, and reboot it if necessary. Takes less resources and the OS will handle everything for you.

+1
source

That kills -0 $ pid. It returns success if there is a process with pid $ pid.

0
source

All Articles