views:

373

answers:

1

Hello everybody,

I have a multi part question for a php script file. I am creating this file that updates the database every second. There is no other modeling method, it has to be done every second.

Now i am running CentOS and i am new to it. The first noob question is:

How do i run a php file via SSH. I read it is just # php path-to/myfile.php. But i tried to echo something, and i dont see it in the text.

Now i don't think that starting the file is going to be a problem. One problem i guess will be, i don't know if it is even possible, but here goes.

Is it possible for me to be hundred percent sure that the file is only run once. What happens if i by accident run the file again.

I was wondering further, if i implement a write to a log every second, i can know if everything is running ok. If there is an error or something wrong the log file will stop.

Is the writing to a log file with the fopen, and write and close. Isn't this going to take a lot of time, isn't there an easier method in CentOS.

Ok another big point i have is what happens when i run the file. Is the file run in the memory, or does it use the file in the system. Does it respond on changes made in the file, for example to stop the execution of the script.

Can i implement some kind of stop mechanism in the file itself. Or is there a command i can use to stop the file.

Another option i know of is implementing a cronjob that runs every minute. And this cronjob executes the php file. The php file will loop for one minute, updateting everything needed, and terminating. I implemented this method, but just used a browser. I just browsed to mu file, and opened it. I saw the browser was busy for a minute, but it didn't update anything in the database. Does anyone have an idea what the reason of this can be.

Another question i have is by implementing the cronjob method, what is the command i fill in the PLESK panel. Is it the same as the above command. just php and the file name. Or are there special command like -f -q -something.

Sorry for all the noob questions.

If someone can help me i really appreciate it.

Ciao!

+5  A: 

The simplest way to ensure only one copy of your script is running is to use flock() to obtain a file lock. For example:

<?php

$fp = fopen("/tmp/lock.txt", "r+");

if (flock($fp, LOCK_EX)) { // do an exclusive lock
    ftruncate($fp, 0); // truncate file
    fwrite($fp, "Write something here\n");
    flock($fp, LOCK_UN); // release the lock
} else {
    echo "Couldn't get the lock!";
}

fclose($fp);

?>

So basically you'd have a dummy file set up where your script, upon starting, tries to acquire a lock. If it succeeds, it runs. If not, it exits. That way only one copy of your script can be running at a time.

Note: flock() is what is called an advisory locking method, meaning it only works if you use it. So this will stop your own script from being run multiple times but won't do anything about any other scripts, which sounds fine in your situation.

cletus