I have php file that is to be executed as cronjob, this php file contains some javascript.
I will explain the flow :
the Php is used to retrive some data(A LIST OF URLS) from DB.
For each URL Obtained, a Java script API is used.
THe result Obj returned from API contains data for each url.
The data is then sent back to as an AJAX Call for each url to a php file .
Can this be implemented Via CRON JOBS ?
OR
Is there any method to schedule javascript to run periodically, like cron for php?
UPDATE: i could manage the javascript call to API with PHP curl ,And the cron Job is getting executed perfectly. But i dont think it is the correct solution to this question may be Node.Js is the solution(i didnt test it yet).
You can't run Javascript in Cronjobs because Javascript is ran by browsers. I think you should take a look at curl in php to call an api instead.
http://www.php.net/manual/en/book.curl.php
You have to split the work: Cron the JS, Cron the PHP. In the middle, deliver one's results to another. Agree with phantomjs usage for JS execution (or casperJS-I prefer). Execute the JS, output to JSON as a file, read from the file using file_get_contents from PHP. And define these actions in two different cron jobs.
You can run Javascript via cron in a Javascript runtime like node.js: http://nodejs.org/
phantomjs is one possibility, see this thread wget + JavaScript?
Otherwise you could run Node.js on your server to execute JavaScript in a CLI type environment but mixing node.js and PHP could become complicated.
you can schedule javascript with cron by using Node.js
Related
I have two restful api endpoints; DispatchJob_Public.php and selectDriverForJobResult.php and I have to call selectDriverForJobResult.php from DispatchJob_Public.php after two minutes. How can i do that without using sleep()? Because i cannot afford putting server on busy wait (due to sleep function).
I have written a function in javascript and ajax to call the second file (selectDriverForJobResult.php), and that works fine when i hit the end point directly from browser but js doesnt work when the file is called from android or postman.
So i need a solution for this, that does not include cron job, javascript and sleep() function.
Thank you very much.
directly from php, there is no way
you can write a bash file and execute it with system
startBatch.sh
sleep 2m
php /path/fo/file.php
in your first file
system('./startBatch.sh >> null');
I am running an external script in PHP using its exec() function. I was looking for various options to create a progress bar. I am able to create a plain rotating loader through AJAX but I couldn't achieve the percentage progress bar. Is there any way to do that?
Depending on the program you want to execute, you could use proc_open() instead of exec(), so you can proccess the output, calculate a percentage and throw it back to your ajax script using ob_flush() and flush().
It's not true that you need to wait for the execution to finish before sending any output, but there are some caveats with your server cache configuration and browsers rendering engines, so it is not so reliable.
If you're not using Websockets (the clean and modern option, which can be achieved with PHP using Ratchet or with nodejs using various options), the most reliable way of doing what you want is with polling.
Briefly, instead of calling your script once, you do a first ajax request to init the proccess and then start poking the server again and again to ask the execution status of your script.
For more information, take a look at those answers:
Run process with realtime output in PHP
PHP - Flushing While Loop Data with Ajax
Grab results from a php exec() while the command is still running?
PHP runs on the server, thus can not achieve this (to my knowledge),
here are some answered questions that might be able to help you.
How to show loading status in percentage for ajax response?
Jquery:: Ajax powered progress bar?
I have a scraper which scrape one site (Written in python). While scraping the site, that print lines which are about to write in CSV. Scraper has been written in Python and now I want to execute it via PHP code. My question is
how can I print each line which is getting printed by python code.
I have used exec function but it is none of my use and gives output after executing all the program. So;
Is it possible to get python output printed while it is getting executed via PHP.
If i understand it well, your python scraper output to a file and you want to "live" display the output via php. What about doing and loop in php in which you use filemtime to know whether or not the file has been updated? You might add a little sleep in order not to overload your server.
If your are using a web page, you may use AJAX to reload only the concerned part of the page at a regular interval.
Hoping this helps you.
Simple case
Assuming execution of scraper is limited to php runtime, run it via popen: http://php.net/manual/en/function.popen.php
More involved case
If you want scraper to run on background and only connect to it vis php from time to time, you can either use some pub/sub toolkit or implement a small web server in the scraper that you can fetch result updates with fopen("https://localhost:port/...") or curl. Many other rpc mechanisms are possible, domain sockets, watching a file, sysv rpc...
I'd communicate using stdout instead of a file. Meaning the python script writes to stdout and the php script reads that.
Using proc_open you can control the python process from php and also read it's output.
Instead of using exec you could use passthru, that will output the data directly to the browser. http://php.net/manual/en/function.passthru.php
That should be enough to get the println from your script.
I think I have a fair idea of what you are saying put I am not too sure what you mean.
If you mean to say that everytime the python script does a print, you want the php code to output what was print?
If that is the case you could pass it as a POST DATA via HTTP. That is instead of printing in Python, you could send it to the PHP Script, which on receiving the data would print it.
I am not too sure if this is what you want though.
For proper communication you need to setup any medium, so you can use fifo, through it you can write string in python and read it with php.
For PHP fifo
http://php.net/manual/en/function.posix-mkfifo.php
For Python
http://www.doughellmann.com/PyMOTW/Queue/
Simply use system() instead of exec(). exec() saves all lines of stdout output of the external program into an array, but system() flushes stdout output "as it happens".
I wrote an HTML page with JavaScript, a PHP file and a shell script
they are all on the same machine
I run the shell script, it will open the html page with Firefox,
when the JavaScript finishes, it will POST to a getdata.php
<form id="hidden_form" method="POST" action="getdata.php" ></form>
The getdata.php will do something and then it will send a signal to the shell script
the above is the normal behaviour, I'm afraid at some time, the PHP or JavaScript run into error and don't send signal to the shell script
are there any good and simple way for shell script to detect whether JavaScript is running?
a guy below mentioned that I can let javascript send request to the server periodically, like once every 2 minutes, but how to let shell script notice this/get the signal?
As someone suggested, you can poll the server with setInterval to see if the javascript engine is still running, but that might as well mean that only the javascript thread doing setInterval is the one actually running (depending on what kind of error could arise). You could do the post to "status.php" which would touch/write to a file on the disk, which the bash script could poll to see if it's updated in regular intervals.
I'd however suggest that you look into something like phantomjs which allows you to solve these kind of problems.
No.
Remember: Javascript is running remotely over on the client browser; PHP (and your shell script) are on the server.
What you can do is set up an alarm (on the server) when you send the web page, and invoke an alarm handler if it triggers:
http://www.woodwose.net/thatremindsme/2011/05/forking-parallel-processes-in-bash/
If your JavaScript is running a long-to-process script, you may intermittently submit a request to the server over a set interval. If you don't see the request after a set period... ( interval x2 or w/e ) and the signal has not been received upon completion, you can assume that the JavaScript itself has stopped running / broke.
setInterval("methodofcommunicatingtotheserver", 1000);
etc.
..as stated before: No.
Your best options in this case are, either:
Re-design your script. Is it absolutely necessary for it to send the request to the php page via the javascript in-browser?
If the answer to the above is yes, have a look at Selenium, especially Selenium WebDriver.
It does not provide bindings for bash scripts out-of-the-box, but it's pretty easy to use from other languages such as Python (or PHP, they say).
I am creating a small plugin get get's data from different websites. The data does not have to be up to date, and I do not want to use a cronjob for this.
Instead with every visit of the website I want to check if the DB needs updating. Now it takes a while before the whole db is updated, and I do not want the user waiting for that.
Is there a way that I can have the function fired, but in the background. The user will just work as normal, but in the background the db is updating.
You could also fork the process using pcntl_fork
As you can see in the php.net example you get two execution threads following the function call. The parent thread could complete as usual, while the child could go on doing its thing
You'd want to use exec() with a command that redirects output to a file or /dev/null, otherwise PHP will wait for the command to complete before continuing with the script.
exec('/path/to/php /path/to/myscript.php 2>&1 > /dev/null');
There are many solutions to execute a PHP code asynchronously. The simplest is calling shell exec asynchronously Asynchronous shell exec in PHP. For more sophisticated true parallel processing in PHP try Gearman. Here a basic example on how to use Gearman.
The idea behind Gearman is you will have a deamon what will manage jobs for you by assigning tasks to worker. You will write two PHP files:
Worker: Which contain the code you want to run asynchronously.
Client: The code that will call your asynchronous function.