开发者

Run a PHP script (w/MYSQL) on a LAMP at shutdown/reboot

I need to execute a PHP script at a LAMP shutdown/reboot. The script will update the state of local server on Mysql databases running on remote servers.

<?php
print "Updating Hosts...";
mysql_connect('localhost','ivo','andream');
mysql_select_db('ecom');
$query = "SELECT * FROM companies"; 
$result=mysql_query($query);
$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
    $remote=mysql_result($result,$i,"sito开发者_如何学运维");
    $remdbport=mysql_result($result,$i,"dbport");
    $remhost = ''.$remote.':'.$remdbport.'';
    $remdb=mysql_result($result,$i,"database");
    $remusr=mysql_result($result,$i,"remusr");
    $rempswd=mysql_result($result,$i,"rempswd");
    $rconnect=mysql_connect($remhost,$remusr,$rempswd);
    mysql_select_db($remdb,$rconnect);
    $query = "UPDATE lareaclient SET serverstate = '0' WHERE kiave = 'workarea'";   
    $resultloop=mysql_query($query);
        mysql_close($rconnect);
    $i++;
}
mysql_close();
print "Hosts updating complete";
?>

The script is working well when launched on CLI this way:

root@server:/home/mylamp# lynx -dump https://ecom.anys.info/private/serverstatedown.php

but it seems not to work at shutdown or reboot. I've included the same CLI in a bash script on both runlevels 0 and 6 with

root@server:/home/mylamp# update-rc.d apache1 start 01 0 6 

but nothing happens.

My idea is that Mysql service stops before the PHP script be executed. Is that wrong? Any idea?


You could run the script from the shell by adding a

#!/usr/bin/php

to the top of the script; and then run it via cron:

@reboot root /path/to/apache1
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜