仅当程序拒绝以软方式终止时,如何通过硬方式终止程序?
How to kill a program by the hard way, only if it refuses to terminate the soft way?
如何通过 bash 以软方式退出程序,如果不成功,则以硬方式退出程序。
知道如何通过bash打开程序以及如何获取程序的PID。以下示例打开编辑器 XED:
固定 & PID=$!
已知如何回显打开的程序的 PID:
回显 $PID
12345 # PID 的示例输出
已知如何尝试以软方式关闭程序以及如何通过 PID select 程序:
kill -15 $PID # select一个程序通过PID和托盘以软方式杀死它
已知如何以硬方式关闭程序以及如何通过 PID select 程序:
kill -9 $PID # select一个程序通过PID硬杀
把我所有的东西放在一起,还缺少一些东西:
xed & PID=$! # open the program xed and get the pid
echo PID of programm: $PID
sleep 5 # lets run the xed 5 seconds
kill -15 $PID # select the xed by PID and try to kill it on soft way
# check based on variant 1, 2, 3 or other, the xed was sucessfull killed on soft way or not
# if the xed still running, kill it on follow way
kill -9 $PID # select the xed by PID and kill it on hard way
#!/bin/bash
xed & PID=$! # Start XED and get PID
sleep 3
# kill -15 $PID # Soft Kill, activate or deactivate for testing
echo
echo
sleep 3
if ps -p $PID
then
echo "The PID/program didn't be killed successful by Soft Kill"
kill -9 $PID # Hard Kill
else
echo "PID/program are already closed by Soft Kill"
fi
echo
echo
您仍然可以通过以下方式运行获取有关进程的信息:
xed & xedPid=$! # Start of editor XED and asking for PID
[1] 12345
kill -15 $PID
# getting the follow, if the XED was killed successfull soft before:
kill -15 $PID
bash: kill: (12345) - Kein passender Prozess gefunden
**[1]+ Beendet xed**
您可以获取有关进程的信息是否仍然运行,也可以遵循以下方式:
xed & xedPid=$! # Start of editor XED and asking for PID
[1] 12345
# Output if xed are running
kill -0 $PID
kill: Aufruf: kill [-s Signalname | -n Signalnummer | -Signalname] pid | jobspec ... oder kill -l [Signalname]
kill -15 $PID
# Output if XED are not more running:
kill -0 $PID
kill: Aufruf: kill [-s Signalname | -n Signalnummer | -Signalname] pid | jobspec ... oder kill -l [Signalname]
**[1]+ Beendet xed**
kill -15 $PID
睡觉 1
if ps -p $PID > /dev/null
然后
杀死-9 $PID
菲
基于 Eddy763 的一个答案,遵循逻辑上更合适的变体:
xed & PID=$! # Start XED and get PID
sleep 2
# kill -15 $PID # Soft Kill, activate or deactivate for testing
echo
echo
sleep 2
if ! ps -p $PID
then
echo "The PID/program are closed successful by Soft Kill"
else
echo "The PID/program didn't be killed successful by Soft Kill active, so it will be killed now on hard way"
kill -9 $PID # Hard Kill
fi
echo
echo
如何通过 bash 以软方式退出程序,如果不成功,则以硬方式退出程序。
知道如何通过bash打开程序以及如何获取程序的PID。以下示例打开编辑器 XED:
固定 & PID=$!
已知如何回显打开的程序的 PID:
回显 $PID
12345 # PID 的示例输出
已知如何尝试以软方式关闭程序以及如何通过 PID select 程序:
kill -15 $PID # select一个程序通过PID和托盘以软方式杀死它
已知如何以硬方式关闭程序以及如何通过 PID select 程序:
kill -9 $PID # select一个程序通过PID硬杀
把我所有的东西放在一起,还缺少一些东西:
xed & PID=$! # open the program xed and get the pid
echo PID of programm: $PID
sleep 5 # lets run the xed 5 seconds
kill -15 $PID # select the xed by PID and try to kill it on soft way
# check based on variant 1, 2, 3 or other, the xed was sucessfull killed on soft way or not
# if the xed still running, kill it on follow way
kill -9 $PID # select the xed by PID and kill it on hard way
#!/bin/bash
xed & PID=$! # Start XED and get PID
sleep 3
# kill -15 $PID # Soft Kill, activate or deactivate for testing
echo
echo
sleep 3
if ps -p $PID
then
echo "The PID/program didn't be killed successful by Soft Kill"
kill -9 $PID # Hard Kill
else
echo "PID/program are already closed by Soft Kill"
fi
echo
echo
您仍然可以通过以下方式运行获取有关进程的信息:
xed & xedPid=$! # Start of editor XED and asking for PID
[1] 12345
kill -15 $PID
# getting the follow, if the XED was killed successfull soft before:
kill -15 $PID
bash: kill: (12345) - Kein passender Prozess gefunden
**[1]+ Beendet xed**
您可以获取有关进程的信息是否仍然运行,也可以遵循以下方式:
xed & xedPid=$! # Start of editor XED and asking for PID
[1] 12345
# Output if xed are running
kill -0 $PID
kill: Aufruf: kill [-s Signalname | -n Signalnummer | -Signalname] pid | jobspec ... oder kill -l [Signalname]
kill -15 $PID
# Output if XED are not more running:
kill -0 $PID
kill: Aufruf: kill [-s Signalname | -n Signalnummer | -Signalname] pid | jobspec ... oder kill -l [Signalname]
**[1]+ Beendet xed**
kill -15 $PID 睡觉 1
if ps -p $PID > /dev/null 然后 杀死-9 $PID 菲
基于 Eddy763 的一个答案,遵循逻辑上更合适的变体:
xed & PID=$! # Start XED and get PID
sleep 2
# kill -15 $PID # Soft Kill, activate or deactivate for testing
echo
echo
sleep 2
if ! ps -p $PID
then
echo "The PID/program are closed successful by Soft Kill"
else
echo "The PID/program didn't be killed successful by Soft Kill active, so it will be killed now on hard way"
kill -9 $PID # Hard Kill
fi
echo
echo