<?php /* * Unix中 nohup 命令功能就是不挂断地运行命令,同时 nohup 把程序的所有输出到放到当前目录 nohup.out 文件中, * 如果文件不可写,则放到 <用户主目录>/nohup.out 文件中。那么有了这个命令以后我们php就写成shell 脚本使用 * 循环来让我们脚本一直运行下去,不管我们终端窗口是否关闭都能够让我们php 脚本一直运行下去。 */ #!/usr/bin/php set_time_limit(0); while(true){ file_put_contents('timer.txt',date('Y-m-d H:i:s')."/n",FILE_APPEND); echo date("Y-m-d H:i:s", 'OK!'); sleep(30); } /* * 保存退出 * #chmod +x timer.php * 让它在后台运行: * # nohup /var/www/html/timer.php & * &符号的作用是让进程在后端挂起 * * # ps * 将会在终端显示 5155 pts/1 00:00:00 timer.php * * 终结进程 * # kill -9 5155 */