如何检查进程是否在linux中运行

时间:2022-07-03 16:24:25

I am trying to automatically check if a process is running or not and have to perform next steps accordingly. I had written a bash script but it doesn't seem to work.

我正在尝试自动检查进程是否正在运行,并且必须相应地执行后续步骤。我写了一个bash脚本,但它似乎不起作用。

if ps aux | grep [M]yProcessName > /dev/null
then
  echo "Running"
else
  echo "Not running"
fi

Is my if statement wrongly used?

我的if语句被错误地使用了吗?

9 个解决方案

#1


10  

You don't want to know if a particular process (of known pid) is running (this can be done by testing if /proc/1234/ exists for pid 1234) but if some process is running a given command (or a given executable).

您不想知道特定进程(已知pid)是否正在运行(这可以通过测试pid 1234是否存在/ proc / 1234 /来完成)但是如果某个进程正在运行给定命令(或给定的可执行文件) )。

Notice that the kill(2) syscall can be portably used to check if a given process is running (with a 0 signal, e.g. kill(pid,0)). From inside a program, this is a common way to check that a process of known pid is still existing and running (or waiting).

请注意,kill(2)系统调用可以方便地用于检查给定进程是否正在运行(带有0信号,例如kill(pid,0))。从程序内部,这是检查已知pid的进程是否仍然存在并正在运行(或等待)的常用方法。

You could use the pidof command to find the processes running some executable, e.g. pidof zsh to find all the zsh processes. You could also use killall -s 0 zsh

您可以使用pidof命令查找运行某些可执行文件的进程,例如pidof zsh找到所有zsh进程。你也可以使用killall -s 0 zsh

And you might be interested by the pgrep utility and the /proc filesystem.

您可能对pgrep实用程序和/ proc文件系统感兴趣。

#2


6  

ps aux | grep [M]yProcessName | grep -v grep

#3


2  

There is a solution:

有一个解决方案:

if [ "$(ps aux | grep "what you need" | awk '{print $11}')" == "grep" ]; then ... elif [ ... ]; then ... else ... fi

This works fine in Debian 6. '{print $11}' is needed, because the sytem treats grep as a process as well

这在Debian 6中工作得很好。'{print $ 11}'是必需的,因为系统也将grep视为一个过程

#4


2  

processid =$(ps aux | grep 'ProcessName' | grep -v grep| awk '{print $2}')

The above command will give you the process id. Assign that process id to a variable and do this -->

上面的命令将为您提供进程ID。将该进程ID分配给变量并执行此操作 - >

if cat /proc/$processid/status | grep "State:  R (running)" > /dev/null
then
  echo "Running"
else
  echo "Not running"
fi

#5


1  

Using -z to check if a string is empty or not, something like this could work:

使用-z检查字符串是否为空,这样的东西可以工作:

line=$(ps aux | grep [M]yProcessName)
if [ -z "$line" ]
then
    echo "Not Running"
else
    echo $line > /dev/null
    echo "Rinnung"
fi

#6


0  

On my system, ps aux | grep ProcessName always get a line of that grep process like:

在我的系统上,ps aux | grep ProcessName总是得到一个grep进程的行,如:

edw4rd     9653  0.0  0.0   4388   832 pts/1    S+   21:09   0:00 grep --color=auto ProcessName

So, the exit status is always 0. Maybe that's why your script doesn't work.

因此,退出状态始终为0.也许这就是您的脚本不起作用的原因。

#7


0  

SMBD=$(pidof smbd)
if [ "$SMBD" == "" ];
then
   /etc/init.d/samba start;
else
   /etc/init.d/samba restart;
fi

#8


0  

return 0 means success while others failed

return 0表示成功,而其他表示失败

kill -0 `pid`; echo $?

#9


-1  

try this

尝试这个

ps aux | grep [M]yProcessName | grep -v grep

#1


10  

You don't want to know if a particular process (of known pid) is running (this can be done by testing if /proc/1234/ exists for pid 1234) but if some process is running a given command (or a given executable).

您不想知道特定进程(已知pid)是否正在运行(这可以通过测试pid 1234是否存在/ proc / 1234 /来完成)但是如果某个进程正在运行给定命令(或给定的可执行文件) )。

Notice that the kill(2) syscall can be portably used to check if a given process is running (with a 0 signal, e.g. kill(pid,0)). From inside a program, this is a common way to check that a process of known pid is still existing and running (or waiting).

请注意,kill(2)系统调用可以方便地用于检查给定进程是否正在运行(带有0信号,例如kill(pid,0))。从程序内部,这是检查已知pid的进程是否仍然存在并正在运行(或等待)的常用方法。

You could use the pidof command to find the processes running some executable, e.g. pidof zsh to find all the zsh processes. You could also use killall -s 0 zsh

您可以使用pidof命令查找运行某些可执行文件的进程,例如pidof zsh找到所有zsh进程。你也可以使用killall -s 0 zsh

And you might be interested by the pgrep utility and the /proc filesystem.

您可能对pgrep实用程序和/ proc文件系统感兴趣。

#2


6  

ps aux | grep [M]yProcessName | grep -v grep

#3


2  

There is a solution:

有一个解决方案:

if [ "$(ps aux | grep "what you need" | awk '{print $11}')" == "grep" ]; then ... elif [ ... ]; then ... else ... fi

This works fine in Debian 6. '{print $11}' is needed, because the sytem treats grep as a process as well

这在Debian 6中工作得很好。'{print $ 11}'是必需的,因为系统也将grep视为一个过程

#4


2  

processid =$(ps aux | grep 'ProcessName' | grep -v grep| awk '{print $2}')

The above command will give you the process id. Assign that process id to a variable and do this -->

上面的命令将为您提供进程ID。将该进程ID分配给变量并执行此操作 - >

if cat /proc/$processid/status | grep "State:  R (running)" > /dev/null
then
  echo "Running"
else
  echo "Not running"
fi

#5


1  

Using -z to check if a string is empty or not, something like this could work:

使用-z检查字符串是否为空,这样的东西可以工作:

line=$(ps aux | grep [M]yProcessName)
if [ -z "$line" ]
then
    echo "Not Running"
else
    echo $line > /dev/null
    echo "Rinnung"
fi

#6


0  

On my system, ps aux | grep ProcessName always get a line of that grep process like:

在我的系统上,ps aux | grep ProcessName总是得到一个grep进程的行,如:

edw4rd     9653  0.0  0.0   4388   832 pts/1    S+   21:09   0:00 grep --color=auto ProcessName

So, the exit status is always 0. Maybe that's why your script doesn't work.

因此,退出状态始终为0.也许这就是您的脚本不起作用的原因。

#7


0  

SMBD=$(pidof smbd)
if [ "$SMBD" == "" ];
then
   /etc/init.d/samba start;
else
   /etc/init.d/samba restart;
fi

#8


0  

return 0 means success while others failed

return 0表示成功,而其他表示失败

kill -0 `pid`; echo $?

#9


-1  

try this

尝试这个

ps aux | grep [M]yProcessName | grep -v grep