【问题标题】:CentOS 7 pidof -x did not workCentOS 7 pidof -x 不起作用
【发布时间】:2016-12-23 17:00:08
【问题描述】:

我有一个 CentOS 7 服务器虚拟机。在这个虚拟机中,我安装了Apache Apollo

我刚刚创建了下面给出的脚本

#!/bin/bash

if pidof -x "apollo" >/dev/null; then
echo "Apollo MQTT is Running."
exit 0
else
echo "Apollo MQTT is Stopped."
exit 2
fi

使用上面的脚本我想检查 apollo 服务是否正在运行。但是当我使用下面的命令运行这个脚本时

sh filename

它在 servce 运行时输出Apollo MQTT is Stopped

当我在命令下运行时

ps -ef | grep apollo

它给出以下输出

root       8647      1  3 10:49 pts/0    00:00:55 java -ea -server -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:-UseBiasedLocking -Dcom.sun.management.jmxremote -Dapollo.home=/opt/apollo -Dapollo.base=/var/lib/mqtt -classpath /opt/apollo/lib/apollo-boot.jar org.apache.activemq.apollo.boot.Apollo /var/lib/mqtt/lib\;/opt/apollo/lib org.apache.activemq.apollo.cli.Apollo run
root       9426   8233  0 11:15 pts/0    00:00:00 grep --color=auto apollo

【问题讨论】:

  • pgrep -f apollo >/dev/null?
  • @Cyrus 当我使用“if pgrep -f apollo >/dev/null; then”时,它总是显示 Apollo MQTT 正在运行。如果服务正在运行并停止这两种情况。
  • 停止阿波罗并与ps -ef | grep apollo联系。
  • 奇怪。停止 Apollo,请发布 pgrep -f apollo 的输出。
  • 我建议在Super User询问。

标签: linux bash shell centos apollo


【解决方案1】:

我相信您忽略了一个问题。进程是java实例,你程序的pidof就是java实例的。 pidofapollo 一无所知,因为这对他来说就像一个论据。

我会推荐来自 muzido 的解决方案

【讨论】:

  • 你是对的。我的 apollo 服务使用 java 实例。但我的问题是它总是使用 java 实例?如果我检查 pidof -x "java" 目前它运行完美,但未来 apollo 总是使用 java 实例?
【解决方案2】:

你可以用这个;

if (( $(ps -ef | grep -v grep | grep -i apollo | wc -l) > 0 ))
then
 echo "Apollo MQTT is Running."
 exit 0
else
 echo "Apollo MQTT is Stopped."
 exit 2
fi

【讨论】:

  • 以上代码始终显示 Apollo MQTT 正在运行。如果服务正在运行并停止这两种情况。
  • 为什么你需要整个管道,当你可以简单地使用pgrep,或者更糟的情况,if ps -ef | grep -iq "[a]pollo"; then ... fi,或者最坏的情况,if ps -ef | grep -i "[a]pollo" &>/dev/null; then ... fi
  • 能否提供 ps -ef | grep -v grep | grep -i 阿波罗
  • 试试; if (( $(ps -ef | grep -v grep | grep -i "org.apache.activemq.apollo.cli.Apollo run" | wc -l) > 0 ))
  • @M.Dogru 当我启动 apollo 并运行“ps -ef | grep -v grep | grep -i apollo”时,输出为“root 12990 1 99 12:48 pts/0 00:00: 19 java -ea -server -Xmx1G -XX:+HeapDumpOnOutOfMemoryError -XX:-UseBiasedLocking -Dcom.sun.management.jmxremote -Dapollo.home=/opt/apollo -Dapollo.base=/var/lib/mqtt -classpath /opt /apollo/lib/apollo-boot.jar org.apache.activemq.apollo.boot.Apollo /var/lib/mqtt/lib\;/opt/apollo/lib org.apache.activemq.apollo.cli.Apollo run"当我停止 apollo 运行“ps -ef | grep -v grep | grep -i apollo”输出为空白。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-21
  • 1970-01-01
  • 1970-01-01
  • 2017-10-12
  • 2015-10-28
  • 1970-01-01
相关资源
最近更新 更多