在docker中安装crontab的时候,踩了一些坑,在此记录一下。
先给出完整的dockerfile文件,后面再详细说明~
FROM python:3.6.8-slim-stretch
WORKDIR /home
RUN apt-get update \
&& apt-get install -y --no-install-recommends cron \
&& rm -rf /var/lib/apt/lists/* \
&& apt-get clean \
&& echo "import datetime\nprint(datetime.datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"))" >> /home/a.py \
&& echo "*/1 * * * * root python3 /home/a.py >> /home/log.txt" >> /etc/crontab
ENV LC_ALL C.UTF-8
CMD PYTHONIOENCODING=utf-8 /etc/init.d/cron start && touch log.txt && tail -f log.txt
echo输入“#!”
echo '#!/bin/bash \n /usr/bin/command args' > ./scripts.sh
利用echo输入#!
的时候,需要将双引号改为单引号,如果是双引号,则会出现如下错误
bash: !/bin/bash: event not found
这是因为echo下面用双引号的话无法解析!字符。
参考:https://www.javatang.com/archives/2012/10/11/3552844.html
echo换行
在命令行输出换行符\n
的时候,需要加-e
参数进行字符转义输出,但是在Dockerfile中echo不加-e
参数可以直接输出转义字符。
参考:https://www.cnblogs.com/alisleepy/p/15601762.html
crontab启动
参考:https://www.cnblogs.com/haoxi/p/8186901.html
Dockerfile中CMD
Dockerfile中的CMD后面如果需要运行多条命令,则可以通过&&
进行连接。