1 安装crontab
但ubuntu18.01是默认安装crontab的,不过可以安装试试,已经安装会有提示的
apt-get install cron
2 启动crontab
先确定用户级别,可以是普通用户(建议),也可以是root用户。
默认是已经运行于后台,也可以试着启动以下:
service cron start
3 查看crontab状态
service cron status
4 修改crontab的默认编辑器
select-editor
然后选择自己想要的编辑器
5 crontab的3个常用命令
crontab -l # 查看
crontab -e # 编辑
crontab -r # 删除
6 crontab内容的格式
分 | 时 | 日 | 月 | 周 | 将要执行的命令 |
---|---|---|---|---|---|
* | * | * | * | * | command |
1~59 | 0~23 | 1~31 | 1~12 | 0~6 |
时间默认用 * 号代替,根据定时的时间,用数字去替换,例如:
1 * * * * /home/user/test.sh # 表示每小时的第1分钟执行一次test.sh脚本
*/1 * * * * /home/user/test.sh # 表示每1分钟执行一次test.sh脚本
1 1 * * * /home/user/test.sh # 每月每天凌晨1点1分执行test.sh脚本
7 crontab -e 于 vim /etc/crontab 的区别
crontab -e 修改的文件在 /var/spod/cron 中,一般用于普通用户级;
/etc/crontab 用于 root 用户级别。