cron 是一个 Linux 下的定时执行工具,可以在无需人工干预的情况下运行作业。守护进程 cron 会读取 crontab 文件,根据配置在指定的时间执行任务。 contab 命令用来添加、删除、显示 cron 任务表。 (Ubuntu 环境)可以通过 service 命令启停 cron 服务: service cron status # 查看 cron 服务状态 service cron start # 启动 cron 服务 service cron stop # 停止 cron 服务 service cron restart # 重启 cron 服务 service cron reload # 重新加载 cron 服务配置 crontab 命令 命令格式 crontab [-u user] file crontab [-u user] [-l | -r | -e] [-i] [-s] 命令参数 -u 指定用户 -l 显示当前的 crontab 文件内容。 -r 删除当前的 crontab 文件内容。 -e 编辑当前的 crontab 文件内容,如果文件不存在则创建新文件。 -i 在删除 crontab 文件内容时予以提示。 crontab 文件 格式:minute hour day_of_month month day_of_week command
实例 a) 添加 cron 任务 huey@huey-K42JE:~/huey/linux/cmdline$ echo "*/3 * * * * date >> ~/huey/linux/cmdline/cron.out" > mycron huey@huey-K42JE:~/huey/linux/cmdline$ crontab mycron b) 删除 cron 任务 huey@huey-K42JE:~/huey/linux/cmdline$ crontab -r c) 显示 cron 任务 huey@huey-K42JE:~/huey/linux/cmdline$ crontab -l */3 * * * * date >> ~/huey/linux/cmdline/cron.out