linux - kill processes started in parallel -


i start app starting many processes, use script (bash) start it.

  • the filemane : start.sh , use sudo ./start.sh run
  • the script in start.sh is:

#!/bin/sh sudo p1 > p1.txt && p2 > p2.txt && p3 > p3.txt

to stop app use ctrl-c not processes stop. know find ps aux | less | grep ..., seems long , want know if there , easy wait stop processes.

update sorry instead (only 1 &)

sudo f1/p1.py > logs/p1.txt &  sudo f2/p2.sh > logs/p2.txt & sudo nodemon f3/p3.js > logs/p3.txt 

store pids, , set trap kill them when signaled or on exit:

#!/bin/bash #      ^^^^ - not /bin/sh, code uses arrays  pids=( )  # define cleanup function cleanup() {   pid in "${pids[@]}";     kill -0 "$pid" && kill "$pid" # kill process if it's still running   done }  # , set function run before exit, or when sigterm trap cleanup exit term  sudo f1/p1.py > logs/p1.txt & pids+=( "$!" ) sudo f2/p2.sh > logs/p2.txt & pids+=( "$!" ) sudo nodemon f3/p3.js > logs/p3.txt & pids+=( "$!" )  wait # sleep until background processes have exited, or trap fires 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -