如何让队列一直在后台运行
作者:钓赛通
发布时间:2021-12-22
点击数:
我们在开发系统时,经常会用到让某个任务一直在后台运行并保持不会假死。
这个时候,我们需要用到 shell 和 crontab
这是一个shell 脚本示例:
#!/bin/bash basepath=$(cd `dirname $0`; pwd) chmod a+x "$basepath/yii" command="$basepath/yii queue/listen 1" result=$(ps -ef | grep "`echo $command`" | grep -v "grep") if [ ! -n "$result" ] then echo "Starting the process." str=$(nohup $command >/dev/null 2>&1 &) echo -e "\033[32mOk.\033[0m" else echo "The process has been started." fi result=$(crontab -l|grep -i "* * * * * $basepath/queue.sh"|grep -v grep) if [ ! -n "$result" ] then echo -e "\033[32mCreating queue crontab.\033[0m" echo "Export crontab data" crontab -l > createcrontemp echo "Add new crontab line" echo "* * * * * $basepath/queue.sh" >> createcrontemp echo "Import crontab data" crontab createcrontemp echo "Delete temp file" rm -f createcrontemp echo -e "\033[32mCreating queue crontab success.\033[0m" else echo "The queue crontab has been add ." fi
把这个脚本加入 crontab 。设置成 * * * * *。
这样就可以保证进程一直在执行。