shell后台定时任务crontab的用法

crontab的用法

 

语法:crontab[-u username]|-l|-r|-e|-v

-u:指定crontab job的用户

-l:列出当前crontabjob

-e:使用$EDITOR编辑crontab job

-v:列出当前crontab job的状态,有的shell可能不支持-v

 

crontab file填写格式:

每一行对应一个cron job

每一行分为六个部分,每一部分用空格隔开,同一个部分用逗号隔开

minute  hour  day_of_month  month  weekday  command

前五个域中使用*,则表示所有的时间点

minute0-59

hour0-230代表零点

day_of_month1-31

month1-12

weekday0-60代表星期天,1-6代表星期一到星期六

command:需要执行的脚本或者命令

 

$ cat test_cron.sh

#!/bin/sh       

#将系统时间输出到文件time.txt                                                                                                               

echo `date`>>time.txt

#查看当前的cronjob,系统提示没用用户habaocronjob

$ crontab -l

no crontab for habao

 

#添加cronjob

$ crontab e

#没隔一分钟执行一次

#显示当前系统时间#写在crontab文件中可方便查看定时作业的用途

* * * * * /home/habao/test_cron.sh

 

#再查看当前cronjob

$ crontab -l

# DO NOT EDIT THIS FILE - edit the master and reinstall.

# (/tmp/crontab.3285 installed on Thu Sep 22 16:28:40 2011)

# (Cron version -- $Id: crontab.c,v 2.13 1994/01/17 03:20:37 vixie Exp $)

#显示当前系统时间

* * * * * /home/habao/test_cron.sh

 

#查看输出文件time.txt

$ cat time.txt

Thu Sep 22 16:33:00 CST 2011

Thu Sep 22 16:34:00 CST 2011

Thu Sep 22 16:35:00 CST 2011

Thu Sep 22 16:36:00 CST 2011

Thu Sep 22 16:37:00 CST 2011

Thu Sep 22 16:38:00 CST 2011

Thu Sep 22 16:39:00 CST 2011

 

若要修改当前cronjobcrontab e

若要删除当前cronjobcrontab r

                                                                                                                       

 

你可能感兴趣的:(crontab,shell,职场,休闲)