My server has a lot of photos saved. I would like to archive (.zip) those photos and download it from the server. I have created a PHP script to make archive those photos into a .zip file and download it.
If I execute that file directly through the browser, it's getting a timeout error. So I have created a CRON job to execute the script. But I only need to execute the CRON job whenever I need it. (Like clicking a button to execute the script).
Thanks for all the help!
See the set_time_limit function to prevent your timeout error.
http://php.net/manual/en/function.set-time-limit.php
As for running a cron as-needed, that isn't how a cron works. A cron executes a command at a set interval. A PHP script can include code to exit immediately if it's processing isn't needed. You will need to create some way to signal to the script if it needs to run or not. You could do this by updating a file or database record.
Alternatively you can use an application server to execute commands (scripts) as-needed via web service calls. PHP doesn't have good support for multi-threading which is needed for most application servers so you will likely need to use Java, Python, or Ruby to create web services to launch scripts as-needed in real time. You can then call these web services from PHP using cURL functions.
Related
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 have a php script which needs to run in the background. Currently on my dev box I am running
shell_exec("php5-cgi <path>");
It works fine. Although, when I pushed my code onto a dedicated phpfog cloud, it seem that the script is not being called. I am unable to find out where the issue is. Is it that exec_shell is not working or php5-cgi command does not exist.
How can I understand better what is going on ?
You might try dropping 5-cgi from your exec call:
exec_shell("php <path>");
Additionally, you could use a cron job for this: http://docs.phpfog.com/customize/cron
Create a cron job to run <path> on an interval that works for your needs (Assuming path is a php file). PHPFog makes this very easy.
If you are trying to execute a long running task, an not make a browser wait for the result, I recommend queuing in a database table then use a cron job to execute a php script that handles those queued tasks. I would use ajax calls from the client to check the status of the task and then do a page refresh when the task completes.
I am developing a in house app which is accessed from multiple clients.There are few xml files on the server which need to be recreated every time whenever server starts.
My question is how can I check whether the server is shut-downed or restarted via my php program? which seems abit tricky..;-) so can i run a method on server shutdown?
I am using codeigniter.
Thanks,
On systems that support cron(8) (Linux, xBSD, etc), you can use #reboot feature to execute command after reboot. Syntax for crontab is simply:
#reboot /path/to/command
You can create a batch file which takes care of creating xml files.
And place that batch file in windows start up folder. So that every time machine starts that batch file will be executed and xml files will be generated.
In batch file call your application which generates the xml files.
what system are you on, Linux?
i would use a script called over PHP Cli that is called when the system starts.
References:
on PHP Cli
http://www.php.net/manual/en/features.commandline.usage.php
on RC scripts - called when the system starts:
http://www.linux.com/news/enterprise/systems-management/8116-an-introduction-to-services-runlevels-and-rcd-scripts
you can find more if you just google for those.
also like the #reboot solution, that plaes proposed.
here some examples
http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples/
I have written a PHP script which generates an SQL file containing all tables in my database.
What I want to do is execute this script daily or every n days. I have read about cron jobs but I am using Windows. How can I automate the script execution on the server?
You'll need to add a scheduled task to call the URL.
First of all, read up here:
MS KB - this is for Windows XP.
Second, you'll need some way to call the URL - i'd recommend using something like wget - this way you can call the URL and save the output to a file, so you can see what the debug output is. You can get hold of wget on this page.
Final step is, as Gabriel says, write a batch file to tie all this up, then away you go.
e: wget is pretty simple to use, but if you have any issues, leave a comment and I'll help out.
ee: thinking about it, you don't even really need a batch file, and could just call wget directly..
add a scheduled task to request the url. either using a batch file or a script file (WSH).
http://blog.netnerds.net/2007/01/vbscript-download-and-save-a-binary-file/
this script will allow you to download binary data from a web source. Modify it to work for you particular case. This vbs file can either be run directly or executed from within a script. Alternately you do not have to save the file using the script, you can just output the contents (WScript.Echo objXMLHTTP.ResponseBody) and utilize the CMD out to file argument:
cscript download.vbs > logfile.log
save that bad boy in a .bat file somewhere useful and call it in the scheduler: http://lifehacker.com/153089/hack-attack-using-windows-scheduled-tasks
Cron is not always available on many hosting accounts.
But try this:
http://www.phpjobscheduler.co.uk/
its free, has a useful interface so you can see all the scheduled tasks and will run on any host that provides php and mysql.
You can use ATrigger scheduling service. A PHP library is also available to create scheduled tasks without overhead. Reporting, Analytics, Error Handling and more benefits.
Disclaimer: I was among the ATrigger team. It's a freeware and I have not any commercial purpose.
Windows doesn't have cron, but it does come with the 'at' command. It's not as flexible as cron, but it will allow you to schedule arbitrary tasks for execution from the command line.
Yes, You can schedule and execute your php script on windows to run automatically. In linux like os u will have cron but on windows u can schedule task using task scheduler.
If your code is in remote hosted server then create a cron-job for the same.
Else if in local then use a scheduled task in windows.Its easy to implement.I am having servers with so many scheduled tasks running.
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.