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:
- && : 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@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.
You should follow me on twitter here or grab rss feed to keep track of new changes.
Featured Articles:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012

- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop













{ 6 comments… read them below or add one }
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 ??
Use ps and jobs commands
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@192.168.1.16 today || echo “Command failed”
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???????????/
There is an error in the article.
You have to wrap the command in quotes like this :
$ ssh user@ras.nixcraft.in ‘today ; echo $?’
Additional note:
After trying this one. i found out that if ssh cannot resolve the host, the exit status is 255.