I have a windows pc with apache running, and I needed a php script to continuously run to listen to inputs coming from a UDP port, and take the required action and send it back.
The only way I know how to do this, is to install curl for cmd, and run the php script with a WHILE loop. What I am afraid is that this is the wrong way to do it.and may be unreliable and take up large amount of system resources.
Can people comment on the above method? I have heard of cron..but thats for unix only? What can I do?
Hey try this below solution.
Use a bat file and schedule to execute that bat file.
For example in the bat file executephp.bat, write this
c:\xampp\php\php.exe -f c:\xampp\htdocs\do_something.php
save that bat file that contains that line.
Go to windows scheduler and create a new task and in action tab, browse to point that executephp.bat and for start in -> direct to the directory u have that executephp.bat.
For example if u save the file under C:\xampp\htdocs put that C:\xampp\htdocs in the start in.
Remember to invoke the script even when the user is not logged on.
Everything is set and it will execute without problem.
A PHP script behind Apache will always have a maximum execution time, so the while-loop should always be stopped after the specific timeout.
You should better use cron or a batch script like Venkat recommended. There are some great services for cron out there, that will do a GET request to your server and run the script. Have a look at this related thread: Scheduled Request to my website from an external source
Doesn't that fit your needs?
Related
Is it possible to invoke php script automatically when someone uploads file via ftp-client on our server.
Do you have complete shell access to the server?
What you need to do is detect whether or not the contents of a folder has changed and have a script run.
If you're on Windows this might be helpful.
If you're on *nix, have a look into inotify or launchd
Have them call a php script and away you go!
If you don't have complete control over the server, no doubt you can execute Cron Jobs. Have it execute a PHP script every x minutes that checks the contents of a directory, compares it to the contents it was x minutes ago and performs a diff between the two to find added or removed files.
You can implement a port knocking daemon with iptables. Port knocking is an automate process to personalize ssh or ftp account. You can write a daemon to listen to iptables and run a php script when the right sequence of ports is knocked. I don't know if you can minimize the sequence to just one knock when you connect with a ftp-client.
At linux you can use the watch command:
watch
Usage: watch [-bdhnptvx] [--beep] [--differences[=cumulative]] [--exec] [--help] [--interval=<n>] [--no-title] [--version] <command>
You can pipe the output to a piece of software actually handling the changes.
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/
Is it possible? E.g. A script every minute write a word in a file (without the page opened in a browser).
Yes, you'd need to set up a cron job that runs regularly (eg: every minute).
You can accomplish this with a cronjob.
Get some detailed info here:
http://service.futurequest.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=30
Plus you might want to ask your hoster how you can add cronjobs on your particular server.
This is kind of job definitely not for web server. The job for web server is to invoke your script on request from the web. PHP engine can run autonomously, without server. You can run it: /usr/bin/php -f yourscript.php, on Windows repace beginning with your php.exe path. Then use cron to schedule execution
You can execute php from the command line, in this case your scripts executes regardless of server/browser, you can use a for loop, combined with sleep to achieve what you described.
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.