When I try to run shell_exec("php -S localhost:8000"), it runs the server but it freezes the terminal.
I tried running $result = shell_exec('php -S localhost:8000 -t public/ &> /dev/null 2>&1') but it doesn't store the output to the variable.
The idea is so that I can customize the output message once server successfully boots up.
Related
I have a condition where I have to start iperf server as daemon on specified port and if iperf server is running, I have to send response to client. I tried
shell_exec('iperf -s -p {port} -D');
but it doesn't return control / infinite loop starts.
The server will start but the code below the shell_exec will never be executed.
Anyone has a solution or suggestion how I should approach this to get the result?
Your command iperf -s -p {port} -D happens to have stderr output, try doing this:
$outfile = "/tmp/erroutperf.out";
$port = 8080;
shell_exec("iperf -s -p $port -D > $outfile 2>&1");
basically the additional command > /tmp/erroutperf.out 2>&1, tells bash to save
both stderr output and stdout of a program (iperf) to a file /tmp/erroutperf.out
getting the output of the command is:
file_get_contents($outfile);
Here is my code:
$pdf = '/Users/macbookpro/Desktop/q.pdf';
$swf = '/Users/macbookpro/Desktop/q.swf';
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
exec($command2,$out,$status);
var_dump($output);
The output is NULL and no SWF is generated. However, if I output the command and copy it to terminal, it works. How do I solve this?
exec runs as the user running the script. Apache user likely doesn't have the PATH variable telling it where to look for programs, so instead of
$command2 = 'pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
Try adding the location of pdf2swf, something like:
$command2 = '/bin/pdf2swf -o '.$swf.' -T -z -t -f '.$pdf.' -s flashversion=9';
And make sure that the apache user has permission to get to the executable, and permission to execute it.
chmod a+x /bin/pdf2swf
Of course replace /bin/ with where ever pdf2swf really lives for all the example code in this answer.
I am attempting to launch sar and have it run forever via a php script. But for whatever reason it never actually launches. I have tried the following:
exec('sar -u 1 > /home/foo/foo.txt &');
exec('sar -o /home/foo/foo -u 1 > /dev/null 2>&1 &');
However it never launches sar. If I just use:
exec('sar -u 1')
It works but it just hangs the php script. My understanding that if a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream.
I will assume your running this on a *nix platform. To get php to run something in the background and not wait for the process to finish I would recommend 2 things: First use nohup and also redirect the output of the command to /dev/null (trash).
Example:
<?php
exec('nohup sar -u 1 > /dev/null 2>/dev/null &');
nohup means we do not send the "hang up" signal (which kills the process) when the terminal running the command closes.
> /dev/null 2>/dev/null & redirects the "normal" and "error" outputs to the blackhole /dev/null location. This allows PHP to not have to wait for the outputs of the command being called.
On another note, if you are using PHP just to call a shell command, you may want to consider other options like Ubuntu's Upstart with no PHP component--if you are using Ubuntu that is.
I am trying to execute a command with exec() and redirecting stdout and stderr to a file.
exec("nohup python main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 &");
It will create the file but it will not print anything to it.
If I run the command in a terminal everything outputs without a problem.
Got it working. Python does its own output buffering which kept it from writing to the file. Running it with the -u option disables this. Final code looks like this:
exec("nohup python -u main.py -i 1 > /var/scripts/logs/1_out.log 2>&1 </dev/null &");
Thanks.
Related: see here
I've got this command:
exec("(wget -O http://domain/file.zip && mysql -u user -ppassword database -e \"UPDATE \\`table\\` SET \\`status\\` = 'live' WHERE \\`id\\` = '1234'\") & echo \$!");
The above command works fine, however PHP waits for the video to finish downloading before going on to the next download. The following line, however, sends the download to the background, which is what I'm trying to achieve from the previous line.
exec("wget -O http://domain/file.zip &>/dev/null & echo \$!");
How do I go about changing the first line above to send the download to the background?
You have to make you use & to send the the process to the background and that you redirect all output. So you need to add
> /dev/null 2>&1 &
at the end of your command. So you should end up with something like this:
exec("(wget -O http://domain/file.zip && mysql -u user -ppassword database -e \"UPDATE \\`table\\` SET \\`status\\` = 'live' WHERE \\`id\\` = '1234'\") echo \$! > /dev/null 2>&1 &");
[Edit]
To make thing simpler, you can also move the wget and the update to another php file that you would call with exec. So you would end up with only
exec("php NewFile.php > /dev/null 2>&1 &");