Linux 添加定時任務,crontab -e 命令與直接編輯 /etc/crontab 文件


1. 使用 crontab -e 命令編輯定時任務列表

使用這個命令編輯的定時任務列表是屬於用戶級別的,初次編輯后在 /var/spool/cron 目錄下生成一個與用戶名相同的文件,文件內容就是我們的定時任務列表。如沒有定時任務,這個文件就是空文件。

crontab命令還有一些其他的選項

  -u #指定哪個用戶的cron服務,一般是root用戶執行這個命令的時候需要

  -l #列出用戶的定時任務列表,默認當前用戶

  -r #刪除用戶的定時任務列表,默認當前用戶 

  -e #編輯用戶的定時任務列表,默認當前用戶

例子:列出xiaoming用戶的cron服務列表

crontab -u xiaoming -l

 

2. 直接編輯 /etc/crontab 文件,命令如下:

編輯 /etc/crontab 文件只有 root 用戶才行

vim /etc/crontab

我們會看到文件內容,如下:

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# 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

這配置的定時任務屬於系統級別的。

 

3. 其他的一些區別

crontab -e 會進行語法檢查、直接編輯 /etc/crontab 文件則不會

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM