我正在通过Jenkins在linux上执行一个pkill命令。当进程不存在时,此命令总是失败构建。
sudo docker exec mycontainer sh -c 'pkill -f processToKill || true '为什么?我怎样才能使它成功,即使是在pkill失败的时候?
发布于 2016-06-22 19:57:04
你要自杀了-- || true部分永远达不到:
$ sh -c 'pkill -f processToKill || true' ; echo $?
Terminated
143原因是您向-f提供了pkill标志。来自管理手册:
-f, --full
The pattern is normally only matched against the process name.
When -f is set, the full command line is used.您需要改进pkill语句的进程选择,这样它就不会捕获传递给sh的命令行。
发布于 2021-11-20 05:26:29
您可以将pkill封装在一个try-catch块中,这样就可以捕获并忽略有关杀死self的异常。
stage('Stop script') {
try {
execute_cmd = "pgrep -f script.sh | xargs -r kill"
sh """
ssh ${SSH_USER}@${HOST_IP} "$execute_cmd"
"""
} catch (ex) {
echo "${ex}"
}
}https://stackoverflow.com/questions/37930295
复制相似问题