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

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

问题描述

我需要独立的平台(的Linux / Unix | OSX)外壳/ bash命令,将确定特定的进程正在运行。例如的mysqld 的httpd ...
什么是最简单的方法/命令来做到这一点?

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?

推荐答案

的pidof 指派,都是伟大的工具确定什么样的运行,他们都是不幸的是,在某些操作系统上不可用。一个明确的故障安全是使用以下内容: PS CAX | grep命令

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

在Gentoo Linux上的输出:


14484 ?        S      0:00 apache2
14667 ?        S      0:00 apache2
19620 ?        Sl     0:00 apache2
21132 ?        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返回退出code,所以很容易检查的过程中发现或不:

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


此外,如果您想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。

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

您可以通过它保存到一个文件(名为运行),测试执行权限(使用chmod + x的),并带有一个参数执行它: ./运行的httpd

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


警告!

请记住,你只是解析 PS AX 这意味着,如Linux输出看到的,它不是简单地处理匹配的输出,而且参数传递给该程序。我强烈建议使用此方法时是尽可能具体(如 ./运行MySQL的也将匹配'的mysqld过程)。我强烈建议使用来核对一个完整路径成为可能。

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.

参考文献:

<一个href=\"http://linux.about.com/od/commands/l/blcmdl1_ps.htm\">http://linux.about.com/od/commands/l/blcmdl1_ps.htm

<一个href=\"http://linux.about.com/od/commands/l/blcmdl1_grep.htm\">http://linux.about.com/od/commands/l/blcmdl1_grep.htm

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

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