后台执行脚本, 直接使用nohup就可以了,这里使用普通的PHP脚本进行操作:
vim bg-test.php
<?php
while (true){
file_put_contents('test.log', date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND);
usleep(1000);
}
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# nohup php bg-test.php &
查看是否成功执行:
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# watch tail -10 test.log
为了避免重启带来的影响,把命令写入脚本,可以加入开机启动项
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# vim gb-test
#!/bin/bash
# chkconfig: 2345 55 25
# Description: php background test
cd /var/data/www/mytest
nohup php bg-test.php &
~
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# chmod u+x gb-test
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# cp gb-test /etc/init.d/
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# chkconfig --add gb-test
查看是否添加成功
[root@iZwz9dyd1fdq7aehs8yuibZ mytest]# chkconfig --list|grep gb-test
gb-test 0:off 1:off 2:on 3:on 4:on 5:on 6:off
重启主机查看。