共計(jì) 1513 個(gè)字符,預(yù)計(jì)需要花費(fèi) 4 分鐘才能閱讀完成。
這篇“l(fā)inux 定時(shí)執(zhí)行 php 啟動(dòng)任務(wù)腳本怎么寫(xiě)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以丸趣 TV 小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“l(fā)inux 定時(shí)執(zhí)行 php 啟動(dòng)任務(wù)腳本怎么寫(xiě)”文章吧。
第一步:創(chuàng)建一個(gè)定時(shí)腳本文件 cron.sh(文件名隨意以.sh 結(jié)尾)。
參考代碼如下(log.txt 是個(gè)日志文件方便查看是否有輸出內(nèi)容):
echo supervisorctl restart... /var/www/html/log.txt
start_time=$(date)
echo $start_time /var/www/html/log.txt
echo /var/www/html/log.txt
supervisorctl restart all
end_time=$(date)
echo supervisorctl end /var/www/html/log.txt
echo $end_time /var/www/html/log.txt
echo /var/www/html/log.txt
我這邊利用 supervisorctl 來(lái)管理 php 進(jìn)程(關(guān)于 supervisorctl 的使用可以查看官方文檔), 當(dāng)然也可以利用其他方式。
這里舉個(gè)例子來(lái)簡(jiǎn)單說(shuō)明 supervisorctl 的使用。
首先需要安裝 supervisorctl,在終端下執(zhí)行 yum install -y supervisor,
supervisorct 默認(rèn)安裝在 /etc/supervisor 目錄下;
完成后,在 /etc/supervisor 路徑下有個(gè)配置文件 supervisord.conf,打開(kāi)后在最后有個(gè) [include] 后配置一下 files 如下:
files = /etc/supervisor/conf.d/*.conf
然后就可以在 /etc/supervisor/conf.d 目錄下創(chuàng)建自己的配置文件;
例如創(chuàng)建文件 test.conf(我這邊是 php laravel 框架執(zhí)行的一個(gè)消費(fèi)任務(wù)隊(duì)列)文件內(nèi)容配置如下:
[program:rabbitmq-comsumer-1]
process_name=%(program_name)s_1
command=php /var/www/html/app/artisan rabbitmq:consumer 1
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/html/app/storage/logs/rabbitmq-comsumer-log-1.log
第二步:其次編寫(xiě)定時(shí)任務(wù)內(nèi)容,在終端控制臺(tái)輸入 vim /etc/crontab 回車;在文件里寫(xiě)入如下代碼:
0 */2 * * * root /var/www/html/cron.sh
注:0 */2 * * *(這是定時(shí)任務(wù)執(zhí)行語(yǔ)法,我這邊是每 2 小時(shí)執(zhí)行,也就是 0:00,2:00,… 自動(dòng)執(zhí)行;相關(guān)語(yǔ)法請(qǐng)百度查閱)
第三步:保存文件,然后使其生效(這一步很重要!)執(zhí)行如下命令:
crontab /ect/crontab
最后可以使用以下命令查看修改后的結(jié)果,如果看到出現(xiàn) 0 */2 * * * root /var/www/html/cron.sh 則說(shuō)明已經(jīng)生效:
crontab -l
以上就是關(guān)于“l(fā)inux 定時(shí)執(zhí)行 php 啟動(dòng)任務(wù)腳本怎么寫(xiě)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望丸趣 TV 小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注丸趣 TV 行業(yè)資訊頻道。