zoukankan      html  css  js  c++  java
  • Ubuntu Linux 下面的计划任务部署详解

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

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

    英文名:kingwang & Email :kingstudy@vip.qq.com
  • 相关阅读:
    Visual C#核心编程之泛型
    Visual C#核心编程之枚举器
    标准的非托管资源的销毁模式
    Visual C#核心编程之LINQ
    Visual C#核心编程之数组和集合
    ACCPSQL第四章上机六
    Visual C#2008核心编程之类型
    一月一代码 3月 kmp 领悟代码
    [转] 技巧 如何统一设置 windows live writer 的 图片大小
    understanding the linux virtual memory management 图序
  • 原文地址:https://www.cnblogs.com/wxjnew/p/2945810.html
Copyright © 2011-2022 走看看