use*_*811 59 bash shell scripting
我尝试了这个代码,但它无法正常工作
#!/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
Run Code Online (Sandbox Code Playgroud)
它在awk附近显示错误.
请给我任何建议.
小智 141
实际上,最简单的方法是传递如下所示的kill参数:
ps -ef | grep your_process_name | grep -v grep | awk '{print $2}' | xargs kill
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你.
N D*_*auf 14
这对我有好处.
PID=`ps -eaf | grep syncapp | grep -v grep | awk '{print $2}'`
if [[ "" != "$PID" ]]; then
echo "killing $PID"
kill -9 $PID
fi
我用这个命令pkill:
NAME
pgrep, pkill - look up or signal processes based on name and
other attributes
SYNOPSIS
pgrep [options] pattern
pkill [options] pattern
DESCRIPTION
pgrep looks through the currently running processes and lists
the process IDs which match the selection criteria to stdout.
All the criteria have to match. For example,
$ pgrep -u root sshd
will only list the processes called sshd AND owned by root.
On the other hand,
$ pgrep -u root,daemon
will list the processes owned by root OR daemon.
pkill will send the specified signal (by default SIGTERM)
to each process instead of listing them on stdout.
Run Code Online (Sandbox Code Playgroud)
如果您的代码通过解释器(java,python,...)运行,那么进程的名称就是解释器的名称.您需要使用参数--full.这与命令名称和参数匹配.
你可能想写
`ps -ef | grep syncapp | awk '{print $2}'`
Run Code Online (Sandbox Code Playgroud)
但我会赞同@PaulR 的回答——这killall -9 syncapp是一个更好的选择。