Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running a PHP Script on reboot of server.

Status
Not open for further replies.

altendew

Programmer
Mar 29, 2005
154
US
Hi.. I need a way of tracking what time the server went down and the difference between when it went up.

I currently have countdown timers on my site, and if my server goes down it messes them up. I need the timers to stop while server is down.

The way I was thinking todo this.. is make a CRON that runs every minute and records the current time. Then once the server goes down the last time is recorded. Then once it comes back.. it minuses current time fron last cron time.. and gets the downtime amount.

The only problem is.. I have other cronjobs that cant run while this is running. I need some guidance on how to make this.

Basically I have to make a MYSQL Code like this for when it reboots.

UPDATE table SET spendTime=spendTime+INTERVAL (DOWNTIME AMOUNT HERE)
 
There are specific scripts/folders of scripts that are run when the server's runlevel is changed to '0' (which I believe is shutdown).

Read up on runlevel and on scripts that are executed. Also, the "shutdown" command's 'man' may help.

D.E.R. Management - IT Project Management Consulting
 
you can use only one script in order to start the counter and stop it. after create it, you must be sure that a parameter "start" will start the counter and "stop" (obviuously) will stop it. So after that, let the script in [tt] /etc/init.d[/tt] with execution permision. Then create a link (ln -s) S99your_script in /etc/rc3.d and /etc/rc5.d, and in /etc/rc0.d and /etc/rc6.d create a link (ln -s) as K99yourscript.

So, when you reboot/shutdown (init 6/0) the script will run with a "stop" parameter and when your machine start (init 3/5) the script will run with "start" parameter.

Cheers.
 
Thank You for your help. I used exactly what you did, and let me say that was a great idea.

I am having trouble though.. for some reason the stop function wont work, so when it shutsdown it wont record a time.

Here is what I currently have

File: startup
Code:
#!/bin/bash
# When the server starts, shutsdown put that time in a file

function start() {
        startFile="/home/gobubble/start";
        startTime=$(date +"%s");
        echo $startTime > $startFile;
}

function stop() {
        stopFile="/home/gobubble/stop";
        stopTime=$(date +"%s");
        echo $stopTime > $stopFile;
}

case "$1" in
	start)
                start
                ;;
	stop)
                stop
                ;;
esac

exit 0;
 
Here some more info on the stop part.

root@penguin [/etc/rc3.d]# cd ../rc0.d
root@penguin [/etc/rc0.d]# ls *startup -all
lrwxrwxrwx 1 root root 17 May 25 17:17 K99startup -> ../init.d/startup*

root@penguin [/etc/rc0.d]# cd ../rc6.d
root@penguin [/etc/rc6.d]# ls *startup -all
lrwxrwxrwx 1 root root 17 May 25 17:17 K99startup -> ../init.d/startup*
 
Also is their a way so that I can have this script run before any of the other ones?
 
Well I found a solution by creating my own snippets in the file main rc file.. This way I can execute code first and last.
 
sorry for late.

is there a way so that I can have this script run before any of the other ones?

of course, that's why the prefix is Sxx and Kxx, in your case S99 and K99. It tells the order to Start and Kill. With S99 you know that the script is the last one to execute (you need to start up some services before your script surely, like samba, apache, whatever) and your script should be the first to stop. e.g. you don't want to umount all your filesyste, before the script stop, right?

Cheers.
 
Anyone know why the code above wont work.. The server still wont run the stop function.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top