Issue
Take the following Python code as an example when connecting via SSH into a Debian server:
stdin, stdout, stderr = ssh.exec_command('sudo systemctl stop tomcat')
if stdout.channel.recv_exit_status() != 0:
for line in iter(stderr.readline, ""):
print(line)
stdin, stdout, stderr = ssh.exec_command('sudo systemctl status tomcat')
if stdout.channel.recv_exit_status() != 0:
for line in iter(stderr.readline, ""):
print(line)
stdout.channel.recv_exit_status() for the second SSH commands returns 3 - whatever that means. Is it the SSH return code? The Debian host return code? The documentation suggests the latter: http://docs.paramiko.org/en/stable/api/channel.html ("the exit code (as an int) of the process on the server.")
Rather interestingly, if I start Tomcat with the same command and then call the status of the service, it returns 0, thus leading me to believe I for whatever reason can't get the status of Tomcat when it's stopped. Yet if I SSH into the server myself, the status command works regardless if Tomcat is started or not.
Any help with this would be greatly appreciated.
Solution
systemctl status tomcat
will return 3 as an exit code if tomcat is stopped, so this behaviour is normal.
systemctl status <service>
's exit code will indicate the status of the requested service, rather than the success of the actual check.
If you run systemctl status tomcat
when tomcat is stopped, then run echo $?
, you'll see that the exit code is 3. If tomcat is running, it should return 0.
systemctl status
follows LSB spec in this regard -- the full list of possible exit codes is here: https://refspecs.linuxbase.org/LSB_3.1.1/LSB-Core-generic/LSB-Core-generic/iniscrptact.html.
Answered By - Joseph Redfern Answer Checked By - Terry (WPSolving Volunteer)