About nixCraft

How do I find the exit status of a remote command executed via ssh?

Posted by Vivek Gite [Last updated: February 5, 2006]

After writing about "Execute Commands on Multiple Linux or UNIX Servers", I received couple of emails asking about "how to find out the exit status of a remote command executed via ssh command." Well I never ever used exit status of a remote command executed via ssh. However, here is simple way to find out exit status:

$ ssh user@ras.nixcraft.in date;echo $?

Sun Feb  5 19:01:01 IST 2006
0

First line is nothing but output of date command, zero (0) is exit status of date command. Try another command

$ ssh user@ras.nixcraft.in today;echo $?

bash: today: command not found
127

Please note that ssh exits with the exit status of the remote command or with 255 if an error occurred. For example:

$ ssh user@ras.nixcraft.in today
$ echo $?

bash: today: command not found
127

Since today is not a valid command bash exited with 127 exit code. However, you cannot use conditional control operator:

So following command will never display Command failed message:

$ ssh root@192.168.1.16 today;echo $? || echo "Command failed"

What you can do is create a shell script wrapper that will execute a remote command and returns the status locally. So if you are using scripts then last line will be always using to determine the exit status of a remote command executed via ssh.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Jafar Says:

    Suppose I put a job to run on remote server (in the background and logged. How can I check later whether the job is done or not.
    Suppose if i know the Job ID ??

  2. vivek Says:

    Use ps and jobs commands

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.