zoukankan      html  css  js  c++  java
  • Linux中包管理与定时任务

    第1章 软件查询

    1.1 查询软件是否安装

    rpm -qa |grep cron 查询是否安装了这个软件.

    [root@znix ~]# rpm -qa |grep cron

    crontabs-1.10-33.el6.noarch

    cronie-1.4.4-16.el6_8.2.x86_64

    cronie-anacron-1.4.4-16.el6_8.2.x86_64

    [root@znix ~]#

    1.2 查看查询是否正在运行

    [root@znix ~]# ps -ef |grep crond

    root       1570      1  0 Sep02 ?        00:00:00 crond

    root      14941  14780  0 09:08 pts/0    00:00:00 grep --color=auto crond

    1.3 安装crond的方法

    1.3.1 crond属于哪里

    crond,不能单独安装,是软件包里所带的功能。

    yum安装的时候需要需要安装,

    [root@znix ~]# yum install crond

    Loaded plugins: fastestmirror, security

    Setting up Install Process

    Loading mirror speeds from cached hostfile

     * base: mirrors.btte.net

     * extras: mirrors.btte.net

     * updates: mirrors.sohu.com

    base                                       | 3.7 kB     00:00    

    extras                                     | 3.4 kB     00:00    

    updates                                    | 3.4 kB     00:00    

    No package crond available.

    Error: Nothing to do

    1.3.2 查询软件所属软件包

    [root@znix ~]# rpm -qf  `which crond`

    cronie-1.4.4-16.el6_8.2.x86_64

    1.3.3 如何查看软件包里的内容

    rpm -ql  显示软件包里的信息。名字必须一致.

    [root@znix ~]# rpm -ql cronie

    /etc/cron.d

    /etc/cron.d/0hourly

    /etc/cron.deny

    /etc/pam.d/crond

    /etc/rc.d/init.d/crond

    /etc/sysconfig/crond

    /usr/bin/crontab

    /usr/sbin/crond

    /usr/share/doc/cronie-1.4.4

    /usr/share/doc/cronie-1.4.4/AUTHORS

    /usr/share/doc/cronie-1.4.4/COPYING

    /usr/share/doc/cronie-1.4.4/ChangeLog

    /usr/share/doc/cronie-1.4.4/INSTALL

    /usr/share/doc/cronie-1.4.4/README

    /usr/share/man/man1/crontab.1.gz

    /usr/share/man/man5/crontab.5.gz

    /usr/share/man/man8/cron.8.gz

    /usr/share/man/man8/crond.8.gz

    /var/spool/cron

    1.3.4 如何查看一个安装的软件

    rpm  如何查询软件包的内容

    yum 安装不上的,可能是软件包的一部分

    查看 crond 属于哪个软件包,查询时要使用绝对路径

    1.4 查询ifconfig 属于哪个软件包

    [root@znix ~]#

    [root@znix ~]# rpm -qf `which ifconfig`

    net-tools-1.60-114.el6.x86_64

    [root@znix ~]# rpm -ql net-tools

    /bin/dnsdomainname

    /bin/domainname

    第2章 定时任务

    2.1 什么是定时任务

    每隔相同的时间,相同的事情(执行命令或脚本)

    2.2 系统中的定时任务

    [root@znix ~]# ls -ld /etc/cron*

    drwxr-xr-x. 2 root root 4096 Aug 10 18:37 /etc/cron.d

    drwxr-xr-x. 2 root root 4096 Aug 10 18:38 /etc/cron.daily 

    -rw-------. 1 root root    0 Aug 24  2016 /etc/cron.deny

    drwxr-xr-x. 2 root root 4096 Aug 10 18:36 /etc/cron.hourly

    drwxr-xr-x. 2 root root 4096 Aug 10 18:37 /etc/cron.monthly

    -rw-r--r--. 1 root root  457 Sep 27  2011 /etc/crontab

    drwxr-xr-x. 2 root root 4096 Sep 27  2011 /etc/cron.weekly

    /etc/cron.deny 禁止用户使用定时任务 ,把用户名放到里面

    2.2.1 日志切割-日志的轮询

    每隔一段时间,将日志进行分割

    [root@znix ~]# ls /var/log/messages*

    /var/log/messages           /var/log/messages-20170828

    /var/log/messages-20170814  /var/log/messages-20170904

    /var/log/messages-20170821

    2.2.2 日志的轮询的定时任务

    [root@znix cron.daily]# cat /etc/logrotate.d/syslog

    /var/log/cron

    /var/log/maillog

    /var/log/messages

    /var/log/secure

    /var/log/spooler

    {

        sharedscripts

        postrotate

        /bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true

        endscript

    }

    2.3 用户定时任务crontab

    2.3.1 crontab 使用

    -e  编辑

    -l  查看

    -u  设置指定用户的定时任务

    [root@znix cron.daily]# crontab --help

    crontab: invalid option -- '-'

    crontab: usage error: unrecognized option

    usage:  crontab [-u user] file

        crontab [-u user] [ -e | -l | -r ]

            (default operation is replace, per 1003.2)

            (edit user's crontab)

            (list user's crontab)

        -r  (delete user's crontab)

        -i  (prompt before deleting user's crontab)

        -s  (selinux context)

    2.3.2 指定用户的定时任务

    -u 指定用户

    [root@znix ~]# crontab -u clsn -l

    #clsn

    [root@znix ~]# ll /var/spool/cron/

    total 8

    -rw------- 1 root root  8 Sep  7 10:23 clsn

    -rw------- 1 root root 77 Sep  4 20:43 root

    2.4 定时任务的使用-

    2.4.1 看定时任务的软件的是否能用

    [root@znix ~]# ps -ef |grep cron

    root       1570      1  0 Sep02 ?        00:00:00 crond

    [root@znix ~]# service crond status

    crond (pid  1570) is running...

    [root@znix ~]# /etc/init.d/crond status

    crond (pid  1570) is running...

    2.4.2 看定时任务是否开机自启动

    [root@znix ~]# chkconfig |grep cron

    crond          0:off   1:off   2:on    3:on    4:on    5:on    6:off

    2.4.3 linux里的守护进程 (服务)

    [root@znix ~]# chkconfig |egrep "crond|network|rsyslog|sshd|sysstat"

    crond           0:off   1:off   2:on    3:on    4:on    5:on    6:off   定时任务

    network        0:off   1:off   2:on    3:on    4:on    5:on    6:off   网络服务

    rsyslog        0:off   1:off   2:on    3:on    4:on    5:on    6:off   系统日志

    sshd            0:off   1:off   2:on    3:on    4:on    5:on    6:off   远程链接服务器

    sysstat        0:off   1:on    2:on    3:on    4:on    5:on    6:off   监测系统性能及效率

    2.5 与定时任务相关的文件

    2.5.1 定时任务的日志

    [root@znix ~]# tail -2 /var/log/cron

    Sep  7 10:23:42 znix crontab[15409]: (root) LIST (clsn)

    Sep  7 10:25:01 znix CROND[15412]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1)

    2.5.2 定时任务所在目录

    [root@znix ~]# ls /var/spool/cron

    2.5.3 禁止那些用户使用定时任务

    [root@znix ~]# cat /etc/cron.deny

    2.6 linux中查看的命令日志

    对与较大的文件,查看的时候,尽量不要cat.通过其他的命令,快速找到要找的内容.

    grep /egrep

    less

    tail /tail -f

    head

    2.7 定时任务的基本格式

    * * * * *  CMD

    分时日月周 CMD

    其中 * 表示每

    /n 每隔多长时间

    2.7.1 定时任务中的时间

    [root@znix ~]# cat /etc/crontab

    SHELL=/bin/bash

    PATH=/sbin:/bin:/usr/sbin:/usr/bin

    MAILTO=root

    HOME=/

     

    # For details see man 4 crontabs

     

    # Example of job definition:

    # .---------------- minute (0 - 59)

    # |  .------------- hour (0 - 23)

    # |  |  .---------- day of month (1 - 31)

    # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...

    # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat

    # |  |  |  |  |

    # *  *  *  *  * user-name command to be executed

    2.7.2 定时任务--5分钟同步一下系统时间

    1.用什么命令,首先将命令测试一下,没有错误后,再写入定时任务.

    [root@znix ~]# ntpdate ntp1.aliyun.com

     7 Sep 10:52:57 ntpdate[15460]: adjust time server 182.92.12.11 offset 0.479762 sec

    2.写定时任务--写定时任务要使用绝对路径, 在后面加上2>&1,表示正确的,错误的都输出到同一个文件中.

    [root@znix ~]# crontab -e

    ###time tongbu

    */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1

    3.检查结果,可以看日志,也可看命令的执行结果.

    [root@znix ~]# tail -1 /var/log/cron

    Sep  7 10:55:01 znix CROND[15466]: (root) CMD (/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1)

    实例2-1 每分钟把自己的名字 追加到 /clsn/clsn.txt

    * * * * * /bin/echo clsn >> /clsn/clsn.txt

           使用tail命令查看命令的执行情况,可以看到文件的内容.

    [root@znix ~]# tail -f /clsn/clsn.txt

    clsn

    clsn

    clsn

           查看日志,查看命令执行的时间.

    [root@znix ~]# tail -3 /var/log/cron

    Sep  7 12:05:01 znix CROND[15674]: (root) CMD (/bin/echo clsn >> /clsn/clsn.txt 2>&1)

    Sep  7 12:06:01 znix CROND[15678]: (root) CMD (/bin/echo clsn >> /clsn/clsn.txt 2>&1)

    Sep  7 12:07:01 znix CROND[15681]: (root) CMD (/bin/echo clsn >> /clsn/clsn.txt 2>&1)

    2.8 注意

    错误:

    * * * * *      /bin/echo "clsn" >>/clsn/clsn.txt >/dev/null 2>&1

    实例2-2 二者不可同时使用.

    定时任务中 命令/脚本定向到空

     /dev/null 2>&1

    定时任务中 命令/脚本追加到文件中

     /clsn/clsn.txt 2>&1

  • 相关阅读:
    HDU 6071
    HDU 6073
    HDU 2124 Repair the Wall(贪心)
    HDU 2037 今年暑假不AC(贪心)
    HDU 1257 最少拦截系统(贪心)
    HDU 1789 Doing Homework again(贪心)
    HDU 1009 FatMouse' Trade(贪心)
    HDU 2216 Game III(BFS)
    HDU 1509 Windows Message Queue(队列)
    HDU 1081 To The Max(动态规划)
  • 原文地址:https://www.cnblogs.com/clsn/p/7606405.html
Copyright © 2011-2022 走看看