我有一个需要在Ubuntu 14.04上作为服务运行的python应用程序。此应用程序需要具备以下功能:
当服务启动时,会在crontab中创建一个cron条目,该条目将定期运行该应用程序。
当服务停止时,crontab条目将被删除。
重启系统/服务器时,需要启动该服务。
我有以下的upstart脚本来运行我的服务:
start on [2345]
stop on [!2345]
script
LOGDIR=/usr/local/etc/myservice/logs/
CFGFILE=/usr/local/etc/myservice/myservice.conf
echo $$ > /var/run/myservice.pid
# If there is no cronjob by the name myservice, then add a cronjob to the crontab
set -x
exec bash -c '
if (( $(crontab -l | grep -c myservice) == 0 )); then
(crontab -l ; echo "1 * * * * myservice) | crontab -
fi'
end script
pre-start script
set -x
echo "[`date`] Starting myservice Service" >> /var/log/myservice.log
# Testing to see if myservice has been installed, else exit
[ -x /usr/local/bin/myservice ] || exit 0
mkdir -p /usr/local/etc/myservice/logs/
end script
pre-stop script
set -x
echo "[`date`] Stopping myservice Service" >> /var/log/myservice.log
end script
post-stop script
set -x
rm /var/run/myservice.pid
# If there is at least 1 cronjob by the name myservice, remove all such entries from crontab
exec bash -c '
if (( $(crontab -l | grep -c myservice) >= 0 )); then
(crontab -l | grep -v myservice) | crontab -
fi'
pkill -f myservice
end script
但是,当我尝试运行该服务时,它会挂起,我必须按ctrl + c才能返回命令行。与停止服务类似。我在这里错过了什么吗?任何帮助将不胜感激!