I was trying to execute a php file thorugh php with shell_exec, this was the code:
$ex="php -f ".rtrim(dirname(__FILE__))."/sendmail.php";
if(substr(php_uname(), 0, 7) == "Windows")
pclose(popen("start /B ".$ex,"r"));
else
shell_exec($ex." > /dev/null 2>/dev/null &");
The problem is that it didn't work on my server(I have even tried without the -f), but this is ok:
$ex="php-cli ".rtrim(dirname(__FILE__))."/sendmail.php";
if(substr(php_uname(), 0, 7) == "Windows")
pclose(popen("start /B ".$ex,"r"));
else
shell_exec($ex." > /dev/null 2> /dev/null &");
Can somebody tell me why it works? Does php-cli even exists?
Thanks in advance
OS doesn't matter. You should be able to use shell_exec if it's not disabled in your php.ini
Related
Im trying to run the function
execInBackground('start cmd.exe #cmd /k "ping google.com"');
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
on my vps but it's not working,
but when I run it exactly on my local pc in works fine, cmd open and run "ping google.com" ,what is making it not working on my vps? I added php to enviroment variables too
I try to execute a .php script with the shell_exec() function on a 1&1 Linux shared Webserver (debian GNU/Linux 8 distribution).
private function callAsyncSkript(){
// Asynchron Zipping over executing a Script in a new process.
if (substr(php_uname(), 0, 7) == "Windows"){
$cmd = "start /B php .\src\Core\Services\AsynchronZip.php fileids=".$this->idQuery . " fulllink=".$this->uri;
pclose(popen($cmd, "r"));
}
else {
$cmd = "/usr/bin/php7.2-cli ".getcwd()."/src/Core/Services/AsynchronZip.php fileids=".$this->idQuery . " fulllink=".$this->uri;
shell_exec("/usr/bin/nohup ".$cmd." >/dev/null 2>&1 &"." | at now");
//shell_exec("/usr/bin/nohup ".$cmd." >/dev/null 2>&1 &");
}
}
The "/usr/bin/nohup " addition donĀ“t help to execute the script asynchron,
if iam using " | at now" the script will not be executed.
Are there some special settings i have to set or are there other options for an asynchron execution over the commandline?
I found out that the only shell available to me is a rssh. Is it possible that she is the mistake.
The following code is working on localhost xampp windows, but when i put it online godaddy shared linux hosting its not working. exec is working as i tries exec('whoami') and it outputs my account id, but the following exec command is not executing.
is there any changes i need to make it work? please guide.
$cmd = 'php file.php';
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
PHP exec are not supported on share hosting accounts: http://support.godaddy.com/groups/web-hosting/forum/topic/is-php-exec-available-or-executable/
Hey there! I want to restart a live stream by hand via a php script. Everything works fine so far, but the following command causes that the script loads forever and the transcoding isn't working:
nohup openRTSP -v -c rtsp://*****.dyndns.org:665 | ffmpeg -r 5 -f mjpeg -i - http://127.0.0.1:8090/feed1.ffm > /dev/null &
Any ideas how to start that command e.g. without waiting for the output?
Not that it will necessarily solve your problem, but it should answer your question, found in the PHP comments under "exec" in which several people came across similar situations.
I combined several efforts in this
topic into one function: This will
execute $cmd in the background (no cmd
window) without PHP waiting for it to
finish, on both Windows and Unix.
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
I think there are tons of similar posts but I haven't yet found a solution after searching around.
Basically, I'm trying to run two scripts in the background. When I run them in the commandline, I see after calling my first script:
/usr/bin/nohup php script.php > nohupoutput.log & echo $!
I've tried ...script.php > /dev/null & with the same result. I get:
/usr/bin/nohup: ignoring input and redirecting stderr to stdout
which I ignore and run the second one. I noticed that it seemed to be hanging there, and pressing Enter brought me back to machine:~folder>
/usr/bin/nohup php script2.php > nohupoutput.log & echo $!
Both scripts work. I tried to then convert this to a shell_exec command and nothing seems to work. I suspect that the ignoring input bit is causing difficulties, but I'm not sure. Regardless, the following does not work. It just hangs in the browser:
$output = shell_exec('/usr/bin/nohup php script.php > /dev/null &');
$output = shell_exec('/usr/bin/nohup php script2.php > /dev/null &');
Try:
$output = shell_exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
Or:
exec('/usr/bin/nohup php script.php >/dev/null 2>&1 &');
This shoul work:
shell_exec('nohup /usr/bin/php path/to/script.php > output.txt &');
<?php
function execInBackground($cmd) {
if (substr(php_uname(), 0, 7) == "Windows"){
pclose(popen("start /B ". $cmd, "r"));
}
else {
exec($cmd . " > /dev/null &");
}
}
// take note: to get your PHP_PATH, try looking at your phpinfo :)
echo execInBackground("/usr/local/php53/bin/php 'example2.php'");
?>
First put your php command in a shell file script, e.g. myscript.sh:
#!/bin/bash
# myscript.sh file
php script.php
Run nohup with myscript.sh:
sudo nohup ./myscript.sh &
Verify with ps:
ps aux | grep myscript.sh