Can I call a function when server restarts or shutdown? - php

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/

Related

CRON-job Command Execute through PHP Script

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.

Running a looping PHP Script on my server

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?

What is the simplest way to make scheduled XML parsing into an SQL database?

So, ok, I'm using PHP for my website, and suppose I know quite a bit of MySQL and a little bit of MS SQL.
Now, I want to parse some XML with PHP/USD exchange rate and store it in the database.
The simplest way one could think of would be, perhaps this:
$XMLContent=file("http://www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=USD&ToCurrency=PHP");
foreach($XMLContent as $line){
$a=&strip_tags($line);
if (is_numeric($a))
{
// output the exchange rate
echo $a;
// THen I would probably go like this:
mssql_query('update table set xchange_rate='.(float)floatval($a));
// break cycle once found;
break;
}
}
That would perfectly do... if it didn't take 0.8 seconds to run this script, due to external XML GET request.
So I suppose I should use Windows Scheduler, and make a task to run, let's say every hour to update the records. Now the question is, I've no idea what script I would use. I mean, I can't just simply run browser with a PHP script - that would be rediculous.
So, what would be the easiest way to make a script/ application to run it outside PHP, without a need to actually open a browser.
Thanks!
EDIT
Good point, right now I'm testing it on Win7, but later it is going to be implemented on Windows Server (2008?).
php has a binary executable interpretor too. On win, its called php.exe
http://php.net/manual/en/features.commandline.php
get it working first from a shell prompt, then make a scheduled task for it.
keep in mind, it is not the same php as you get when running a script through a webserver. Some settings are different, and the version may be different, and may use a different php.ini file(and so may have different extensions loaded).
consider using absolute file paths to start until you get things working.
You don't need a browser to run a PHP script. PHP can happily run as a console application. Without knowing what system you're deploying this on (i.e. which OS), it's hard to give you more directions. For example, on a unix-like system (UN*X, linux, OS X, etc.) you can create your PHP file:
somefile.php
<?php
.....
?>
save this file into some pre-defined directory, for example, /opt/myproject and then schedule it in /etc/crontab (assuming your php is installed in /usr/bin):
7 * * * * /usr/bin/php /opt/myproject/somefile.php
This would run your script with PHP, without any browser, once an hour, at 7 minutes past each hour.
EDIT If you're to deploy this on a windows server, you can either use at command on the command prompt or use the Task Scheduler snap-in in Server Manager to configure your job. The PHP script would be saved where you like it to be (e.g. D:\Projects\MyProject\myfile.php) and the command you would schedule to run would be C:\WAMP\bin\php.exe - or whereve your php.exe is located).

Is it possible to invoke php script automatically when someone upload file via ftp

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.

Schedule and execute a PHP script automatically

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.

Categories