Bash 脚本中的 pidof 问题

发布于 2024-11-29 06:30:29 字数 1811 浏览 1 评论 0原文

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

该脚本如下所示:

#!/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 "$1" 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

p4d 运行时,stop 块按预期工作,但是当没有 p4d 时,使用 运行脚本>stop 仅输出 BLABLA 和一个空的新行,因为 echo "$(pidof /usr/local/bin/p4d)"。永远不会打印指出没有服务器正在运行的错误消息。我在这里做错了什么?

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

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

编辑2:问题解决了!

#!/bin/sh -e 中的 -e 导致 shell 在任何返回非零返回值的语句后退出脚本。

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

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 "$1" 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 p4d is running the stop block works as intended, but when there is no p4d running the script with stop only outputs BLABLA and 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?

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

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

EDIT 2: Problem solved!

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅语花开 2024-12-06 06:30:29

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

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

将被处理,

echo ""

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

如果您不想要这个空行,那么只需删除此语句即可,毕竟您在进程未运行时会打印错误消息。

When your service is not running, the command

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

is processed as

echo ""

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

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.

最笨的告白 2024-12-06 06:30:29

问题解决了!

#!/bin/sh -e 中的 -e 导致 shell 在任何返回非零返回值的语句后退出。

Problem solved!

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文