如何使用 shell 脚本删除 Linux 进程 ID 中的多余字符
我试图通过 pid 杀死一个进程,这是我从网上找到的脚本。
PID=`ps -ef | grep myProcess | grep -v grep | awk '{print $2}'`
echo -e Killing myProcess with pid: $PID..
输出:Killing myProcesswith pid: 13275^M ..
有谁知道为什么会有 ^M ,我该如何摆脱它,因为kill命令无法运行:
**arguments must be process or job IDs**
我在网上搜索但仍然不知道如何克服这个问题.. 任何帮助表示赞赏!谢谢!
I m trying to kill a process by its pid, and this is the script that I found from web.
PID=`ps -ef | grep myProcess | grep -v grep | awk '{print $2}'`
echo -e Killing myProcess with pid: $PID..
Output: Killing myProcesswith pid: 13275^M..
Does anyone know why is there a ^M , how do I get rid of that because the kill command failed to run :
**arguments must be process or job IDs**
I searched online but still got no idea how to overcome this..
Any help is appreciated!! Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,你的语法是错误的。使用
$()
调用命令并将其输出存储到变量Second,您可以在一个
awk
语句中完成这一切,而不需要额外的grep进程。
first, your syntax is wrong. Use
$()
to call a command and store its output to variablesecond, you can do this all in one
awk
statement without the need for extragrep
processes.从在线快速阅读来看,awk 的 print 命令总是附加换行符(有时可以用 Control-M 或 ^M 表示)。
printf 似乎是一个合适的替代方案。或许:
From a quick read online, the print command to awk always appends a newline (which can sometimes be represented by Control-M, or ^M).
It would appear that printf would be a suitable alternative. Maybe:
据我所知,您不想通过 PID 或名称来终止进程。你可以通过获取进程 PID 来做到这一点,然后尝试通过 PID 杀死它。如果您想按名称杀死,请使用
killall processname
。From what I see, you dont want to kill a process by PID, by its name. And you do it by getting the process PID and then try to kill it via PID. If you want to kill by name, use
killall processname
.你可以只使用:
You can just use :