Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.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/5/bash/16.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
终止屏幕但java进程未结束_Java_Bash_Gnu Screen_Kill Process - Fatal编程技术网

终止屏幕但java进程未结束

终止屏幕但java进程未结束,java,bash,gnu-screen,kill-process,Java,Bash,Gnu Screen,Kill Process,我正在运行一个脚本,它基本上运行一组服务器进行本地测试 这些JAR在不同的屏幕上运行,因为它们都需要独立地接受键盘输入。为此,我使用了屏幕 command1="java -jar $fullPath" screen -d -m -S "${screenName[$i]}" bash -c "$command1" 它工作得很好 然后我需要一种杀死所有服务器的方法 所以我写了一个这样的脚本 for session in $(screen -ls | grep -o "[0-9]*\.${scree

我正在运行一个脚本,它基本上运行一组服务器进行本地测试

这些JAR在不同的屏幕上运行,因为它们都需要独立地接受键盘输入。为此,我使用了屏幕

command1="java -jar $fullPath"
screen -d -m -S "${screenName[$i]}" bash -c "$command1"
它工作得很好

然后我需要一种杀死所有服务器的方法

所以我写了一个这样的脚本

for session in $(screen -ls | grep -o "[0-9]*\.${screenName[$i]}")
do
    screen -X -S "$session" quit
    echo "killing screen $session"
done
它工作得很好,因为屏幕被杀死了。 但当我第二次这么做时,java突然占用了我100%的CPU

在使用退出屏幕命令之前

在我使用退出屏幕命令之后

他们也会花很长时间通过gui使用强制退出进行杀戮

其他资料:

  • 服务器使用的是运行在一个java线程上的Jetty。然后另一个线程就坐在那里等待键盘输入
  • 显然,这是在mac上运行的,脚本在bash中,所以我想要一个适用于mac和linux的bash解决方案
  • 它们也使用Java7构建,但使用Java8运行
因为服务器接受键盘输入,所以发送到屏幕的所有命令都被服务器接收。 他们确实有退出的输入,但我不想信任服务器退出

因此,我的问题是:

  • 有没有办法让屏幕在终止时终止其中所有正在运行的进程?

  • 如果没有,是否有方法将ctrl-c发送到特定屏幕?

  • 如果没有,是否有办法在屏幕本身不运行命令的情况下查看特定屏幕的运行过程?(这样我就可以使用kill)


tl;dr当我关闭屏幕时,正在运行的进程开始使用我所有的cpu,并且不会终止。我想让它终止。

自己做了解决方案

简而言之,它会找到screen进程并找到所有java进程,然后查找其祖辈进程是screen的java进程

它的效率极低,因为它会在每个屏幕的阵列中循环。所以基本上是O(n^2),但数量很少,所以它对我有效

代码:

length=$(expr${#screenName[@]}-1)
#获取所有java进程及其父进程
#原因是屏幕上有两个进程,一个是java进程,另一个是父进程
#由于某些原因,我无法在mac中获取子进程,但我可以获取父进程
javaPs=()
javaGpPs=()
用于以$表示的javaId(pgrep java)
做
#回音
#echo$javaId
#echo$(ps-oppid=$javaId)
#echo$(ps-oppid=$(ps-oppid=$javaId))
javaPs+=($javaId)
javaGpPs+=($(ps-oppid=$(ps-oppid=$javaId)))
完成
echo“子进程随后是屏幕进程”
echo${javaPs[@]}
echo${javaGpPs[@]}
#获取数组中元素的索引
#搜索词后面紧跟着数组
#请注意,由于它是通过echo返回的,所以不能将任何调试语句添加到此函数中
搜索(){
局部i=1;
searchTerm=“${1}”
shift#移动参数
array=(“${@}”)#将其余参数作为数组(这是一个数组)获取
对于${array[@]};do中的str
如果[“$str”=“$searchTerm”];则
echo$((i-1))#应该引用正确的索引(0到某物)
返回
其他的
((i++)
fi
完成
回声“-1”
}

对于((i=0;iYou可以通过调用
killall-9java
杀死所有Java程序。好吧,好主意,我会用它作为最坏情况的解决方案,因为它会杀死我运行的任何其他Java程序……但它确实阻止我在应该工作的时候玩minecraft:P但希望有一个只杀死这些特定服务器的解决方案出现。)如果您找到一种方法来确定
屏幕
进程的进程id(或者在生成进程时更好地存储它们),您可能可以使用
ps
-ppid
选项来杀死
屏幕
进程生成的所有进程。我找到了一种方法来获取屏幕进程的id,但是命令
$(ps-ppid$pid)
表示
ps:Invalid process id:pid
与此问题完全相同,您是否找到它占用100%cpu的原因?
length=$(expr ${#screenName[@]} - 1)

# gets all of the java processes and their grand parents
# the reason is that the screen makes 2 processes one is the java process and the other is the parent process
# I can't grab a children in mac for some reason BUT i can grab the parent process
javaPs=()
javaGpPs=()
for javaId in $(pgrep java)
do
    #echo
    #echo $javaId
    #echo $(ps -o ppid= $javaId)
    #echo $(ps -o ppid= $(ps -o ppid= $javaId))
    javaPs+=($javaId)
    javaGpPs+=($(ps -o ppid= $(ps -o ppid= $javaId)))
done

echo "Child procressed followed by screen processes"
echo ${javaPs[@]}
echo ${javaGpPs[@]}
#gets the index of an element in an array
#search term is first followed by the array
#note that becuase it returns by echo you can not add any debug statements into this function
search() {
    local i=1;
    searchTerm="${1}"
    shift #moves over the argument looking
    array=("${@}") #grabs the rest of the args as an array (which is an array)
    for str in ${array[@]}; do
        if [ "$str" = "$searchTerm" ]; then
            echo $((i - 1)) #should reference the correct index (0 to something)
            return
        else
            ((i++))
        fi
    done
    echo "-1"
}

for (( i=0; i<=$length; i++ ))
do
    #looks to see if there are multiple screens with the same name
    for session in $(screen -ls | grep -o "[0-9]*\.${screenName[$i]}")
    do
        echo
        echo "killing screen $session"
        IFS='.' read -ra ADDR <<< "$session" #splits the id from the name
        pid=${ADDR[0]}
        screen -X -S "$session" quit # exit session

        # now we kill the still running java process (because it will not exist for some reason)
        itemIndex=$(echo $(search "${pid}" "${javaGpPs[@]}"))
        javaId=${javaPs[$itemIndex]}
        # the process that is being killed
        echo "killing java process"
        echo $(ps -p $javaId)
        kill -9 $javaId
        sleep 1
    done
done

echo
echo "All process should now be dead doing extra clean up now"
screen -wipe #remove all dead screens