Linux中执行后台执行脚本


后台执行脚本, 直接使用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

重启主机查看。






 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值