2

I have a PHP CLI daemon which is sending files via SFTP regularily to a flaky server, and before I fix the server I want to implement error handling in my script.

After logging in succesfully, I run this command and it works about 70% of the time:

 if ($sftp->put("/home/whatever/$name","$local_path")) 
{ 
    print "no problem\n";
} else {
    print "is problem\n";
}

I don't care if connection doesn't work, because I can make my daemon try again, BUT the problem is that I get these errors and then my daemon just dies:

PHP Notice:  Connection closed by server in /usr/share/pear/Net/SSH2.php on line 1009

Notice: Connection closed by server in /usr/share/pear/Net/SSH2.php on line 1009

the "is problem" message never appears, program just dies.

How can I detect when I get connection close without script dying?

Cocorico
  • 2,319
  • 3
  • 22
  • 31
  • 1
    Have you tried setting an error handler? – Devon Bessemer May 12 '15 at 02:49
  • I did try and maybe I did it wrong cause I could get a message printed but the script still stopped. This was my code for error handling set_error_handler("on_error"); function on_error($num, $str, $file, $line) { print "blah blah"; } – Cocorico May 12 '15 at 03:01
  • I think what I did was convert phpseclib to throw exceptions. You could try throwing an exception in on_error and using a try catch block for transfer. I'm not sure if that would get around it or not. – Devon Bessemer May 12 '15 at 03:03
  • Do you mean converting phpseclib as in actually altering the phpseclib library? I am shady on exceptions and stuff, maybe I should read up on this again!! – Cocorico May 12 '15 at 03:06
  • Yeah, I modified the actual source from github when I used phpseclib, but I think you could throw exceptions in the error callback. A PHP notice shouldn't cause the script to end though, so I'm not sure the exact problem you are having. I think it is just easier to use try catch blocks for this. – Devon Bessemer May 12 '15 at 03:08
  • I feel also like I can solve this with php.ini or something. – Cocorico May 12 '15 at 03:10
  • 1
    oh no it looks like it's actually doing it fine now, it is dying because there was a line from example code where it would die if you cou;ldn't log in, okay things are fine – Cocorico May 12 '15 at 03:14
  • Lol, okay, probably just delete this one then.... – Devon Bessemer May 12 '15 at 03:15

0 Answers0