Thanks for taking time to read this question.
I'm having issues running scripts into Rundeck. Having the following example:
#!/bin/bash
SERVICE=$(whereis -b service | awk '{ print $2 }')
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
Running locally in the server, gives the expected output:
sudo bash -x test.sh
++ whereis -b service
++ awk '{ print $2 }'
+ 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.
Running the same code as script option in Rundeck, produces the following:
++ awk '{ print $2 }'
++ 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!
As you can see, in the first output, the result for variable CHECK
is equal to 0, since no MongoDB processes are running.
The second output is taking as 3 the value for CHECK
, hence, the if
condition exits immediately.
My Rundeck version is the open source version 3.3.5
Any advices?
Testing your script it seems that the line ps axu | grep mongodb | grep -v grep | wc -l
isn't the best way to verify if the process is up, for example, running the line alone works like a charm, but called from a rundeck script step, another process (bash) with "mongodb" name is created and referenced like this (generating two lines and always exiting):
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
Maybe the best way to identify your service to restart it would be something like (of course you can improve it):
#!/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
Looks like this on a 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>
Better if you set the service as an option:
<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>
Both jobs tested and works, feel free to use or modify them.
Of course, to restart processes, you need sudo
elevation and 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 if it's about an external server.