我有一个Symfony应用程序,它依赖于Enqueue Bundle来执行后台命令,并且正在将其部署到AWS弹性beantalk中。 现在,我想在部署(部署后)后直接在后台运行enqueue broker命令以及所有其他enqueue命令。
我现在所做的只是创建一个shell脚本,该脚本创建了一个新的/etc/init.d
服务,该服务用于基于命令PID启动/停止/状态命令。
通过向.ebextensions/files.config
添加新命令
"/opt/elasticbeanstalk/hooks/appdeploy/post/02_start_my_command_service.sh":
mode: "000755"
owner: root
group: root
content: |
#!/bin/bash
/var/www/html/bin/my-command-service.sh
和my-command-service.sh
是负责创建和运行my-command-service
#!/bin/bash
COMMAND='php /var/app/current/bin/console my:command'
LOG_DIR='/var/log/my-command'
LOG_FILE='/var/log/my-command/log.log'
LOCK_FILE='/var/lock/subsys/my'
PID_FILE='/var/app/current/bin/my_pid.txt'
SERVICE_NAME='myCommand'
SERVICE_LOG_NAME='My_Command'
SERVICE_INSTALLATION_LOG_NAME='MY_COMMAND'
echo "#$SERVICE_INSTALLATION_LOG_NAME# Creating log directory"
echo
mkdir -p $LOG_DIR
touch $LOG_FILE
echo "#$SERVICE_INSTALLATION_LOG_NAME# Creating service"
echo
cat > /etc/rc.d/init.d/$SERVICE_NAME << EOF
#!/bin/bash
#
# /etc/rc.d/init.d/$SERVICE_NAME
#
# chkconfig: 345 70 30
# description: Runs $SERVICE_NAME command
# processname: $SERVICE_NAME
### BEGIN INIT INFO
# Provides: $SERVICE_NAME
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Should-Start:
### END INIT INFO
# Source function library.
. /etc/rc.d/init.d/functions
case "\$1" in
start)
echo -e "($SERVICE_LOG_NAME) - Service is starting..\n"
echo -e "($SERVICE_LOG_NAME) - Creating lockfile\n"
touch $LOCK_FILE
echo -e "($SERVICE_LOG_NAME) - Running command and directing output to $LOG_FILE\n"
nohup $COMMAND &>$LOG_FILE &
echo \$! > $PID_FILE
echo -e "($SERVICE_LOG_NAME) - Disowning this process from the current session\n"
disown
echo
;;
stop)
echo -e "($SERVICE_LOG_NAME) - Service is running..\n"
echo -e "($SERVICE_LOG_NAME) - Killing command process id\n"
kill -9 \`cat $PID_FILE\`
echo -e "($SERVICE_LOG_NAME) - Removing PID file\n"
rm -f $PID_FILE
echo -e "($SERVICE_LOG_NAME) - Removing lockfile\n"
rm -f $LOCK_FILE
;;
status)
if [ -s $PID_FILE ]
then
echo -e "Command is running on PID \$(cat $PID_FILE)\n"
echo -e "Check command logs by reading this file $LOG_FILE\n"
else
echo -e "Command is not running, PID file was not found in here $PID_FILE\n"
fi
echo
;;
restart)
;;
reload)
;;
probe)
;;
*)
echo "Usage: $SERVICE_NAME {start|stop|status|reload|restart[|probe]"
exit 1
;;
esac
EOF
chmod +x /etc/rc.d/init.d/$SERVICE_NAME
chkconfig --add $SERVICE_NAME
service $SERVICE_NAME start
echo "#$SERVICE_INSTALLATION_LOG_NAME# Created service was done"
现在,尽管它可以完美运行并创建新服务myCommand
,但我可以通过以下方式运行和控制它service myCommand start|stop|status
但是我觉得我使问题复杂化了,因为对于每个命令,我都需要在后台运行它,我必须遵循完全相同的步骤来创建自己的服务。
那么还有什么更好的方法吗?在AWS EB中部署后自动运行后台作业吗?