Linux Crontab 命令使用

阅读更多
Linux定时任务Crontab详解: http://blog.chinaunix.net/uid-7552018-id-182133.html
Linux crontab 命令格式与详细例子: http://blog.csdn.net/ethanzhao/article/details/4406017
##新建定时任务执行脚本
Donald_Draper@rain:~> vim test.sh
echo "===============test============="
:wq

保存!

##新建定时任务
Donald_Draper@rain:~> crontab -e
no crontab for Donald_Draper - using an empty one
*/1 * * * * /home/Donald_Draper/test.sh
~
~
"crontab.XXXXelGeVT" 1L, 33C written                                                      
crontab: installing new crontab

##查看当前用户的定时任务
Donald_Draper@rain:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXelGeVT installed on Mon Oct 24 18:45:39 2016)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
*/1 * * * * /home/Donald_Draper/test.sh
Donald_Draper@rain:~> 
Donald_Draper@rain:~> sudo ls -al /var/spool/cron/ 
root's password:
total 16
drwx------  4 root root 4096 Nov  3  2014 .
drwxr-xr-x 16 root root 4096 Jan 22  2015 ..
drwxr-xr-x  2 root root 4096 Oct 24 18:45 lastrun
drwx------  2 root root 4096 Oct 24 18:45 tabs


###定时任务已经执行,并将结果发送到用户邮箱
You have new mail in /var/mail/Donald_Draper

##查看定时任务执行结果
Donald_Draper@rain:~> cat /var/mail/Donald_Draper 
From [email protected]  Mon Oct 24 18:49:01 2016
Return-Path: 
X-Original-To: Donald_Draper
Delivered-To: [email protected]
Received: by rain.site (Postfix, from userid 1001)
        id 2DFFF99FCA; Mon, 24 Oct 2016 18:49:01 +0800 (CST)
From: [email protected]
To: [email protected]
Subject: Cron  /home/Donald_Draper/test.sh
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
X-Cron-Env: 
Message-Id: <[email protected]>
Date: Mon, 24 Oct 2016 18:49:01 +0800 (CST)
===============test=============


##系统定时任务存储位置
Donald_Draper@rain:~> sudo ls -al /var/spool/cron/tabs
total 16
drwx------ 2 root root  4096 Oct 24 18:45 .
drwx------ 4 root root  4096 Nov  3  2014 ..
-rw-r--r-- 1 root users  235 Oct 24 18:45 Donald_Draper
-rw-r--r-- 1 root root   253 Feb 13  2015 root


###查看定时任务
Donald_Draper@rain:~> sudo ls -al /var/spool/cron/tabs/Donald_Draper
-rw-r--r-- 1 root users 235 Oct 24 18:45 /var/spool/cron/tabs/Donald_Draper
Donald_Draper@rain:~> sudo cat /var/spool/cron/tabs/Donald_Draper
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXelGeVT installed on Mon Oct 24 18:45:39 2016)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
*/1 * * * * /home/Donald_Draper/test.sh
Donald_Draper@rain:~> sudo ls -al /var/spool/cron/lastrun    
total 8
drwxr-xr-x 2 root root 4096 Oct 24 18:45 .
drwx------ 4 root root 4096 Nov  3  2014 ..
-rw-r--r-- 1 root root    0 Oct 24 17:15 cron.daily
You have new mail in /var/mail/Donald_Draper


###查看运行的定时任务
Donald_Draper@rain:~> sudo cat /var/spool/cron/lastrun/cron.daily


##删除指定用户的定时任务
Donald_Draper@rain:~>  sudo crontab -u Donald_Draper -r


##列出当前用户的定时任务
Donald_Draper@rain:~> crontab -l
no crontab for Donald_Draper

你可能感兴趣的:(linux)