How to restart a process in bash or kill it on command?

I have a script that will track the process, and if this process dies, it will update it. I want script tracking to also kill the process if it was told to make script tracking a sigmor (e.g..). In other words, if I kill the tracking script, it should also kill the process that it is tracking, and not respawn and exit again.

Combining several messages (which, in my opinion, are best practices, for example, do not use a PID file), I get the following:

#!/bin/bash

DESC="Foo Manager"
EXEC="python /myPath/bin/FooManager.pyc"

trap "BREAK=1;pkill -HUP -P $BASHPID;exit 0" SIGHUP SIGINT SIGTERM

until $EXEC
do
    echo "Server $DESC crashed with exit code $?.  Restarting..." >&2
    ((BREAK!=0)) && echo "Breaking" && exit 1
    sleep 1
done

So now, if I ran this script in one xterm. And then in another xterm I send a script something like:

kill -HUP <tracking_script_pid>  # Doesn't work.
kill -TERM <tracking_script_pid>  #Doesn't work.

script - . FooManager.pyc , SIGHUP SIGTERM. , , , , ?

.

+4
1

:

Bash , ​​, , . Bash wait, , ​​, , wait 128, .

- .

, , , Bash , , .

, . 128, , :

#!/bin/bash

desc="Foo Manager"
to_exec=( python "/myPath/bin/FooManager.pyc" )

trap 'trap_triggered=true' SIGHUP SIGINT SIGTERM

trap_triggered=false
while ! $trap_triggered; do
   "${to_exec[@]}" &
   job_pid=$!
   wait $job_pid
   job_ret=$?
   if [[ $job_ret = 0 ]]; then
      echo >&2 "Job ended gracefully with no errors... quitting..."
      break
   elif ! $trap_triggered; then
      echo >&2 "Server $desc crashed with exit code $job_ret. Restarting..."
   else
      printf >&2 "Received fatal signal... "
      if kill -0 $job_pid >&/dev/null; then
          printf >&2 "killing job $job_pid... "
          kill $job_pid
          wait $job_pid
      fi
      printf >&2 "quitting...\n"
   fi
done

.

  • , : Bash .
  • , . , , , , . . ( , .)
+3

Source: https://habr.com/ru/post/1568650/


All Articles