用于检查 shell 脚本是否正在运行的 Linux 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16828035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linux command to check if a shell script is running or not
提问by Manikandan Kannan
What is the linux command to find if a process say aa.sh is running or not. ps command does not seem to work and it does not show the shell script names.
查找进程是否说 aa.sh 正在运行的 linux 命令是什么。ps 命令似乎不起作用,并且不显示 shell 脚本名称。
Please advise.
请指教。
采纳答案by krizna
Check this
检查这个
ps aux | grep "aa.sh"
回答by lc2817
Give an option to ps to display all the processes, an example is:
给 ps 一个选项来显示所有进程,一个例子是:
ps -A | grep "myshellscript.sh"
Check http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/for more info
查看http://www.cyberciti.biz/faq/show-all-running-processes-in-linux/了解更多信息
And as Basile Starynkevitch mentioned in the comment pgrep
is another solution.
正如评论中提到的 Basile Starynkevitchpgrep
是另一种解决方案。
回答by NRJ
Adding to the answers above -
添加到上面的答案 -
To use in a script, use the following :-
要在脚本中使用,请使用以下内容:-
result=`ps aux | grep -i "myscript.sh" | grep -v "grep" | wc -l`
if [ $result -ge 1 ]
then
echo "script is running"
else
echo "script is not running"
fi
回答by Gilles Quenot
The simplest and efficient solution is :
最简单有效的解决方案是:
pgrep -fl aa.sh
回答by mklement0
The solutions above are great for interactive use, where you can eyeball the result and weed out false positives that way.
上面的解决方案非常适合交互式使用,您可以在其中观察结果并以这种方式清除误报。
False positives can occur if the executable itself happens to match, or any arguments that are not script names match - the likelihood is greater with scripts that have no filename extensions.
如果可执行文件本身恰好匹配,或者任何不是脚本名称匹配的参数,则可能会发生误报 - 没有文件扩展名的脚本的可能性更大。
Here's a more robustsolution for scripting, using a shell function:
这是一个更强大的脚本解决方案,使用shell 函数:
getscript() {
pgrep -lf ".[ /]( |$)"
}
Example use:
使用示例:
# List instance(s) of script "aa.sh" that are running.
getscript "aa.sh" # -> (e.g.): 96112 bash /Users/jdoe/aa.sh
# Use in a test:
if getscript "aa.sh" >/dev/null; then
echo RUNNING
fi
- Matching is case-sensitive (on macOS, you could add
-i
to thepgrep
call to make it case-insensitive; on Linux, that is not an option.) - The
getscript
function also works with full or partial paths that include the filename component; partial paths must not start with/
and each component specified must be complete. The "fuller" the path specified, the lower the risk of false positives. Caveat: path matching will only work if the script was invokedwith a path - this is generally true for scripts in the $PATH that are invoked directly. - Even this function cannot rule out all false positives, as paths can have embedded spaces, yet neither
ps
norpgrep
reflect the original quoting applied to the command line. All the function guarantees is that any match is not the first token(which is the interpreter), and that it occurs as a separate word, optionally preceded by a path. - Another approach to minimizing the risk of false positives could be to match the executablename (i.e., interpreter, such as
bash
) as well - assuming it is known; e.g.
- 匹配是大小写敏感的(在MacOS,你可以添加
-i
到pgrep
通话,使之区分于敏感;在Linux上,这不是一个选项) - 该
getscript
函数也适用于包含文件名组件的完整或部分路径;部分路径不能以开头,/
并且指定的每个组件都必须是完整的。指定的路径越“完整”,误报的风险就越低。警告:路径匹配仅在使用路径调用脚本时才有效- 这对于 $PATH 中直接调用的脚本通常是正确的。 - 即使这个函数也不能排除所有误报,因为路径可以嵌入空格,但既不能
ps
也不能pgrep
反映应用于命令行的原始引用。所有的函数都保证任何匹配都不是第一个标记(它是解释器),并且它作为一个单独的 word 出现,可选地前面有一个路径。 - 另一种最小化误报风险的方法是匹配可执行文件名称(即解释器,例如
bash
)——假设它是已知的;例如
# List instance(s) of a running *bash* script.
getbashscript() {
pgrep -lf "(^|/)bash( | .*/)( |$)"
}
If you're willing to make further assumptions - such as script-interpreter paths never containing embedded spaces - the regexes could be made more restrictive and thus further reduce the risk of false positives.
如果您愿意做出进一步的假设 - 例如脚本解释器路径从不包含嵌入的空格 - 可以使正则表达式更具限制性,从而进一步降低误报的风险。
回答by Prashant Kandathil
pgrep -f aa.sh
To do something with the id, you pipe it. Here I kill all its child tasks.
要对 id 做一些事情,你可以通过管道来处理它。在这里,我杀死了它的所有子任务。
pgrep aa.sh | xargs pgrep -P ${} | xargs kill
If you want to execute a command if the process is running do this
如果要在进程正在运行时执行命令,请执行此操作
pgrep aa.sh && echo Running
回答by rj45
I was quite inspired by the last answer by mklement0 - I have few scripts/small programs I run at every reboot via /etc/crontab
. I built on his answer and built a login script, which shows if my programs are still running.
I execute this scripts.sh
via .profile
-file on every login, to get instant notification on each login.
mklement0 的最后一个答案给了我很大的启发 - 我在每次重新启动时通过/etc/crontab
. 我根据他的回答构建了一个登录脚本,它显示我的程序是否仍在运行。我在每次登录时scripts.sh
通过.profile
-file执行此操作,以便在每次登录时获得即时通知。
cat scripts.sh
#!/bin/bash
getscript() {
pgrep -lf ".[ /]( |$)"
}
script1=keepalive.sh
script2=logger_v3.py
# test if script 1 is running
if getscript "$script1" >/dev/null; then
echo "$script1" is RUNNING
else
echo "$script1" is NOT running
fi
# test if script 2 is running:
if getscript "$script2" >/dev/null; then
echo "$script2" is RUNNING
else
echo "$script2" is NOT running
fi
回答by Saurabh Lende
Check this
检查这个
ps -ef | grep shellscripname.sh
You can also find your running process in
您还可以在
ps -ef
回答by JaySync
here a quick script to test if a shell script is running
这里有一个快速脚本来测试 shell 脚本是否正在运行
#!/bin/sh
scripToTest="your_script_here.sh"
scriptExist=$(pgrep -f "$scripToTest")
[ -z "$scriptExist" ] && echo "$scripToTest : not running" || echo "$scripToTest : runnning"