Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 从shell脚本中杀死一组特定的进程_Linux_Sh - Fatal编程技术网

Linux 从shell脚本中杀死一组特定的进程

Linux 从shell脚本中杀死一组特定的进程,linux,sh,Linux,Sh,我有一个shell脚本,它运行另一个java.jar,该java.jar正在使用chromedriver运行一些自动化测试。 有时,退出时会留下5或6个chrome浏览器打开。 *我目前正在努力解决这个问题,但我需要一个临时的快速解决方案 我需要一种在不影响非脚本启动的chrome窗口的情况下终止这些进程的方法 我尝试了下面的一个,但它只会扼杀第一个进程 #!/bin/bash while true do openchrome=$(pgrep chrome) java -jar upload

我有一个shell脚本,它运行另一个java.jar,该java.jar正在使用chromedriver运行一些自动化测试。 有时,退出时会留下5或6个chrome浏览器打开。 *我目前正在努力解决这个问题,但我需要一个临时的快速解决方案

我需要一种在不影响非脚本启动的chrome窗口的情况下终止这些进程的方法

我尝试了下面的一个,但它只会扼杀第一个进程

#!/bin/bash
while true
do

openchrome=$(pgrep chrome)

java -jar uploadv2.jar

pgrep chrome | grep -v $openchrome |xargs -r  kill

不确定这是否有效,但我认为根据Stephen p的评论,这就是它的样子。这假设java程序只生成作为子进程的chrome进程,而不生成其他进程

#!/bin/bash

$openjava=""
while true
do 
    java -jar uploadv2.jar &
    $openjava="$openjava $!" # List of all java programs running
    # The $! gets the most recently spawned process id 

    for process in $openjava
    do 
        childProc="$childProc `pgrep -P $process`" #get chrome processes spawned by java
    done

    for tab in childProc 
    do
        kill $tab    #kill the child processes (chrome tabs) 
    done
done        

也许您可以获取java程序的进程ID(如果它是java生成的Chrome),然后使用
pkill
上的
-ppid
选项仅杀死您的程序作为父级的Chrome。