Let's say I want to do some test VIA ssh


ssh 2>/dev/null "smartctl -a /dev/sdb -q silent"

echo $?

in that case we get exit code 1

How to know if the problem is about ssh or about the smartctl command ?


The determination is difficult as many processes all use the same value.

% ssh 2>/dev/null localhost 'exit 255' ; echo $?
% ssh 2>/dev/null nopelocalhost 'exit 0' ; echo $?

With a heuristic you could guess which code is from which program based on commonly used exit codes; this would be mostly right, unless the programs involved overlap or something unexpected happens. Standard error may or may not be available and the exit code may change depending on how the program exits:

% ssh localhost ./segfault ; echo $?
% ./segfault ; echo $?
zsh: bus error  ./segfault

Thus it is not so reliable to guess at where $? came from. Instead, a better option may be to design a protocol that communicates more information than provided by the exit status word; examples would include Nagios or Ansible that communicate more than just $? when determining how a remote command ran. This could be as simple as a line of text that depends on how smartctl ran (or segfaulted, or ...) or something more complicated like a JSON structure with the standard out, err, exit status word, and other such metadata. So instead of running smartctl directly, you would instead call the wrapper program that runs smartctl and parses its output, and on the other side of ssh you would collect that output; if the output is not available, then something went wrong with the ssh, or with your wrapper program.


From manpages:

     ssh exits with the exit status of the remote command or with 255 if an error occurred.

If an error occured with ssh command it will return 255, otherwise it returns exit status of remote command.


$ ssh root@not.exists
ssh: Could not resolve hostname not.exists: Name or service not known
$ echo $?

In your case 1 is exit status of smartctl command, not ssh


smartctl exit statuses:

       The  exit  statuses  of  smartctl  are  defined by a bitmask.  If all is well with the disk, the exit status (return value) of smartctl is 0 (all bits turned off).  If a problem occurs, or an error,
       potential error, or fault is detected, then a non-zero status is returned.  In this case, the eight different bits in the exit status have the following meanings for ATA disks; some of these  values
       may also be returned for SCSI disks.

       Bit 0: Command line did not parse.

       Bit 1: Device open failed, device did not return an IDENTIFY DEVICE structure, or device is in a low-power mode (see '-n' option above).

       Bit 2: Some SMART or other ATA command to the disk failed, or there was a checksum error in a SMART data structure (see '-b' option above).

       Bit 3: SMART status check returned "DISK FAILING".

       Bit 4: We found prefail Attributes <= threshold.

       Bit 5: SMART status check returned "DISK OK" but we found that some (usage or prefail) Attributes have been <= threshold at some time in the past.

       Bit 6: The device error log contains records of errors.

       Bit 7: The device self-test log contains records of errors.  [ATA only] Failed self-tests outdated by a newer successful extended self-test are ignored.

exit status 1 on smartctl would mean bit 0 is on, since 1=2^0, so that command line didn't parse

  • as you can see we put 2>/dev/null after the ssh , to do silent mode – yael Nov 22 '18 at 12:45
  • 2
    @Yael 2> redirects stderr, not the exit status. – ctrl-alt-delor Nov 22 '18 at 12:50
  • 2
    that doen't supress exit status, just redirects error from terminal to /dev/null – rAlen Nov 22 '18 at 12:50
  • @Yael why don't you run some tests. Run true, false remotely, run ssh with error in arguments. – ctrl-alt-delor Nov 22 '18 at 12:52
  • not answered on my question ! , how to know if the problem is with ssh or with remote command ? – yael Nov 22 '18 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.