为什么 pkill return -9?
Why pkill return -9?
我想在python3中运行命令pkill -9 -f 'java.*7104'
使用subprocess.run
,但我不知道为什么returncode=-9
,谁能解释一下?
>>> subprocess.run("pkill -9 -f 'java.*7104'", shell=True)
CompletedProcess(args="pkill -9 -f 'java.*7104'", returncode=-9)
当我运行在bashshell中执行相同的命令时,return代码为0:
$ pkill -9 -f 'java.*7104'
$ echo $?
0
运行 命令在 bash shell:
root@w-test01:/home/renyuntao# ps -ef | grep -E 'java.*7104' | grep -v grep
root 127147 1 99 11:37 pts/1 00:00:13 java -Djava.security.egd=file:/dev/./urandom -Xmx10240m -jar ./aichemy.jar --server.port=7104 --spring.profiles.active=test01_use
root@w-test01:/home/renyuntao# pkill -9 -f 'java.*7104'
root@w-test01:/home/renyuntao# echo $?
0
运行 命令使用 subprocess.run
:
root@w-test01:/home/renyuntao# ps -ef | grep -E 'java.*7104' | grep -v grep
root 128644 1 99 11:38 pts/1 00:00:22 java -Djava.security.egd=file:/dev/./urandom -Xmx10240m -jar ./aichemy.jar --server.port=7104 --spring.profiles.active=test01_use
root@w-test01:/home/renyuntao# python3
Python 3.5.2 (default, Apr 16 2020, 17:47:17)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run("pkill -9 -f 'java.*7104'", shell=True)
CompletedProcess(args="pkill -9 -f 'java.*7104'", returncode=-9)
Joseph Sible-Reinstate Monica answer the question here:
When you use subprocess.run
with shell=True
, Python starts a shell
process that in turn starts your process. This means that it executes
sh -c "pkill -9 -f 'java.*7104'"
. The pkill
process only has
special protections to avoid matching itself, not to avoid matching
its parent process. You need to adjust your regex to not match itself.
Putting a ^
before java
is probably the easiest way to do that,
like this:
subprocess.run("pkill -9 -f '^java.*7104'", shell=True)
我想在python3中运行命令pkill -9 -f 'java.*7104'
使用subprocess.run
,但我不知道为什么returncode=-9
,谁能解释一下?
>>> subprocess.run("pkill -9 -f 'java.*7104'", shell=True)
CompletedProcess(args="pkill -9 -f 'java.*7104'", returncode=-9)
当我运行在bashshell中执行相同的命令时,return代码为0:
$ pkill -9 -f 'java.*7104'
$ echo $?
0
运行 命令在 bash shell:
root@w-test01:/home/renyuntao# ps -ef | grep -E 'java.*7104' | grep -v grep
root 127147 1 99 11:37 pts/1 00:00:13 java -Djava.security.egd=file:/dev/./urandom -Xmx10240m -jar ./aichemy.jar --server.port=7104 --spring.profiles.active=test01_use
root@w-test01:/home/renyuntao# pkill -9 -f 'java.*7104'
root@w-test01:/home/renyuntao# echo $?
0
运行 命令使用 subprocess.run
:
root@w-test01:/home/renyuntao# ps -ef | grep -E 'java.*7104' | grep -v grep
root 128644 1 99 11:38 pts/1 00:00:22 java -Djava.security.egd=file:/dev/./urandom -Xmx10240m -jar ./aichemy.jar --server.port=7104 --spring.profiles.active=test01_use
root@w-test01:/home/renyuntao# python3
Python 3.5.2 (default, Apr 16 2020, 17:47:17)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run("pkill -9 -f 'java.*7104'", shell=True)
CompletedProcess(args="pkill -9 -f 'java.*7104'", returncode=-9)
Joseph Sible-Reinstate Monica answer the question here:
When you use
subprocess.run
withshell=True
, Python starts a shell process that in turn starts your process. This means that it executessh -c "pkill -9 -f 'java.*7104'"
. Thepkill
process only has special protections to avoid matching itself, not to avoid matching its parent process. You need to adjust your regex to not match itself. Putting a^
beforejava
is probably the easiest way to do that, like this:subprocess.run("pkill -9 -f '^java.*7104'", shell=True)