杀死进程的 shell 脚本
我需要实现一个终止进程的 shell 脚本。问题是我需要执行一个条件才能查看进程是否正在运行。
这是我的代码,但它不起作用:
#!/bin/sh
if [ -x "MY_PROCCESS_NAME"]; then
killall MY_PROCCESS_NAME
else
echo "Doesn't exist"
fi
这是错误:
line 3: [: missing `]'
I need to implement a shell script that kills a process. The problem is that I need to do a conditional to be able to see if the process is running or not.
This is my code, but it is not working:
#!/bin/sh
if [ -x "MY_PROCCESS_NAME"]; then
killall MY_PROCCESS_NAME
else
echo "Doesn't exist"
fi
This is the error:
line 3: [: missing `]'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要检查进程是否在 mac os x 上运行,您可以使用:
如果您想减少 shell 脚本的数量,您可以将进程名称的字符之一括在方括号中:
结合在您的测试中,这看起来像:
这比在 Linux 下需要做的要复杂一些,因为通常有“pgrep”命令,它大致相当于“ps -fe |” | grep ... | grep -v grep'
to check if a process is running on mac os x you can use:
if you want to reduce the number of shell scripts you can enclose one of the characters of the name of the process in square brackets:
combined in your test this would look like:
it's a little more complicated than you would need to do under linux as you generally have the 'pgrep' command, which is the rough equivalent of the 'ps -fe | grep ... | grep -v grep'
不确定它是否可以在 OSX 中运行,它可以在 ubuntu 中运行。
但作为一个班轮:
ps aux | awk '$11~/vim/ {PID = $2} END {if (PID) print "kill -9 "PID;否则打印“回显无进程”}' | bash
它的作用是找到一个进程,在本例中是 vim 并返回
kill -9 pid
如果没有找到字符串,则返回echo no process
然后它将输出传送到 bash。not sure if it would work in OSX, it works in ubuntu.
but as a one liner:
ps aux | awk '$11~/vim/ {PID = $2} END {if (PID) print "kill -9 "PID; else print "echo no process"}' | bash
what it does is it finds a process, in this case, vim and returns the
kill -9 pid
if no string is found it returnsecho no process
it then pipes the output to bash.