bash 如何检测我的新贵脚本是否正在运行?

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

How can I detect if my upstart script is running?

bashubuntuupstart

提问by Hoa

In pseudo code I'm trying to do something like the following

在伪代码中,我正在尝试执行以下操作

if myService is running
  restart myService
else
  start myService

How can I translate the above into a bash script or similar?

如何将上述内容翻译成 bash 脚本或类似脚本?

回答by David W.

The standard way is to use a PID file for storing the PID of the service. Then, you can use the PID stored in the PID file to see if the service is already running or not.

标准方式是使用PID文件来存储服务的PID。然后,您可以使用存储在 PID 文件中的 PID 来查看该服务是否已经在运行。

Take a look at the various scripts under the /etc/init.ddirectory and see how they use the PID file. Also take a look under /var/runin most Linux systems to see where the PID files are stored.

查看/etc/init.d目录下的各种脚本,看看它们是如何使用PID文件的。还可以查看/var/run大多数 Linux 系统中的 PID 文件的存储位置。

You can do something like this which is a generic way of handling this for all Bourne shell type shells:

你可以做这样的事情,这是处理所有 Bourne shell 类型 shell 的通用方法:

# Does the PID file exist?

if [ -f "$PID_FILE" ]
then
    # PID File does exist. Is that process still running?
    if ps -p `cat $PID_FILE` > /dev/null 2&1
    then

       # Process is running. Do a restart
       /etc/init.d/myService restart
       cat $! > $PID_FILE
    else
       # Process isn't' running. Do a start
       /etc/init.d/myService start
       cat $! > $PID_FILE
else
   # No PID file to begin with, do a restart
   /etc/init.d/myService restart
   cat $! > $PID_FILE
fi

However, on Linux, you can take advantage of pgrep:

但是,在 Linux 上,您可以利用pgrep

if pgrep myService > /dev/null 2>&1
then
    restart service
else
    start service
fi

Note how you do not use any braces. The ifstatement operates on the exit status of the pgrepcommand. I'm outputting both STDOUT and STDERR to /dev/null because I don't want to print them. I just want the exit status of the pgrepcommand itself.

请注意您如何不使用任何大括号。该if语句对pgrep命令的退出状态进行操作。我将 STDOUT 和 STDERR 都输出到 /dev/null 因为我不想打印它们。我只想要pgrep命令本身的退出状态。

READ THE MANPAGEON PGREP

阅读PGREP 的手册页

There are quite a few options. For example, you might want to use -xto prevent unintended matches, or you might have to use -fto match on the full command line used to start the service.

有很多选择。例如,您可能希望使用-x来防止意外匹配,或者您可能必须-f在用于启动服务的完整命令行上使用来匹配。

回答by Alex Oliveira

If you see myServicewhen you run a ps aux, then you could simply do this in bash (edited to use pgrep, as jordanm suggested):

如果您在运行ps aux时看到myService,那么您可以简单地在 bash 中执行此操作(编辑为使用 pgrep,正如 jordanm 建议的那样):

if [ $(pgrep myService) ]; then
    restart myService;
else
    start myService;
fi