捕获进程 ID 并在存在时将其杀死的 Shell 脚本 [英] Shell script to capture Process ID and kill it if exist

查看:25
本文介绍了捕获进程 ID 并在存在时将其杀死的 Shell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过这段代码,但它不起作用

I tried this code and it is not working

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print $2}'`

if [[ -z "$PID" ]] then
Kill -9 PID
fi

它在 awk 附近显示错误.

It is showing a error near awk.

请提出任何建议.

推荐答案

实际上,最简单的方法是传递如下 kill 参数:

Actually the easiest way to do that would be to pass kill arguments like below:

ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill

希望有帮助.

这篇关于捕获进程 ID 并在存在时将其杀死的 Shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆