run a php script in background - php

I have a php scrip, in which i have written the following code
$client = new \Predis\Client();
$client->select(4);
$client->lpush('emailid',$x['to']);
$command = "/usr/bin/php5 -f /var/www/Symfony/src/Ens/NewBundle/Controller/cron.php";
exec( "$command > /dev/null &", $arrOutput );
return $this->render('EnsNewBundle:Email:header.html.twig');
in this I have written an another php script named as cron.php. I want to run that script in background. and I want to check that is this running in background or not. how can i check that

Maybe you could have a look to the Symfony2 Process component.
It's quite useful for running command from PHP.

You can take the output of cron in a file by > filename and check if it really runs.
Or check in process list if there is a new php process stating when you run this one.
You should also look at Codememe bundle here
Do check open source queuing systems too, they are helpful many times.
Like Beanstalkd or RabbitMQ
You can push data to these queues, they can be say "filenames" and other worker takes data from the "tubes" of queues and apply say "php filename" and then picks up next data from queue.

Related

Run script on other Server

I have 2 websites, hosted on 2 different servers. They are kind of interlinked. Sometimes I just do stuff on Website-1 and run a script on Website-2. Like I edited something on Website-1 and now I want to run a script on Website-2 to update accordingly on it's server.
Till now I am using following code on website 1.
$file = file_get_contents('Website-2/update.php');
But the problem with this is that my Website-1 server script stops running and wait for the file to return some data. And I don't wanna do anything with that data. I just wanted to run the script.
Is there a way where I can do this in a better way or tell PHP to move to next line of code.
If you want to call the second site without making your user wait for a response,
I would recommend using a message queue.
Site 1 request would put a message to the queue.
Cron job to check queue and run update on site 2 when message exists.
Common queues apps to look at:
[https://aws.amazon.com/sqs/?nc2=h_m1][1]
[https://beanstalkd.github.io/][2]
[https://www.iron.io/mq][3]
[1]: https://aws.amazon.com/sqs/?nc2=h_m1
[2]: https://beanstalkd.github.io/
[3]: https://www.iron.io/mq
What you're trying to achieve is called a web hook and should be implemented with proper authentication, so that not anybody can execute your scripts at any time and overload your server.
On server 2 you need to execute your script asynchronously via workers, threads, message queues or similar.
You can also run the asynchronous command on your server 1. There are many ways to achieve this. Here are some links with more on this.
(Async curl request in PHP)
(https://segment.com/blog/how-to-make-async-requests-in-php/)
Call your remote server as normal. But, In the PHP script you normally call, Take all the functionality and put it in a third script. Then from the old script call the new one with (on Linux)
exec('php -f "{path to new script}.php" $args > /dev/null &');
The & at the end makes this a background or non-blocking call. Because you call it from the remote sever you don't have to change anything on the calling server. The php -f runs a php file. The > /dev/null sends the output from that file to the garbage.
On windows you can use COM and WScript.Shell to do the same thing
$WshShell = new \COM('WScript.Shell');
$oExec = $WshShell->Run('cmd /C php {path to new script}.php', 0, false);
You may want to use escapeshellarg on the filename and any arguments supplied.
So it will look like this
Server1 calls Server2
Script that was called (on Server2) runs exec and kicks off a background job (Server2) then exits
Server1 continues as normal
Server2 continues the background process
So using your example instead of calling:
file_get_contents('Website-2/update.php');
You will call
file_get_contents('Website-2/update_kickstart.php');
In update_kickstart.php put this code
<?php
exec('php -f "{path}update.php" > /dev/null &');
Which will run update.php as a separate background (non-blocking) call. Because it's non-blocking update_kickstart.php will finish and return to searver1 which can go about it's business and update.php will run on server2 independantly
Simple...
The last note is that file_get_contents is a poor choice. I would use SSH and probably PHPSecLib2.0 to connect to server2 and run the exec command directly with a user that has access only to that file(Chroot it or something similar). As it is anyone can call that file and run it. With it behind a SSH login it's protected, with it Chrooted that "special" user can only run that one file.

How could I run a command in like an background task in Symfony application?

I have to set up a job manager for the console command that I have created.
The console command needs from 20 - 30 minutes to finish.
This console command is writing into the database multiple queries and let's assume that it can't be shrink-ed to a less time.
I have all created in Symfony2.8, but I am a really newbie using it.
So I enter link description here
All I need is to run this command like a background task. Without touching the frontend of the application. Could anyone help me out ?
Ah, all right. This needs to be done regularly, but on a user/clients request.
It very much depends on what it needs to do.
Just run once it once-only? Login onto the server, use tmux or screen to keep something running, even if you disconnect, and then run the command (bin/console name-of-command).
Running regularly - put an entry into crontab for the user that needs to run it.
Without knowing what the script needs to do, and how often it would be done (if any), it's hard to say what the nexts steps could be.
You need The Process Component.
You can achieve php running in background the following way:
$builder = new ProcessBuilder();
$builder->setPrefix('/usr/bin/php'); // put your php executable here
$cmd = $builder->setArguments(['/full/path/to/php/file/you/want/to/run.php', 'argumentOne', 'argumentTwo'])
->getProcess()
->getCommandLine();
$process = new Process($cmd . ' > /dev/null 2>&1 &'); // async trick here
$process->run();

Big XML file import to database PHP

I am facing a problem that somehow I don't see the solution to it. I have a XML file that needs to be importet to custom DB structure, when user uploads / imports the file the ajax post is waiting untill the file import is finished, but this could take 5 hours or more I don't know. What is the best way to handle this UI issue.
I was thinkg about thread uplaod, to split the file in multiple parts and upload each with it's own thread (pthreads, having problems with instalation on centos 7 / PHP 7)
Or if there is any other way that I could import the file in the background and whenerever the user refreshes the page there would be a status log output so that user would know when the import is finished and if successful.
You would want to run them using a background job ( a detached process ) this way the end user gets a confirmation message right away, and then send an email when the long running task is complete. Then they don't have to wait for it to finish. As I mentioned in the comments I have a class I wrote on my git hub for this
https://github.com/ArtisticPhoenix/MISC/blob/master/BgProcess.php
But it passes the args as a path because it's setup for Code Igniter, so you would have to change that or split the arguments up within your code.
Anyway the basics is similar to running a cron job, This varies in the implantation depending on the OS of the server. But on Linux the command is like this
php -f "path/to/phpfile.php" "{args}" > /dev/null &
The > /dev/null & part sends the output to null ( throws it away ) and the & runs it as a non-blocking process meaning the script starting the command can continue on. So using an example as this
.. other code before starting background job ..
exec( 'php -f "path/to/phpfile/xmlProcessor.php" "testXML/2" > /dev/null &');
.. code to tell user job is started .. this runs right after the call without waiting for that process to finish.
Then in xmlProcessor.php you would have this
<?php
$args = explode('/', $argv[1]);
$file = $ags[0];
$user_id = $args[1];
... code to process xml
... email user confirmation of completion
http://php.net/manual/en/reserved.variables.argv.php
As I said typically would call it this way,
exec( 'php -f "path/to/phpfile/xmlProcessor.php" "testXML" "2" > /dev/null &');
And access them using
$argv[1] // = testXML
$argv[2] // = 2
But because I use this with CI, it does it's routing for me to a special controller and handles all that. The nice thing about my class is that it should find the PHP executable under most cases, and it has windows compatibility built in ( which was a pain in the ...)
Using that class you would just call it like this
$command = new BgProcess( "path/to/phpfile/xmlProcessor.php", "testXML", 2);
echo $command;
Would output 'php -f "path/to/phpfile/xmlProcessor.php" "testXML/2" > /dev/null &' after starting the process ( the return is just for debugging )
Basically your running a separate background job with PHP via the command line.

Run python server script via php file and display output

After many trials and many days of search I am asking this question. I have tried many online solutions but doesn't seem to work for me. I have a python file Server.py that starts the flask server for me.
I want to start this file from a php file and display output. Right now I am trying with exec() function but the webpage keeps running and I could not figure if the server has started or not.
I have tried following ways with exec() and making a batch file
exec('C:/foldername/py/apps/webapp/s.bat &');
or
exec('C:/foldername/py/apps/webapp/s.bat > /dev/null');
or
$command = "python C:/folder/server.py";
$pid = popen( $command,"r");
Please help
I was able to achieve what I want in the end. I created a task schedule to open the batch file that can start my server. Then I set trigger on windows event. After that I created a batch file which creates an event in windows event logger and a php script to run that batch file. So, finally I was able to start python server file by php script with the help of task scheduler.

PHP Recurring Operation

For an iOS Push Notification server, I am implementing a web service that checks a feed on the net for a particular price.
Therefore I need my PHP to keep checking a price (every 20 seconds or so) and check values.
I was wondering (forgive my ignorance I just started with PHP today) is the way people do this a cronjob? Or is there some special way to fire a php script that runs until it's killed and repeats a task?
Thanks!
John
If PHP was your preferred route, a simple script such as the following can be set to run indefinitely in the background (name this grabber.php):
#!/usr/bin/php
<?php
do {
// Grab the data from your URL
$data = file_get_contents("http://www.example.com/data.source");
// Write the data out somewhere so your push notifications script can read it
file_put_contents("/path/to/shared/data.store", $data);
// Wait and do it all over again
sleep(20);
} while (true);
And to start it (assuming you're on a unixy OS):
$ chmod u+x grabber.php
$ ./grabber.php > /path/to/a/file/logging/script/output.log 2>&1 &
That & at the end sends the process to run in the background.
PHP is probably overkill for this however, perhaps a simple bash script would be better:
#!/bin/bash
# This downloads data and writes to a file ('data-file')
doWork () {
data=$(curl -L http://www.example.com/data.source)
echo $data > data-file
sleep 20
doWork
}
# Start working
doWork
$ chmod u+x grabber.sh
$ ./grabber.sh > /path/to/logger.log 2>&1 &
That is possible by setting up a cron jobs on your server.
Login to your web hosting e.g cpanel create a new cron job and add the path to the php file that you want to run. e.g php /home/[your username]/public_html/rss/import_feeds.php. There is field where you can input the number of minutes would you like the php script to run.
Run a PHP file in a cron job using CPanel

Categories