FROM ubuntu:latest RUN apt-get update && apt-get -y install cron # Add the cron job file ADD crontab /etc/cron.d/crontab # Give execution rights on the cron job RUN chmod 0644 /etc/cron.d/crontab # Apply cron job RUN crontab /etc/cron.d/crontab # Create the log file to be able to run tail RUN touch /var/log/cron.log # Install required command RUN apt-get -y install bc # Start the command CMD cron && tail -f /var/log/cron.log
Dockerfile 安装了 cron 和 bc 命令,并将 crontab 文件添加到 /etc/cron.d/ 目录中。接下来,设置文件权限和应用 crontab 文件。然后创建一个日志文件,并启动 cron 和 tail 命令以查看日志。
crontab 文件的内容:
0 0 * * * /bin/bash -c 'while true; do for i in {1..100000}; do j=$((i*i)); done; done' >> /var/log/cron.log 2>&1
0 1 * * * /usr/bin/pkill bash >> /var/log/cron.log 2>&1
文件将在每天的 00:00 启动一个 while 循环,并在 01:00 停止该循环。循环将运行所需的命令,并将输出重定向到日志文件中。此外,在 01:00,该文件将通过 pkill 命令停止正在运行的循环。
version: '3' services: crontab: build: . container_name: crontab volumes: - ./cron.log:/var/log/cron.log
bc 命令
bc
是一个计算器程序,它支持任意精度算术运算和数学函数。它通常用于在 shell 脚本中执行数学计算和简单的数据处理任务。
bc
的常见用法包括:
- 执行简单的算术运算,如加、减、乘、除、模运算等。
- 计算数学函数,如三角函数、指数函数、对数函数等。
- 读取和处理文本数据,如在脚本中解析数字和进行条件判断等。
# 执行算术运算 $ echo "3 + 4" | bc 7 # 计算数学函数 $ echo "scale=3; s(1)" | bc -l .841 # 处理文本数据 $ echo "12.5 * 2" | bc 25