书写技术成长之路

Linux Crontab 介绍

Crontab是Linux下用于管理定时任务运行的工具,常用的命令有两个crontab -ecrontab -l

有两种方式可以建定时任务

  • 第一种,每个用户都可以建立自己的crontab,直接输入crontab -e便可建立自己的crontab,建议用于临时测试。
  • 第二种,编辑文件vim /etc/crontab,这种方式便于系统的统一管理和调度,避免找不到哪些crontab是由哪些用户建的问题,推荐此方式。
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# 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-name  command to be executed

# 如上说明了crontab的基本语法,以及可以指定运行crontab的用户身份。
# 比如下面的例子指定了以www用户的身份来运行,这种指定用户的方式可以很好的避免权限所带来的一些问题。
# * * * * * www php artisan queue:work