PHP pid from popen [duplicate] - php

This question already has answers here:
Killing processes opened with popen()?
(3 answers)
Closed 1 year ago.
Do I have a chance to find the pid of the application I'm running with popen?
I know it's possible with proc_open but it's unlikely for me to change my app's structure
Or how can I stop the process that is opened with popen and continues to run?
When encoding stream with ffmpeg sometimes I need to stop

function popen:
https://www.php.net/manual/en/function.popen.php
function pclose:
https://www.php.net/manual/en/function.pclose.php
Where pclose will return -1 if it could not close the process.
If pclose cannot close the process run popen & use a system command to kill the process by getting the parent pid of the forked process:
Killing processes opened with popen()?
Very last answer may work, otherwise use proc_open:
https://www.php.net/manual/en/function.proc-open.php

Related

Running unix process in background from php [duplicate]

This question already has answers here:
php execute a background process
(21 answers)
Closed 9 years ago.
If I run this unix command directly in shell:
$ sleep 100 &
sleep runs in the background as expected, and I can continue working in the command line.
but trying the same thing with shell_exec() and php I get different results.
<?php
$sleep = $argv[1];
$shell="sleep " . $sleep . " &";
shell_exec($shell);
?>
when executing php sleep.php 100 the command line hangs and wont accept any more commands until sleep finishes. I am not sure whether this is a nuance I am missing with shell_exec() / $argv of php or with the unix shell.
Thanks.
The shell_exec function is trying to capture the output of the command, which it can't do while simultaneously continuing processing. In fact, if you look at the php source code, the php shell_exec function does a popen C call, which does a wait syscall on the command. wait guarantees that the subprocess doesn't return until the child has exited.

How to run a background process in PHP and get the return value [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Make PHP wait for Matlab script to finish executing
Okay, starting from php execute a background process
to run a background process works great. The problem is, I need the return of that process also. The obvious solution to me is :
$cmd = "($cmd > $outputfile 2>&1 || echo $? > $returnfile) & echo $! > $pidfile";
exec($cmd);
When I run the generated command on the command line, it backgrounds and the files are filled out as expected. The problem is that when php exec() runs, the command doesn't go to the background (at least, exec doesn't return until the command finishes). I tried variations with nohup and wait $pid, but still no solution.
Any thoughts?
This is tricky- you could potentially fork the process to do something else, leaving the original process in place.
http://php.net/manual/en/function.pcntl-fork.php
However, if this is a web application, there's no built-in way to retrieve the return code or STDOUT back into the parent process since it's technically async (your request-response cycle will likely end before a result can be produced).
You could store the return code and / or STDOUT to files to check later, though.

Test if a cron is executed only by the server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In php, how to detect the execution is from CLI mode or through browser?
How to test if a cron is executed only by the server
Thanks
Cron will never put anything on the display unless you use something like 'wall' in your cron script.
Are you redirecting your output to a log file or anything?
What you can do is to add a line at the bottom of the script you are executing in the cron; that does something like:
date +"%D %r `echo Cron completed`" >> /tmp/cron_job.log
Then you could check
cat /tmp/cron_job.log
and it would tell you when it finished.

exec() waiting for a response in PHP [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
php exec command (or similar) to not wait for result
I have a page that runs a series of exec() commands which forces my PHP script to halt alteration until it receives a response. How can I tell exec() to not wait for a response and just run the command?
I'm using a complex command that has a backend system I can query to check the status, so I'm not concerned with a response.
Depends on what platform you are using, and the command you are running.
For example, on Unix/Linux you can append > /dev/null & to the end of the command to tell the shell to release the process you have started and exec will return immediately. This doesn't work on Windows, but there is an alternative approach using the COM object (See edit below).
Many commands have a command line argument that can be passed so they release their association with the terminal and return immediately. Also, some commands will appear to hang because they have asked a question and are waiting for user input to tell them to continue (e.g. when running gzip and the target file already exists). In these cases, there is usually a command line argument that can be passed to tell the program how to handle this and not ask the question (in the gzip example you would pass -f).
EDIT
Here is the code to do what you want on Windows, as long as COM is available:
$commandToExec = 'somecommand.exe';
$wshShell = new COM("WScript.Shell");
$wshShell->Run($commandToExec, 0, FALSE);
Note that it is the third, FALSE parameter that tells WshShell to launch the program then return immediately (the second 0 parameter is defined as 'window style' and is probably meaningless here - you could pass any integer value). The WshShell object is documented here. This definitely works, I have used it before...
I have also edited above to reflect the fact that piping to /dev/null is also required in order to get & to work with exec() on *nix.
Also just added a bit more info about WshShell.
In the past, I've had good luck with constructs like the following (windows, but I'm sure there's an equivalent command in *nix
pclose(popen('START /B some_command','r'));
What about running command in background ?
exec('./run &');

Python and executing php files? [duplicate]

This question already has an answer here:
Closed 12 years ago.
Possible Duplicate:
Start local PHP script w/ local Python script
How do you execute a php file and then view the output of it?
os.system("php ./index.php")
Does not work only returns 0
What you need is the os.popen function. It runs the command and returns the stdout pipe for that commands output. You can also capture the stdin, stderr by using os.popen2, popen3
import os
outp = os.popen('php ./index.php')
text = outp.read() # this will block until php finishes
# and returns with all output in text
print text
If you're working under Windows, you can use os.startfile().

Categories