I want to perform a task with a php file, (updating a feed) which I want to do automatically once a day. I DON'T want to have to load the file in a browser by hand. The file itself could be anything (very small and fast) but it needs to be run every day without using Cron jobs
If you're on a Windows machine, you can use a scheduled task (here are the instructions for Windows 7, but Google "run scheduled task " to find similar pages for other versions). It has much the same options as Cron, with a simple interface.
Two other possible hacks:
Have the URL for the feed itself be a PHP script that updates the feed and outputs it directly. Then you could but a cache in front of that URL so it only refreshes once a day (for instance the free level of Cloudfare).
Have the PHP script create a webpage that refreshes itself once a day, using the meta refresh tag. Then open a browser window and never close it.
If the page you have created is exposed to network, you could theoretically run a cron-job from another machine on the said network and call a curl to the page:
curl http://server/yourphp
Related
I want to run a PHP script every 5 seconds without using cronjobs. How is it possible in PHP?
I want to update user data every 5 seconds. The program will execute when I refresh the page but I want to run that script if the page is open or not in browser.
How can I achieve this ?
One way to do it would be to have a text file or a database entry that holds the time of the last run in UNIX time.
Then on all (or selected) pages you add something like;
If($lastrun +5 < strtotime(now)){
//Run the user update
}
This means when a user or visitor on your page goes to one of the "selected" pages with the code above this visitor will "run the update"
You must have some basics about PHP. PHP only runs when you request a page. So it's impossible to run php without requesting the page. Somehow you must reload the page every 5 seconds then you can run it every 5 sec.
So you must use cron or something like this. Or, you can you can use an old computer (NOT RECOMMENDED) which will relaod the page. To relaod the page you can use browser plugins like auto reload (for chrome).
But almost every hosting companies provide free cron job. Please search your cpanel for that. Or, mail your hosting provider for help. Cron is the best way to do this job for free.
Is that possible?I know how to refresh a page with header commands or javascript but this demands that the user will constantly have a browser opened.I want to build an automated bot that will not need my computer opened all the time.
Run the PHP script from a command line with your CRON or Windows Task Scheduler. Create a new Task, and execute that task as often as you like without a user or a browser.
C:\php\php.exe -f C:\My_Folder\My_Script.php
So you want to execute your script independently of web-calls, the usual approach would be to launch your PHP-Script using cron.
You can also use 3d party service such as https://www.setcronjob.com/ as "external" cron - if the intervals are not too narrow...
i am working on web Payroll project using symfony framework. we have 27000 employees to process every month. when we doing employee payroll process we can not depend on browser request as it is a long time taking process and server time out coming. as a work around we like to execute the php script from the background on linux server. then even browser closed the scripts can excute the background.
what is the best way to do this task please help
More info
we would like to give the pay roll process start button from the web interface when user click on the button pay roll process should start even browser close it should execute until done from the background.
regards
To run in the backgroud do the following steps:
Wrap your php command in a shell script:
**
#!/usr/bin/bash
# set up environment variables, PATH etc. here
php /home/yourapp/yourscript.php
**
Code up a web page/php script to request the start.
In the script you need the line:
system('/home/yourapp/yourscript.sh > scriptlog.txt &'
You probably need an extra link to browse the "scriptlog.txt" file from the web.
There are different ways to handle this problem.
The first way is to execute , startinan shell command in background (and with using nohup if you like) starting from your PHP code. Thats nearly the same think like the first answer in the duplicate question.
The 2nd way is to use PHP PCNTL Feature.
Using PCNTL you can create child processes, which running in background. So it's possible to make an fork and returning to the user: "pay roll process is running - you will get an mail, if the system is ready".
I created a script that gets data from some web services and our database, formats a report, then zips it and makes it available for download. When I first started I made it a command line script to see the output as it came out and to get around the script timeout limit you get when viewing in a browser. But because I don't want my user to have to use it from the command line or have to run php on their computer, I want to make this run from our webserver instead.
Because this script could take minutes to run, I need a way to let it process in the background and then start the download once the file has been created successfully. What's the best way to let this script run without triggering the timeout? I've attempted this before (using the backticks to run the script separately and such) but gave up, so I'm asking here. Ideally, the user would click the submit button on the form to start the request, then be returned to the page instead of making them stare at a blank browser window. When the zip file they exists (meaning the process has finished), it should notify them (via AJAX? reloaded page? I don't know yet).
This is on windows server 2007.
You should run it in a different process. Make a daemon that runs continuously, hits a database and looks for a flag, like "ShouldProcessData". Then when you hit that website switch the flag to true. Your daemon process will see the flag on it's next iteration and begin the processing. Stick the results in to the database. Use the database as the communication mechanism between the website and the long running process.
In PHP you have to tell what time-out you want for your process
See PHP manual set_time_limit()
You may have another problem: the time-out of the browser itself (could be around 1~2 minutes). While that time-out should be changeable within the browser (for each browser), you can usually prevent the time-out user side to be triggered by sending some data to the browser every 20 seconds for instance (like the header for download, you can then send other headers, like encoding etc...).
Gearman is very handy for it (create a background task, let javascript poll for progress). It does of course require having gearman installed & workers created. See: http://www.php.net/gearman
Why don't you make an ajax call from the page where you want to offer the download and then just wait for the ajax call to return and also set_time_limit(0) on the other page.
I am working on a site that require a php script running on a server without any request,
it is a bot script that keeps (not full time but at least once a day) checking client accounts and send alert messages to clients when something happens.
any ideas are appreciated.
Assuming you need to do this on linux, you may run any php script from the browser and from the CLI as well.
You may run a simple php script:
<? echo "Ana are mere"; ?>
like this:
php -f ./index.php
Be careful about file-permissions, and any bug that may creep inside your code, memory leaks or unallocated variables will become VERY visible now, as the process will run continuously.
If you dont want it running in the background all the time, take a look at crontab (http://unixgeeks.org/security/newbie/unix/cron-1.html) to be able to start jobs regularly.
-- edit--
take a look at php execute a background process and PHP: How to return information to a waiting script and continue processing
Basically you want to start a background process, and you may do this by either using exec() or fsockopen() or a file_get_contents() on your own script probably in this order, if don't have access to exec, or socket functions.
Also take a look at http://us2.php.net/manual/en/function.session-write-close.php so the "background script" won't "block" the request and http://us2.php.net/manual/en/function.ignore-user-abort.php
Use a cron job to do it http://www.cronjobs.org/
You can automatically call a script at any interval you like indefinitely. Your hosting provider should support them if they are good.
You should also consider putting a unique key on the end of the page
ie. www.yoursite.com/cronjob.php?key=randomstring
and then only run the script if the key is correct, to prevent bots and other users from running the script when you don't want it run.
If you can't create a cron job, then create a page that does what you want and create a scheduled task on another machine (maybe your PC?) that just goes out and hits that page at a certain time every day.
It's really a hack, but if you absolutely can't set up a cron job, it would be an option.
As Evernoob and Quamis said, you want to have a cron job (UNIX/Linux/Mac OS) or a scheduled task (MS Windows). Furthermore, you can either have the PHP script run using the PHP command line interface (CLI), in which case you can invoke the PHP executable and then your script name. As an alternate, you can use a tool like wget (availble on all platforms) to invoke the PHP script as if someone had typed the URL in the location bar of a web browser.
A php script could not be used like you imagine here. Because it's executed through apache after a request from somewhere.
Even if you do while(1) in your script, apache/php will automaticly stop your script.
Responding to your comment, yes you'll need ssh access to do this, except if your web interface allow you to add cronjob.
Maybe you can write a service which can be executed with a program on another server and do the job.
If you have no access to the server the easiest way would probably be to hit it through the browser, but that would require you or an external script hitting the URL at the same interval each day when you wanted it to one. You may also be able to setup a Selenium test suite that runs locally on a schedule and hits the page. I'm not 100% if that's possible with Selenium though, you may need some 3rd-party apps to make it happen.
Something else you could try would be to see about using PHP's Process Control Functions (link). These will let you create a script that is a deamon and runs in the background. You may be able to do this to keep the script running on the server and firing off commands at programmed intervals. You will still need some way to get it running the first time (browser request or via command line) though.