Linux/Unix 命令确定进程是否正在运行? [英] Linux/Unix command to determine if process is running?

查看:43
本文介绍了Linux/Unix 命令确定进程是否正在运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个独立于平台的 (Linux/Unix|OSX) shell/bash 命令来确定特定进程是否正在运行.例如mysqldhttpd...执行此操作的最简单方法/命令是什么?

解决方案

虽然 pidofpgrep 是确定正在运行什么的好工具,但不幸的是,它们都不可用在某些操作系统上.一个明确的故障安全将使用以下内容: ps cax |grep 命令

Gentoo Linux 上的输出:

<前>14484?S 0:00 apache214667?S 0:00 apache219620 ?SL 0:00 apache221132?Ss 0:04 apache2

OS X 上的输出:

<前>42582 ??Z 0:00.00 (smbclient)46529 ??Z 0:00.00 (smbclient)46539 ??Z 0:00.00 (smbclient)46547 ??Z 0:00.00 (smbclient)46586 ??Z 0:00.00 (smbclient)46594 ??Z 0:00.00 (smbclient)

在 Linux 和 OS X 上,grep 会返回一个退出代码,因此很容易检查是否找到了进程:

#!/bin/bashps cax |grep httpd >/开发/空如果 [$?-eq 0 ];然后echo "进程正在运行."别的echo "进程没有运行."菲

<小时>

此外,如果您想要 PID 列表,您也可以轻松地 grep 获取这些列表:

ps cax |grep httpd |grep -o '^[ ]*[0-9]*'

在 Linux 和 OS X 上的输出相同:

3519 3521 3523 3524

下面的输出是一个空字符串,使这种方法对于未运行的进程是安全的:

echo ps cax |grep aasdfasdf |grep -o '^[ ]*[0-9]*'

这种方法适用于编写一个简单的空字符串测试,然后甚至遍历发现的 PID.

#!/bin/bash进程=$1PIDS=`ps cax |grep $PROCESS |grep -o '^[ ]*[0-9]*'`如果 [ -z "$PIDS" ];然后echo "进程没有运行."1>&2出口 1别的对于 $PIDS 中的 PID;做回声$PID完毕菲

您可以通过将其保存到具有执行权限(chmod +x running)的文件(名为running")并使用参数执行它来测试它:./running "httpd"

#!/bin/bashps cax |grep httpd如果 [$?-eq 0 ];然后echo "进程正在运行."别的echo "进程没有运行."菲

<小时>

警告!!!

请记住,您只是在解析 ps ax 的输出,这意味着,正如在 Linux 输出中所见,它不仅匹配进程,还包括传递给的参数那个程序.我强烈建议在使用此方法时尽可能具体(例如 ./running "mysql" 也将匹配 'mysqld' 进程).我强烈建议尽可能使用 which 来检查完整路径.

<小时>

参考:

http://linux.about.com/od/commands/l/blcmdl1_ps.htm

http://linux.about.com/od/commands/l/blcmdl1_grep.htm

I need a platform independent (Linux/Unix|OSX) shell/bash command that will determine if a specific process is running. e.g. mysqld, httpd... What is the simplest way/command to do this?

While pidof and pgrep are great tools for determining what's running, they are both, unfortunately, unavailable on some operating systems. A definite fail safe would be to use the following: ps cax | grep command

The output on Gentoo Linux:

14484 ?        S      0:00 apache2
14667 ?        S      0:00 apache2
19620 ?        Sl     0:00 apache2
21132 ?        Ss     0:04 apache2

The output on OS X:

42582   ??  Z      0:00.00 (smbclient)
46529   ??  Z      0:00.00 (smbclient)
46539   ??  Z      0:00.00 (smbclient)
46547   ??  Z      0:00.00 (smbclient)
46586   ??  Z      0:00.00 (smbclient)
46594   ??  Z      0:00.00 (smbclient)

On both Linux and OS X, grep returns an exit code so it's easy to check if the process was found or not:

#!/bin/bash
ps cax | grep httpd > /dev/null
if [ $? -eq 0 ]; then
  echo "Process is running."
else
  echo "Process is not running."
fi


Furthermore, if you would like the list of PIDs, you could easily grep for those as well:

ps cax | grep httpd | grep -o '^[ ]*[0-9]*'

Whose output is the same on Linux and OS X:

3519 3521 3523 3524

The output of the following is an empty string, making this approach safe for processes that are not running:

echo ps cax | grep aasdfasdf | grep -o '^[ ]*[0-9]*'

This approach is suitable for writing a simple empty string test, then even iterating through the discovered PIDs.

#!/bin/bash
PROCESS=$1
PIDS=`ps cax | grep $PROCESS | grep -o '^[ ]*[0-9]*'`
if [ -z "$PIDS" ]; then
  echo "Process not running." 1>&2
  exit 1
else
  for PID in $PIDS; do
    echo $PID
  done
fi

You can test it by saving it to a file (named "running") with execute permissions (chmod +x running) and executing it with a parameter: ./running "httpd"

#!/bin/bash
ps cax | grep httpd
if [ $? -eq 0 ]; then
  echo "Process is running."
else
  echo "Process is not running."
fi


WARNING!!!

Please keep in mind that you're simply parsing the output of ps ax which means that, as seen in the Linux output, it is not simply matching on processes, but also the arguments passed to that program. I highly recommend being as specific as possible when using this method (e.g. ./running "mysql" will also match 'mysqld' processes). I highly recommend using which to check against a full path where possible.


References:

http://linux.about.com/od/commands/l/blcmdl1_ps.htm

http://linux.about.com/od/commands/l/blcmdl1_grep.htm

这篇关于Linux/Unix 命令确定进程是否正在运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆