I wanted to run a command in shell (basically via ansible playbook), the condition is, if the ps status gives non zero exit code then it needs to run another command. I don't wanted to split this into two tasks, one to check the ps status and another to execute the command. Instead using if/else condition, can this be merged into a single task?
ps -ef | grep -v grep | grep httpd
If the above command exits with non zero status, then I need to fire another command which is given below. How can I merge these two commands as one and include this as a single task?
nohup /root/scripts/httpd.sh start &
Best Answer
Solution
This does what you want:
NOTE: I'm using the command
sleep 1000
as a stand-in for your Apache daemon process,httpd
.Example Run
Notice we have neither a sleep or httpd process present:
Now we run the playbook:
Now we have a sleep process going:
Now if we reset things and do the same thing but this time open up a edit window with a filename of
httpd
to "fake out" thegrep [h]ttpd
:Running the playbook this time will do nothing:
Still just our edit window with the 'httpd' file open: