Bash 脚本中的 pidof 问题

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

Problem with pidof in Bash script

bashif-statement

提问by alexfr

I've written a script for me to start and stop my Perforce server. To shutdown the server I use the kill -SIGTERMcommand with the PID of the server daemon. It works as it should but there are some discrepancies in my script concerning the output behavior.

我已经为我编写了一个脚本来启动和停止我的 Perforce 服务器。要关闭服务器,我使用kill -SIGTERM带有服务器守护程序 PID的命令。它可以正常工作,但我的脚本中关于输出行为存在一些差异。

The script looks as follows:

该脚本如下所示:

#!/bin/sh -e

  export P4JOURNAL=/var/log/perforce/journal
  export P4LOG=/var/log/perforce/p4err
  export P4ROOT=/var/local/perforce_depot
  export P4PORT=1666

  PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"

  . /lib/lsb/init-functions

  p4start="p4d -d"
  p4stop="p4 admin stop"
  p4user=perforce

  case "" in
  start)
    log_action_begin_msg "Starting Perforce Server"
    daemon -u $p4user -- $p4start;
    echo "\n"
    ;;

  stop)
    echo "BLABLA"
    echo "$(pidof /usr/local/bin/p4d)"
    #daemon -u $p4user -- $p4stop;
    p4dPid="$(pidof /usr/local/bin/p4d)"
    echo $p4dPid
    if [ -z "$(pidof /usr/local/bin/p4d)" ]; then
      echo "ERROR: No Perforce Server running!"
    else
      echo "SUCCESS: Found Perforce Server running!\n\t"
      echo "Shutting down Perforce Server..."
      kill -15 $p4dPid;
    fi
    echo "\n"
    ;;

  restart)
    stop
    start
    ;;

  *)
    echo "Usage: /etc/init.d/perforce (start|stop|restart)"
    exit 1
    ;;

esac

exit 0

When p4dis running the stopblock works as intended, but when there is no p4drunning the script with stoponly outputs BLABLAand an empty new line because of the echo "$(pidof /usr/local/bin/p4d)". The error message stating that no server is running is never printed. What am I doing wrong here?

p4d运行stop块的工作如预期,但在没有p4d与运行该脚本stop只输出BLABLA因为和一个空的新的生产线echo "$(pidof /usr/local/bin/p4d)"。永远不会打印说明没有服务器正在运行的错误消息。我在这里做错了什么?

PS: The part if [ -z "$(pidof /usr/local/bin/p4d)" ]; thenhas been changed from if [ -z "$p4dPid" ]; thenfor debug reasons.

PS:由于调试原因,部分if [ -z "$(pidof /usr/local/bin/p4d)" ]; then已更改if [ -z "$p4dPid" ]; then

EDIT: I narrowed down the problem. If I don't use the p4dPidvariable and comment out the lines p4dPid="$(pidof /usr/local/bin/p4d)"and echo $p4dPidthe ifblock is processed and the error messages is printed. Still I don't unterstand what is causing this behavior.

编辑:我缩小了问题的范围。如果我不使用该p4dPid变量并注释掉这些行p4dPid="$(pidof /usr/local/bin/p4d)",则会处理echo $p4dPidif块并打印错误消息。我仍然不明白是什么导致了这种行为。

EDIT 2: Problem solved!

编辑2:问题解决了!

The -ein #!/bin/sh -ewas causing the shell to exit the script after any statement returning a non-zero return value.

-e#!/bin/sh -e是造成外壳返回一个非零返回值的任何语句后退出脚本。

回答by Alexey Ivanov

When your service is not running, the command

当您的服务未运行时,命令

echo "$(pidof /usr/local/bin/p4d)"

is processed as

被处理为

echo ""

because pidofdid not return any string. So the command outputs an empty line.

因为pidof没有返回任何字符串。所以该命令输出一个空行。

If you do not want this empty line, then just remove this statement, after all you print an error message when the process is not running.

如果您不想要此空行,则只需删除此语句,毕竟在进程未运行时打印错误消息。

回答by alexfr

Problem solved!

问题解决了!

The -ein #!/bin/sh -ewas causing the shell to exit after any statement returning a non-zero return value.

-e#!/bin/sh -e返航非零返回值的任何语句后使shell退出。