给进程的variables或名称杀死这个实例的每个进程,但只有给定的名称(variables)?

我有一个程序(在这种情况下是node.js进程)运行许多进程。 有时我需要运行几个(例如10个nodejs进程),我用Makefile启动它们。 我希望能够在我的Makefile中使用一些bash命令在需要时closures这10个进程,但是我不想杀死其他node.js正在运行的进程。 所以我可以使用pkill node但它会杀死每个节点进程,我怎么能给这个10进程的一些名称或一些variables,杀死只有他们杀死-9或pkill?

您可以将subprocess的PID存储在一个文件中,并在稍后使用它来杀死它们。 sleepsubprocess的示例:

 $ cat Makefile all: start-1 start-2 start-3 start-%: sleep 100 & echo "$$!" >> pids.txt kill: kill -9 $$( cat pids.txt ); rm -f pids.txt $ make sleep 100 & echo "$!" >> pids.txt sleep 100 & echo "$!" >> pids.txt sleep 100 & echo "$!" >> pids.txt $ ps PID TTY TIME CMD 30331 ttys000 0:00.49 -bash 49812 ttys000 0:00.00 sleep 100 49814 ttys000 0:00.00 sleep 100 49816 ttys000 0:00.00 sleep 100 $ make kill kill -9 $( cat pids.txt ); rm -f pids.txt $ ps PID TTY TIME CMD 30331 ttys000 0:00.50 -bash 

注意:如果你使用并行,你应该注意pids.txt访问的竞争条件。

您可以尝试通过那里PID( 进程ID )来杀死进程:例如:

 # ps -ax | grep nginx 22546 ? Ss 0:00 nginx: master process /usr/sbin/nginx 22953 pts/2 S+ 0:00 grep nginx 29419 ? Ss 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf 29420 ? S 1:59 nginx: worker process 29421 ? S 1:54 nginx: worker process 29422 ? S 1:56 nginx: worker process 29423 ? S 1:49 nginx: worker process 29425 ? S 0:09 nginx: cache manager process 30796 ? S 1:49 nginx: worker process 

然后你可以杀死进程:

 kill 22546; kill 22953; kill ... 

您也可以通过以下方式捕获PID:

 # ps -ax | grep nginx | cut -d' ' -f1 | 22546 24582 29419 29420 29421 29422 29423 29425 30796 

更新:

你可以把这个PID写到一个文件中,并把它们拉回来,就像这样:

 pids: echo ps -ax | grep nginx | cut -d' ' -f1 | > PIDs.txt \ FILE="/location/of/PIDs.txt" \ old_IFS=$IFS \ IFS=$'\n' \ lines=($(cat FILE)) \ IFS=$old_IFS \ PID=$(echo {line[4]}) \ kill $PID 
Interesting Posts