简体   繁体   English

使用与 SSH 一起使用的 shell_exec 命令执行后台 php 脚本

[英]Execute background php script with shell_exec command that works with SSH

The problem I am struggling for a few hours is: I want to execute a script in background with PHP, so I'm using shell_exec() function, and I can't get it work.我苦苦挣扎了几个小时的问题是:我想用 PHP 在后台执行一个脚本,所以我使用的是 shell_exec() function,但我无法让它工作。 But is not a problem related with shell_exec function because I have tested the shell_exec('sleep 5') command and it worked.但这不是与 shell_exec function 相关的问题,因为我已经测试了 shell_exec('sleep 5') 命令并且它有效。 The command Im trying to execute is:我试图执行的命令是:

shell_exec('nohup /kunden/usr/local/bin/php5.4 /kunden/homepages/XX/dXXXXXXXX/htdocs/myproject/background_script.php > /dev/null 2>&1 & echo $!');

The script inserts one row in a MySQL database.该脚本在 MySQL 数据库中插入一行。 I have tested the exact same command inside the quotes via SSH with putty and it worked perfectly, but i can't get it work from the web browser.我已经通过 SSH 和 putty 测试了引号内完全相同的命令,它运行良好,但我无法从 web 浏览器中运行它。

I don't think is important, but is a shared server with 1and1.我认为不重要,但它是 1and1 的共享服务器。

According to the original poster: 根据原始海报:

Finally I used the library phpseclib and now I have it working. 最后,我使用了库phpseclib,现在我可以使用它了。

 $ssh = new Net_SSH2('myhosting'); if (!$ssh->login('user', 'pass')) { exit('Login Failed'); } $this->pid = $ssh->exec(sprintf( '%s > %s 2>&1 & echo $!', $this->command, $outputFile )); 

it works for me:这个对我有用:

$connection = ssh2_connect('host', 22);
ssh2_exec($connection, "screen -dmS 'php-script' /kunden/usr/local/bin/php5.4 /kunden/homepages/XX/dXXXXXXXX/htdocs/myproject/background_script.php 2>&1");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM