I need keep running the PHP script but I have a problem, currently I use putty to login to the server then execute PHP code. When I close putty session the script stops the execution.
Steps:
but when I close the putty session the program will obvious close. I need to keep running the program on server regardless of closing putty and anyone logging into server can stop it and run it again as a process. How to run a php script independent of putty session, which can be controlled by anyone logging to server?
I'm not sure your question is really in the scope of programming, it is more a unix/linux specific question of "How to run a program from shell that doesn't close when I close the terminal". PHP happens to be that program.
You can do php -f scriptname.php > /dev/null 2>/dev/null & disown
.
STDOUT and STDERR will be redirected to /dev/null (or you can change those to real files) and the ambersand will fork the process. Disown will remove the running PID from the terminal session.
What you need to do is to fork the process before you terminate it. This way when you exit the process (or close your ssh connection) the secondary process will continue to run.
$pid = pcntl_fork();
if ($pid == -1) {
die('could not fork');
} else if ($pid) {
// parent
pcntl_wait($status);
} else {
// child
// write your code here!
}