Ubuntu Linux 下面的计划任务部署详解

浏览: 2072

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也有自己的计划任务 

 

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

推荐 0
本文由 kingwang 创作,采用 知识共享署名-相同方式共享 3.0 中国大陆许可协议 进行许可。
转载、引用前需联系作者,并署名作者且注明文章出处。
本站文章版权归原作者及原出处所有 。内容为作者个人观点, 并不代表本站赞同其观点和对其真实性负责。本站是一个个人学习交流的平台,并不用于任何商业目的,如果有任何问题,请及时联系我们,我们将根据著作权人的要求,立即更正或者删除有关内容。本站拥有对此声明的最终解释权。

0 个评论

要回复文章请先登录注册