1、查看当前用户的 crontab 文件:

crontab -l

2、编辑当前用户的 crontab 文件:

crontab -e

3、删除当前用户的 crontab 文件:

crontab -r

4、列出系统上所有用户的 crontab 任务:

sudo crontab -l

5、重启 cron 服务:

sudo service cron restart  或者  sudo systemctl restart cron

5、查看 cron 服务状态:

sudo service cron status  或者  sudo systemctl status cron

6、环境变量:
默认情况下,cron 任务不会加载用户的环境变量。如果需要环境变量,可以将其添加到 crontab 文件中,或者使用 env 命令将它们传递给 cron。

在编辑 crontab 文件时,每一行都代表一个计划任务,格式如下

*     *     *   *    *        command to be executed
-     -     -   -    -
|     |     |   |    |
|     |     |   |    +----- day of the week (0 - 6) (Sunday=0)
|     |     |   +------- month (1 - 12)
|     |     +--------- day of the month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

例如,下面的 crontab 条目表示每天凌晨 1 点执行 /path/to/script.sh 脚本:

0 1 * * * /path/to/script.sh

使用 crontab 时,请确保正确设置时间,并且脚本有执行权限。

标签: none

添加新评论