[英]How to execute remote SSH command in background using PHP exec()?
Is there a simple way to execute SSH commands in the background on remote machines from PHP without using ssh2_*
? 有没有一种简单的方法可以在后台使用PHP在远程计算机上执行SSH命令而不使用
ssh2_*
? The PHP script is executed by the user from bash (no Apache involved), so it's not an issue of rights. PHP脚本是由用户从bash(不涉及Apache)执行的,因此这不是权限问题。 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 user@192.168.0.19 '/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. 所有PHP变量都已通过
escapeshellarg()
进行了转义,并且$remoteCommand
是远程计算机上的bash脚本,该脚本会休眠几分钟,然后开始执行一些命令。
My problem is that if I execute that SSH command from bash, it gives control back to bash immediately. 我的问题是,如果我从bash执行该SSH命令,它将立即将控制权交还给bash。 If I execute it from php using
exec()
it waits until the remote command executes. 如果我使用
exec()
从php执行它,它将等待直到远程命令执行为止。 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. 我尝试在
/home/user/script.sh
之后添加2>&1 >/dev/null </dev/null
,但是执行仍然不会将控制权返回给PHP脚本。
我认为您在将执行发送到后台的命令末尾缺少& 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.