King

business intelligence ,is my love。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linux下面的任务部署大家都知道是利用crontab来实现的,至于crontab的一些类型我这里就不详细的来阐述,例如:

-e (edit user's crontab)--编辑当前的计划任务
-l (list user's crontab)--查看当前的计划任务
-r (delete user's crontab)--删除当前的计划任务
-ri (prompt before deleting user's crontab)--删除前给出确认,-r的话就是直接删除,通常使用下面的,-ri删除操作危险

king@ubuntu:/$ crontab -ri
crontab: really delete king's crontab? (y/n)

--提示你是否删除

格式:

 大概的语法知道了,下面我们就从一个0起步的人做起,此时我们的系统里面的计划任务就是一个空白,没有设置任何计划任务,也可以创建一个新用户登录查看

step1:以study 用户登录执行crontab -l命令

study@ubuntu:/$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

study@ubuntu:/$

可以看出最后面的不带#注释后面什么也没有,切换一个用计划任务的用户king,再次执行crontab -l

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/2 * * * * date >>/home/king/b1/time.txt
king@ubuntu:/$

大家可以清楚的看到这一行:*/2 * * * * date >>/home/king/b1/time.txt

这个就是一个每隔2分钟读取当前时间输出到/home/king/b1/time.txt这个文件中,下面我们切换回study用户

crontab -e

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

~
"/tmp/crontab.Aulssl/crontab" 22L, 886C

我们发现不能编辑,此时就要切换一下到编辑模式:i 进入编辑模式 输入

*/1 * * * * date >>/home/study/test/testdate.txt

然后按Esc退出编辑模式

然后按Shift+: 保存 

然后按wq 退出

编辑成功 crontab -l 查看我们编辑的任务

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
*/1 * * * * date >>/home/study/test/testdate.txt
study@ubuntu:/$

OK 部署上去了,接着看部署执行效果 cd /home/study/test/

study@ubuntu:/$ cd /home/study/test

study@ubuntu:~/test$ cat testdate--这样写
cat: testdate: No such file or directory--提示不存在
study@ubuntu:~/test$ cat testdate.txt--cat 查看文件的全称 ok 
Wed Mar 6 11:57:01 CST 2013
Wed Mar 6 11:58:02 CST 2013
Wed Mar 6 11:59:01 CST 2013
Wed Mar 6 12:00:01 CST 2013
Wed Mar 6 12:01:01 CST 2013
study@ubuntu:~/test$

最后要强调的是每一个crontab是针对当前用户的,用户king 有自己的计划任务 用户study也有自己的计划任务 

 

可以看到 在文件夹里面已经按照每分钟输入格式显示出来了。以上文章献给初学者,共勉!

 

 

posted on 2013-03-06 12:11  kingstudy  阅读(4628)  评论(0编辑  收藏  举报