crond
是Linux
下知名的计划任务执行工具, 但是大多数Docker
镜像为了精简默认并不会安装.
这里记录下如何在Docker
中使用crond
.
crond任务和启动脚本
首先把cron任务和启动脚本保存到app目录中, 如下所示:
cron任务
cat app/cron
# check status every 10 min
*/10 * * * * root /root/app/check.sh
启动脚本
cat app/start_app.sh
#!/usr/bin/env bash
# Start crond
/usr/sbin/crond
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start crond: $status"
exit $status
fi
# Start your app
start_your_app
status=$?
if [ $status -ne 0 ]; then
echo "Failed to start app: $status"
exit $status
fi
# Naive check runs checks once a minute to see if either of the processes exited.
# This illustrates part of the heavy lifting you need to do if you want to run
# more than one service in a container. The container exits with an error
# if it detects that either of the processes has exited.
# Otherwise it loops forever, waking up every 60 seconds
while sleep 60; do
ps aux |grep crond |grep -q -v grep
PROCESS_1_STATUS=$?
ps aux |grep your_app |grep -q -v grep
PROCESS_2_STATUS=$?
# If the greps above find anything, they exit with 0 status
# If they are not both 0, then something is wrong
if [ $PROCESS_1_STATUS -ne 0 -o $PROCESS_2_STATUS -ne 0 ]; then
echo "One of the processes has already exited."
exit 1
fi
打包至镜像中
下面是使用centos7
的Dockerfile
示例:
Dockerfile
FROM centos:7
COPY . /app
RUN yum install cronie -y
RUN cp /app/cron /var/spool/cron/root
CMD ./app/start_app.sh
打包
docker build -t your_app .