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

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 date;echo $?

Sun Feb  5 19:01:01 IST 2006

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

$ ssh today;echo $?

bash: today: command not found

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

$ ssh today
$ echo $?

bash: today: command not found

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

  • && : Execute command only if command returns an exit status of zero
  • || : Execute command only if command returns an exit status of non zero

So following command will never display Command failed message:

$ ssh root@ 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.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Jafar Mar 23, 2008 @ 8:56

    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 ??

  • 🐧 nixCraft Mar 23, 2008 @ 9:53

    Use ps and jobs commands

  • Greg Wells Oct 24, 2008 @ 19:33

    You can use conditional control operators if you omit the echo $? command. The conditional operator evaluates the exit status from the echo $? command which should always be 0.

    This version should work:

    $ ssh root@ today || echo “Command failed”

  • vishal Nov 27, 2008 @ 15:13

    I am using remsh to execute remote command.
    How to obtain exit status of remotely executed command .In remote program i am using exit(number) to differinciate b/w exit status.But all time i get exit status as 0.
    Even if i use abort() the exit status is 0.
    SOme one help???????????/

  • Olivier Feb 10, 2011 @ 21:11

    There is an error in the article.
    You have to wrap the command in quotes like this :
    $ ssh ‘today ; echo $?’

  • Leonel May 19, 2011 @ 7:35

    Additional note:
    After trying this one. i found out that if ssh cannot resolve the host, the exit status is 255.

  • Shane Holder Jun 21, 2013 @ 20:04

    Um, I think the reason that

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

    doesn’t work is because the echo $? changes the exit status, not because of weirdness with SSH.

  • Rahul Jul 23, 2013 @ 12:43

    @Vivek / all
    I want something like

    ssh ” [ -e /some/path ] ”
    and then $? = 0 then print file exists

    “this is my first comment in your site, Jus wanna say you are doing a nice job”

  • zaid Ahmed Jan 23, 2014 @ 8:24

    how can we pass the parameter to file which is also in command with ssh for remote execution

  • Daniele Salvatore Albano Apr 17, 2014 @ 12:51

    Just do
    (ssh “command_does_not_exists” >/dev/null 2>&1) || echo “Command doesn’t exists! HOSTNAME: $(hostname)”

    you can pass -i /path/to/private-key to authenticate to the remote machine using an ssh key if you don’t want to use ssh-agent or other stuff like that (useful when doing remote automations, you can use in rsync too with -e or in git using a simple wrapper)

  • pranav Mar 30, 2017 @ 10:47

    It’s simple:
    ext_status=`ssh ‘today; echo $?’`
    if [ $ext_status -eq 0] then
    echo “execution failed”

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum