Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have this PHP code:

$execout=exec('ssh root@xxx.xxx.xxx.xx "sudo /etc/init.d/smokeping reload"',$output1,$result);
if($result !=0){
    echo"that can't reload";
}
else{
    echo "successfully reloaded";
}

in this code, always gives the $result = 0. Why is that? Sometimes if smokeping has an error, it shows that error when bellow command typed in command line,

ssh root@xxx.xxx.xxx.xx "sudo /etc/init.d/smokeping reload"

then it gives this error messege.

* Reloading latency logger daemon configuration...
ERROR: can't open /etc/smokeping/devices/errorfilename: No such file or   directory
 ...done.

so it has an error, but $result every times gives the same as 0 . (Whether it had an error or not)

Why does the exec command behaves like this(in php and CLI)?

share|improve this question

2 Answers 2

In

ssh root@xxx.xxx.xxx.xx "sudo /etc/init.d/smokeping reload"

Your shell

(1) (or in your example the shell started by PHP's exec()) will parse that command line and if found execute a ssh

(2) Command which will contact a sshd

(3) Server on xxx.xxx.xxx.xx which after successful authentication will call a shell

(4) Which will interpret that sudo command line and run the sudo

(5) Command which after checking permissions will run /etc/init.d/smokeping

(6) Which itself is probably a shell script that runs several commands.

Several things may fail there. If all the steps one to 5 are successful, then the exit status of /etc/init.d/smokeping will be reported to your shell because sudo does report the exit status of the command it runs and the remote shell exits with the exit status of the last command it ran and ssh reports the exit status of the remote shell.

Now, commands return a non-zero exit status, by convention, to report to the caller that they've not managed to do what they were asked to do.

In your case, either /etc/init.d/smokeping decided that whatever error occurred was not enough to warrant a non-zero exit status, or that script is not written correctly and fails to return with a non-zero exit status upon failure (or some pathological case involving misbehaviour or misconfiguration of ssh, sudo or the remote shell).

/etc/init.d scripts are often written with set -e. With that flag enabled, the shell interpreting the script will by default exit when a command it runs fails (with the non-zero exit status of the failing command), which makes me think here we may be in the first situation: "smokeping" does decide to report that it has successfully reloaded.

An example of a pathological case could be for instance: bash used as the remote shell (bash does read the ~/.bashrc when invoked over ssh even though those are not interactive shells), and the ~/.bashrc has something like:

trap 'whatever...; exit 0' EXIT

Which would cause the exit status of interactive shells or shells started over ssh or rsh to always be 0.

share|improve this answer
    
In OPs case, wouldn't successful execution of ssh return 0 regardless of what happens on the remote end? –  Mel Boyce Jun 27 '13 at 12:41
1  
@MelBoyce, that would only happen in pathological cases, ssh is meant to return the exit status of the remote shell. –  Stéphane Chazelas Jun 27 '13 at 12:56
    
@Stephane Chazelas-Thank you very much.And you explain that very clear. –  Thusitha Sumanadasa Jun 28 '13 at 4:54
up vote 0 down vote accepted

I found a little answer for that.for the above code I put 2>&1 at the end. As

$execout=exec('ssh root@xxx.xxx.xxx.xx "sudo /etc/init.d/smokeping reload 2>&1"',$output1,$result);

When there is an error occurs in the application that I'm going to reload(smokeping), print_r($output); gives the error as

Array ( [0] => * Reloading latency logger daemon configuration... [1] => ERROR: can't open /etc/smokeping/devices/errorfilename: No such file or directory [2] => ...done. )

Now I can handle it from here.Thank you all .

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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