用于检查shell脚本是否正在运行的Linux命令

时间:2021-01-07 00:09:05

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.

请指教。

8 个解决方案

#1


21  

Check this

检查一下

ps aux | grep "aa.sh"

#2


22  

The simplest solution is :

最简单的解决方案是:

pgrep -fl aa.sh

#3


4  

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

#4


3  

Check this

检查一下

ps -ef | grep shellscripname.sh

You can also find your running process in

您还可以在中找到正在运行的进程

ps -ef

#5


1  

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 robust solution for scripting, using a shell function:

这是一个更强大的脚本解决方案,使用shell函数:

getscript() {
  pgrep -lf ".[ /]$1( |\$)"
}

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 OSX, you could add -i to the pgrep call to make it case-*in*sensitive; on Linux, that is not an option.)
  • 匹配区分大小写(在OSX上,您可以在pgrep调用中添加-i以使其*在*敏感中;在Linux上,这不是一个选项。)
  • 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 invoked with a path - this is generally true for scripts in the $PATH that are invoked directly.
  • getscript函数也适用于包含文件名组件的完整或部分路径;部分路径不能以/开头,并且指定的每个组件都必须完整。指定路径“更饱满”,误报风险越低。警告:路径匹配仅在使用路径调用脚本时才有效 - 对于直接调用的$ PATH中的脚本通常是这样。
  • Even this function cannot rule out all false positives, as paths can have embedded spaces, yet neither ps nor pgrep 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.
  • 即使这个函数也不能排除所有误报,因为路径可以有嵌入空格,但ps和pgrep都不能反映应用于命令行的原始引用。所有函数保证是任何匹配都不是第一个标记(它是解释器),并且它作为单独的单词出现,可选地前面有一个路径。
  • Another approach to minimizing the risk of false positives could be to match the executable name (i.e., interpreter, such as bash) as well - assuming it is known; e.g.
  • 另一种最小化误报风险的方法可能是匹配可执行文件名(即解释器,例如bash) - 假设它已知;例如
# List instance(s) of a running *bash* script.
getbashscript() {
  pgrep -lf "(^|/)bash( | .*/)$1( |\$)"
}

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.

如果您愿意做出进一步的假设 - 例如从不包含嵌入空间的脚本解释器路径 - 可以使正则表达式更具限制性,从而进一步降低误报的风险。

#6


1  

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

#7


1  

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运行的脚本/小程序很少。我建立了他的答案,并构建了一个登录脚本,显示我的程序是否仍在运行。我在每次登录时通过.profile -file执行此scripts.sh,以获得每次登录的即时通知。

cat scripts.sh 
#!/bin/bash

getscript() {
  pgrep -lf ".[ /]$1( |\$)"
}

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

#8


0  

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 Starynkevitch在评论中提到的那样,pgrep是另一种解决方案。

#1


21  

Check this

检查一下

ps aux | grep "aa.sh"

#2


22  

The simplest solution is :

最简单的解决方案是:

pgrep -fl aa.sh

#3


4  

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

#4


3  

Check this

检查一下

ps -ef | grep shellscripname.sh

You can also find your running process in

您还可以在中找到正在运行的进程

ps -ef

#5


1  

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 robust solution for scripting, using a shell function:

这是一个更强大的脚本解决方案,使用shell函数:

getscript() {
  pgrep -lf ".[ /]$1( |\$)"
}

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 OSX, you could add -i to the pgrep call to make it case-*in*sensitive; on Linux, that is not an option.)
  • 匹配区分大小写(在OSX上,您可以在pgrep调用中添加-i以使其*在*敏感中;在Linux上,这不是一个选项。)
  • 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 invoked with a path - this is generally true for scripts in the $PATH that are invoked directly.
  • getscript函数也适用于包含文件名组件的完整或部分路径;部分路径不能以/开头,并且指定的每个组件都必须完整。指定路径“更饱满”,误报风险越低。警告:路径匹配仅在使用路径调用脚本时才有效 - 对于直接调用的$ PATH中的脚本通常是这样。
  • Even this function cannot rule out all false positives, as paths can have embedded spaces, yet neither ps nor pgrep 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.
  • 即使这个函数也不能排除所有误报,因为路径可以有嵌入空格,但ps和pgrep都不能反映应用于命令行的原始引用。所有函数保证是任何匹配都不是第一个标记(它是解释器),并且它作为单独的单词出现,可选地前面有一个路径。
  • Another approach to minimizing the risk of false positives could be to match the executable name (i.e., interpreter, such as bash) as well - assuming it is known; e.g.
  • 另一种最小化误报风险的方法可能是匹配可执行文件名(即解释器,例如bash) - 假设它已知;例如
# List instance(s) of a running *bash* script.
getbashscript() {
  pgrep -lf "(^|/)bash( | .*/)$1( |\$)"
}

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.

如果您愿意做出进一步的假设 - 例如从不包含嵌入空间的脚本解释器路径 - 可以使正则表达式更具限制性,从而进一步降低误报的风险。

#6


1  

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

#7


1  

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运行的脚本/小程序很少。我建立了他的答案,并构建了一个登录脚本,显示我的程序是否仍在运行。我在每次登录时通过.profile -file执行此scripts.sh,以获得每次登录的即时通知。

cat scripts.sh 
#!/bin/bash

getscript() {
  pgrep -lf ".[ /]$1( |\$)"
}

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

#8


0  

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 Starynkevitch在评论中提到的那样,pgrep是另一种解决方案。