Rundeck 不同的输出 运行 脚本

Rundeck different output running scripts

感谢您花时间阅读这个问题。

我在 Rundeck 中遇到 运行 脚本问题。有以下例子:

#!/bin/bash

SERVICE=$(whereis -b service | awk '{ print  }')
MDBC="/etc/mongod.conf"
CHECK=$(ps axu | grep mongod | grep -v grep | wc -l)

if [ $CHECK -eq 0 ]; then
  echo "Restarting MongoDB"
  $(which mongod) -f $MDBC 
  if [ $? -ne 0 ]; then 
    echo "Restart failed. Trigger this job manually."
  else 
    echo "Service restarted."; fi
else
  echo "Service is up and running!"; fi

运行 在服务器本地,给出预期的输出:

sudo bash -x test.sh 
++ whereis -b service
++ awk '{ print  }'
+ SERVICE=/sbin/service
+ MDBC=/etc/mongod.conf
++ grep -v grep
++ wc -l
++ grep mongod
++ ps axu
+ CHECK=0
+ '[' 0 -eq 0 ']'
+ echo 'Restarting MongoDB'
Restarting MongoDB
++ which mongod
+ /usr/bin/mongod -f /etc/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 84141
child process started successfully, parent exiting
+ '[' 0 -ne 0 ']'
+ echo 'Service restarted.'
Service restarted.

运行 与 Rundeck 中的 script 选项相同的代码,产生以下内容:

++ awk '{ print  }'
++ whereis -b service
+ SERVICE=/sbin/service
+ MDBC=/etc/mongod.conf
++ grep -v grep
++ ps axu
++ wc -l
++ grep mongod
+ CHECK=3
+ '[' 3 -eq 0 ']'
+ echo 'Service is up and running!'
Service is up and running!

如您所见,在第一个输出中,变量 CHECK 的结果等于 0,因为没有 MongoDB 个进程 运行.

第二个输出取 3 CHECK 的值,因此,if 条件立即退出。

我的Rundeck版本是开源版本3.3.5

有什么建议吗?

测试您的脚本,ps axu | grep mongodb | grep -v grep | wc -l 行似乎不是验证进程是否启动的最佳方式,例如,运行 行本身就像一个魅力,但称为从 rundeck 脚本步骤,创建另一个名称为“mongodb”的进程 (bash) 并像这样引用(生成两行并始终退出):

user      112156  0.0  0.0   2596   752 pts/1    S+   18:09   0:00 sh myscript.sh
user      112166  0.1  0.2 754928 48196 pts/1    Sl+  18:09   0:00 /path/to/mongodb/binary

也许识别您的服务以重新启动它的最佳方式是(当然您可以改进它):

#!/bin/bash

service docker status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
    echo "restarting docker"
    sudo service docker restart > /dev/null
    echo "docker restarted"
else
    echo "docker service is running"
fi

在 Rundeck 上看起来像这样 job definition:

<joblist>
  <job>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>927d0085-c4f8-45c9-ba2c-01575b167c76</id>
    <loglevel>DEBUG</loglevel>
    <name>RestartService</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <fileExtension>.sh</fileExtension>
        <script><![CDATA[#!/bin/bash

service docker status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
    echo "restarting docker"
    sudo service docker restart > /dev/null
    echo "docker restarted"
else
    echo "docker service is running"
fi]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
    </sequence>
    <uuid>927d0085-c4f8-45c9-ba2c-01575b167c76</uuid>
  </job>
</joblist>

如果您将服务设置为选项更好:

<joblist>
  <job>
    <context>
      <options preserveOrder='true'>
        <option name='service' value='docker' />
      </options>
    </context>
    <defaultTab>nodes</defaultTab>
    <description></description>
    <executionEnabled>true</executionEnabled>
    <id>927d0085-c4f8-45c9-ba2c-01575b167c76</id>
    <loglevel>DEBUG</loglevel>
    <name>RestartService</name>
    <nodeFilterEditable>false</nodeFilterEditable>
    <plugins />
    <scheduleEnabled>true</scheduleEnabled>
    <sequence keepgoing='false' strategy='node-first'>
      <command>
        <fileExtension>.sh</fileExtension>
        <script><![CDATA[#!/bin/bash

service @option.service@ status | grep 'active (running)' > /dev/null 2>&1

if [ $? != 0 ]
then
    echo "restarting @option.service@"
    sudo service @option.service@ restart > /dev/null
    echo "@option.service@ restarted"
else
    echo "docker service is running"
fi]]></script>
        <scriptargs />
        <scriptinterpreter>/bin/bash</scriptinterpreter>
      </command>
    </sequence>
    <uuid>927d0085-c4f8-45c9-ba2c-01575b167c76</uuid>
  </job>
</joblist>

这两个作业都经过测试并且有效,请随意使用或修改它们。

当然,要重新启动进程,您需要 sudo 提升和 this plugin would be useful if the service coexists with Rundeck at the same server (it's a node executor / file copier) or check this 如果它是关于外部服务器的。