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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13260974/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 17:43:29  来源:igfitidea点击:

How to check if process is running in linux

linuxbash

提问by noMAD

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 ifstatement wrongly used?

我的if陈述是否被错误地使用?

采纳答案by Basile Starynkevitch

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 的)特定进程是否正在运行(这可以通过测试/proc/1234/pid 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 pidofcommand to find the processes running some executable, e.g. pidof zshto find all the zshprocesses. You could also use killall -s 0 zsh

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

And you might be interested by the pgreputility and the /procfilesystem.

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

回答by Joe Yang

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

回答by gaoxinbo

try this

尝试这个

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

回答by Edw4rd

On my system, ps aux | grep ProcessNamealways 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。也许这就是您的脚本不起作用的原因。

回答by Guest67

There is a solution:

有一个解决方案:

if [ "$(ps aux | grep "what you need" | awk '{print }')" == "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 视为一个进程

回答by Yogeesh Seralathan

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

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

回答by begin

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

回答by Ender Fletcher

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

回答by vinllen

return 0 means success while others failed

返回 0 表示成功,其他则失败

kill -0 `pid`; echo $?