I am writing a script that logs into a remote node via SSH and checks for a service being up by saving the output of pgrep -a <service-name> , storing it in a variable and checking if that variable is UP or not.
HOST="172.29.219.110"
COMMAND="pgrep -a haproaxy"
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], shell=False, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result = ssh.stdout.readlines()
print ( result )
if result == "":
print ("The service is NOT running")
else:
print ("The service is running")
If I run the above as it is, I get the below response:
[b'31318 /usr/sbin/haproxy-systemd-wrapper -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid\n', b'31319 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds\n', b'31320 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid -Ds\n']
The service is running
But when I pgrep a bogus service, the response is
[]
The service is running
Ive checked on the remote server that pgrep -a haaaaaaaaa retuns nothing. But it doesnt seem to register as an empty variable in python. What is it that I am doing wrong ?
if not result. Orif result == [][]is simply not the same as"".if result: #good else: #bad