帮我写一个脚本:执行ps -ef | grep auth-server.jar|grep -v grep |awk '{print$2}',如果返回结果为0,则不管,否则执行ps -ef | grep auth-server.jar|grep -v grep |awk '{print$2}'| xargs kill -9
时间: 2023-07-06 13:35:10 浏览: 182
好的,以下是一个简单的脚本,可以实现你的需求:
```bash
#!/bin/bash
PID=$(ps -ef | grep auth-server.jar | grep -v grep | awk '{print $2}')
if [[ -z $PID ]]; then
echo "auth-server.jar is not running."
else
echo "auth-server.jar is running with PID $PID. Killing the process..."
ps -ef | grep auth-server.jar | grep -v grep | awk '{print $2}' | xargs kill -9
echo "Process has been killed."
fi
```
这个脚本首先使用 `ps -ef | grep auth-server.jar | grep -v grep | awk '{print $2}'` 命令查找进程 ID,并将其赋值给变量 `$PID`。然后判断 `$PID` 是否为空,如果为空则输出 `auth-server.jar is not running.`,否则输出 `auth-server.jar is running with PID $PID. Killing the process...`,并使用 `ps -ef | grep auth-server.jar | grep -v grep | awk '{print $2}' | xargs kill -9` 命令结束进程。最后输出 `Process has been killed.`。
你只需要将这段代码保存到一个文件中,比如 `kill_auth_server.sh`,然后在终端中执行 `bash kill_auth_server.sh` 即可。
阅读全文