Skip to content

Latest commit

 

History

History
74 lines (58 loc) · 1.95 KB

2016-11-30-crontab.md

File metadata and controls

74 lines (58 loc) · 1.95 KB

crontab

cron

Linux、Unix、*nix 等系统自带的一个跑定时任务的守护进程。该进程每分钟定期检查是否有需要执行的任务,有则执行。

crontab

crontab 用于管理需要 cron 执行的定时任务。

  • 系统级任务

    • 存放在 /etc/crontab 中
    • 可通过 crontab -uroot -e 将 root 用户的任务写入 /etc/crontab 中
  • 用户级任务

    • 可存放在系统级任务调度中但是不建议这么做,反之不行

参数

  • -e edit
    • * 表所有可能值
    • , 列出可能值
    • - 表可能值范围
    • / 表频率
# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * [user] command
# 系统任务调度中需要写入任务用户
*/10 1-3 1,10 * * command
# 表示每年每个月的一号和十号的一点两点三点每过十分钟执行一次 command

45 10 * * 1-5

  • -l list
  • -r remove

遇到的问题

$ crontab -e
crontab: "/usr/bin/vi" exited with status 1
// solution,似乎由于 vi 在编辑时出了一些问题
$ export EDITOR=vim
$ crontab -e
crontab: temp file must be edited in place
// solution
// 在 .vimrc 中添加,不生成备份文件
autocmd filetype crontab setlocal nobackup nowritebackup

环境变量

  • 使用绝对路径
  • 引入所需环境变量
    • cron 执行命令是无法使用你在 shell 中设置的环境变量的,所以需要你手动引入

Donating to Wikipedia

Imagine a world in which every single person on the planet has free access to the sum of all human knowledge. —Jimmy Wales, Founder of Wikipedia

donate here

1496 days left