When we using Symfony\Component\Process\Process, the command run as who?
I tried the command whoami through Process, but it return void???
$return = exec('whoami');
echo $return."\n"; // return [myname]
$process = new Process('whoami'); // The symfony process
echo $process->getOutput(); // return nothing #_#
Yes, it runs as user you run this command or the user of your webserver.
Your code seems a bit incomplete. I suggest adding $process->run(); before trying to get an output.
I almost guarantee that that Process runs as whatever user your webserver is running as. If you're running apache for instance, try running:
ps aux | egrep '(apache|httpd)'
In your terminal to discover which user apache is running as. My money would be on either apache or httpd as the user which Process runs under. Hope that helps.
From the documentation is better use start() instead run() if you want to create a background process. The process_max_time could kill your process if you create it with run()
"Instead of using run() to execute a process, you can start() it: run() is blocking and waits for the process to finish, start() creates a background process."
Related
I want to run a Symfony Console Command from a controller. How to do so is explained in the documentation.
Now my controller waits until the Command is finished. But I want the Command to be executed as a background task (like a cronjob). So the controller doesn't need to wait to complete the Command, only start it.
Is this possible with Symfony Console?
I think you have to use Process component and launch command like that:
$process = new Process('php bin/console your:command');
$process->start();
I am attempting to call a console command from a controller using the Process component in Symfony2 to have it run in the background, however all it does is hang and end up at a white screen. This is an example of what fails:
$process = new Process('php app/console list');
$process->setWorkingDirectory($this->get('kernel')->getRootDir().'/../');
$process->run();
print $process->getOutput();
I have tried interchanging $process->run() with $process->start() and it still doesn't work.
Checkout AsyncServiceCallBundle, it allows you to execute your application service's methods in background without having to wait until they are finished. Just use it like this:
$this->get('krlove.async')->call('service_id', 'method', [$arg1, $arg2, $arg3]);
It uses this approach to make such calls asynchronous.
We have configured Hangfire to run as part of our web app using OWIN as provided in the tutorial.
We enqueue long running background process via an API we provide. The job we run initializes a R process in the background using the.Net Process class. The R code internally spawns a number of processes internally to finish the job faster. We get a number of Rscript process running in Task manager when the job runs.
On manually Recycling the app pool of our web app(to see how process restart works) the Rscript Processes are not killed. We have a custom kill strategy we to get rid of all the Rscript process within our code.
while (IsNotTimedOut())
{
try
{
_token.ThrowIfCancellationRequested();
Thread.Sleep(2000);
}
catch (OperationCanceledException)
{
Kill();
throw;
}
}
Within the kill method, we block using Process.WaitForExit() method.
When we do the manual recycle all the process are not killed. The current thread instead of blocking for the process to kill just dies after killing a couple of Rscript processes.
The hangfire code seems to just cancel the token and it doesn't seem to wait for the process to get killed that are listening on the cancellation token. Please, can someone suggest how do we get this working, Please let me know if more details are required?
I'm positive this is some misconfiguration but I can't seem to find what it is looking through the gearmanbundle and gearman docs.
I've set up Gearman and the gearman PHP extension, and have them running with Symfony2's GearmanBundle.
I can set up jobs and send them to a worker, but the worker isn't automatically executing them, rather it waits for me until I run the app/console gearman:worker:execute command.
Essentially all I want to do is write some images to S3 as a background process so my application doesn't wait for the process to complete before continuing.
When I do run that command it works fine, seems like everything is ok.
Am I running my gearman daemon with the wrong arguments, or, am I missing a configuration somewhere?
Or, do I need to do something like
foreach( $this->gearman->getWorkers() as $worker) {
$worker->doWork();
}
Or, should I be adding all of these as tasks? Just a little confused I guess.
Thanks a bunch. Here's what the code that calls the worker looks like:
foreach($photos as $photo){
$payload = array(
'tempBase64Data' => $photo->getTempBase64Data(),
'path' => $photo->getPath(),
'mime' => $photo->getMime(),
);
$jobIds[] = $this->gearman->doBackgroundJob(
'imageWorker~writeFileToS3',
json_encode($payload)
);
}
After more research I've found that this is the intended functionality of the GearmanBundle. It will not automatically execute jobs unless you run the execute console command.
I intend to use Supervisor to make sure these workers are always running.
again, im stucking in Gearman. I was implementing the ulabox gearman Bundle which works nicely. But there are two things which I dont unterstand yet.
How do I start a Worker??
Im the documentation, I should first execute a worker and the start the code.
https://github.com/ulabox/GearmanBundle/blob/master/README.md
Open the first console and run:
$ php app/console gearman:worker:execute --worker=AcmeDemoBundle:AcmeWorker
Now open another console and run:
$ php app/console gearman:client:execute --client=UlaboxGearmanBundle:GearmanClient:hello_world --worker=AcmeDemoBundle:AcmeWorker --params="{\"foo\": \"bar\" }"
So, if I dont start the worker manually, the job would be done by itsself. If I start the worker, everysthin is fine. But at least, it is a bit strange to start in manually, even if there is set an iteration of x so that the worker will kill itsself after that amount of job.
So please, can anyone help me out of this :((((
Heeeeeelp :) lol
thanks in advance an kind regards
Phil
Yes to run some task in background not only Gearman need to be run but also workers.
So you have run "gearman" that wait for some command (e.x. email send).
Additionally you have waiting workers.
When gearman view new command he look for first free worker and pass this command to it.
Next worker process execution for command and after finish return to Gearman server that it finished and ready to process new command.
More worker you have faster commands in queue processed.
You can use "supervisor" for automatic maintenance workers running.
Bellow you can find few links with more information:
http://www.daredevel.com/php-jobs-with-gearman-and-supervisor/
http://www.masnun.com/2011/11/02/gearman-php-and-supervisor-processing-background-jobs-with-sanity.html
Running Gearman Workers in the Background