0

Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. I've tried doing this:

exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i {$keyFile} {$user}@{$ip} {$remoteCommand} 2>&1 >/dev/null </dev/null");

For example:

exec("ssh -f -o UnknownHostsFile=/dev/null -o StrictHostKeyChecking=no -i /home/data/id_rsa [email protected] '/home/user/script.sh ; exit' 2>&1 >/dev/null </dev/null");

All PHP variables have been escaped with escapeshellarg() and $remoteCommand is a bash script on the remote machine that sleeps for a few minutes and then starts executing some commands.

My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. If I execute it from php using exec() it waits until the remote command executes. I've tried adding 2>&1 >/dev/null </dev/null after /home/user/script.sh, but the execution still doesn't return control to the PHP script.

5
  • I don't understand the problem with bash. Don't you want it to give control back to bash immediately? Aren't you trying to execute in the background? Commented Dec 13, 2011 at 10:19
  • If you don't mind elaborating, why don't you want to use ssh2_*? Commented Dec 13, 2011 at 10:20
  • @davidethell Yes, I do, but it doesn't seem to work. Commented Dec 13, 2011 at 10:21
  • @favoretti I have used it before and it is something I would like to avoid. Commented Dec 13, 2011 at 10:22
  • Have you seen this? stackoverflow.com/questions/45953/… Commented Dec 13, 2011 at 10:31

1 Answer 1

1

I think you are missing an & at the end of your command for sending the execution to the background.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I was missing that, but it still didn't help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.