PHP execute command and log output without waiting - php

I use exec() to execute command, either linux or windows.
How do you execute a command, linux and windows, and log the output without waiting?
I know for linux, to not wait for the output: command* > /dev/null 2>/dev/null &
And to log output for linux: command* > /path/to/log.txt 2>/path/to/error.txt
How would you go about logging and setting it to background in one command? How would windows look like too?

On Linux you can do:
exec('command* > /dev/null 2>/dev/null &');
On Windows you can do:
pclose(popen('start /B cmd /C "command* >NUL 2>NUL"', 'r'));
Both examples disable output and errors, those go to /dev/null (linux) or NUL (windows) which means they are stored "nowhere".
You can replace these with valid paths on your system.
On Linux, a & at the end places it into background. On windows this is more complicated and needs start to invoke the process and cmd to allow redirection of the streams.

Related

Running PHP Script with Windows Task Scheduler

So I found some posts on here about running PHP CLI scripts in the Task Scheduler, and it works like a charm! However, when I normally run the script in command line I output the results to a .log file:
ScriptName.php -arg1 -arg2 > log.log 2>&1
This doesn't seem to work in the "Add arguments" section of the Task.
I have it where Program/script is: C:\PHP\php.exe my argument is ScriptName.php -arg1 -arg2 > log.log 2>&1
Is there something I should be doing different?
Programs run from Scheduled Tasks don't generate any standard output or standard error. You need to launch a new cmd instance:
cmd /C C:\PHP\php.exe ScriptName.php -arg1 -arg2 > log.log 2>&1
So as a workaround I will just run the script from a .bat file
PHP ScriptName.php -arg1 -arg2 > log.log
and go from there.

How do I run PHP's built-in web server in the background?

I've written a PHP CLI script that executes on a Continuous Integration environment. One of the things it does is it runs Protractor tests.
My plan was to get the built-in PHP 5.4's built-in web server to run in the background:
php -S localhost:9000 -t foo/ bar.php &
And then run protractor tests that will use localhost:9000:
protractor ./test/protractor.config.js
However, PHP's built-in web server doesn't run as a background service. I can't seem to find anything that will allow me to do this with PHP.
Can this be done? If so, how?
If this is absolutely impossible, I'm open to alternative solutions.
You could do it the same way you would run any application in the background.
nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &
Here, nohup is used to prevent the locking of your terminal. You then need to redirect stdout (>) and stderr (2>).
Also here's the way to stop built-in php server running in the background.
This is useful when you need to run the tests at some stage of CI:
# Run in background as Devon advised
nohup php -S localhost:9000 -t foo/ bar.php > phpd.log 2>&1 &
# Get last background process PID
PHP_SERVER_PID=$!
# running tests and everything...
protractor ./test/protractor.config.js
# Send SIGQUIT to php built-in server running in background to stop it
kill -3 $PHP_SERVER_PID
You can use &> to redirect both stderr and stdout to /dev/null (noWhere).
nohup php -S 0.0.0.0:9000 -t foo/bar.php &> /dev/null &

nohup >/dev/null alternative for windows

I'm running a php script which is not working properly in my windows OS but this is supposed to work in linux.
I figured it out that nohup is not an associated tool with windows.
$ffmpeg = 'C:\ffmpeg\bin\ffmpeg';
$command = "nohup >/dev/null 2>&1 ".$ffmpeg." -i {$input_path} {$ffmpeg_string} -stats -y {$output_path} 2> {$log_path} >/dev/null &";
exec( $command );
So what could be my best alternatives if I want to run this code on windows.
Detailed explanation will be greatly appreciated as I don't know much about background process.
nohup on windows, exec without waiting for finish
nohup >/dev/null 2>&1
The above command redirects everything from console to the null location, including the error log (2>&1).
on Windows this is done using echo off in batch.
Your question should probably be changed to "What is the equallent of nohup on Windows", it gives clarity.
i think this link gives you more information on your issue: What's the nohup on Windows?

Php exec ffmpeg not running in background

I am trying to start a ffmpeg process from a php script and I know it has been asked a lot of times but I tried many solutions and none of them seem to work, each time the php script never finishes unless I kill the ffmpeg process. At the moment I am using this script which indeed starts ffmpeg and writes info in the designated files but the php script is loading forever.
What am I missing?
$cmd = 'cd cache && ffmpeg -y -i "rtsp://stream" -r 20 -f image2 a%6d.jpg >/dev/null 2>/dev/null &';
exec(sprintf("%s > %s 2>&1 & echo $! >> %s", $cmd, 'log.txt', 'error.txt' . '.pid'));
A little more info: I am running FFMpeg 0.6.5, PHP 5.3.3 on CentOS 6.5
Thank you for your time!
You can use > /dev/null & instead of 2>&1 &
This will execute $cmd in the background without PHP waiting for it to finish.
Ref: http://php.net/manual/en/function.exec.php

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_*? 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 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.
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.
I think you are missing an & at the end of your command for sending the execution to the background.

Categories