• Linux8.5 任务计划及系统服务


    Linux任务计划cron

      任务计划配置文件

    [root@chyuanliuNJ ~]# cat /etc/crontab
    SHELL=/bin/bash
    PATH=/sbin:/bin:/usr/sbin:/usr/bin
    MAILTO=root
    
    # 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
    

      crontab -e  定义任务计划,不写用户,默认执行root用户

    #指定任务计划
    #格式  :    分       时         日       月       周     user   command
    #范围  : 0-59  0-23  0-31  0-12   1-6
    #可用格式  : 1-5表示一个范围1到5 ; 1,2,3表示1或者2或者3
    #可用格式  :  */2表示被2整除的数字,比如小时,那就是每隔2小时,双数时间
    
    [root@chyuanliuNJ ~]# crontab -e
    
    0 3 * * * /bin/bash  /usr/local/sbin/123.sh  >>/tmp/123.log 2>>/tmp/123.log
    

      使用星期去确定唯一性,今年的8月16和明年的8月16星期肯定不一样。

      要保证服务的启动。  systemctl  start    crond

      查看cron服务状态。绿色则启动。  systemctl  status  crond

      切记,任务计划和shell脚本里的一切命令都用绝对路径。每写一个任务计划都要追加日志,可以保证该任务有据可查。

      备份任务计划,以用户名字命名的文件名

    [root@chyuanliuNJ ~]# cat /var/spool/cron/root
    0 3 * * * /bin/bash  /usr/local/sbin/123.sh  >>/tmp/123.log 2>>/tmp/123.log
    
    [root@chyuanliuNJ ~]# crontab -l
    0 3 * * * /bin/bash  /usr/local/sbin/123.sh  >>/tmp/123.log 2>>/tmp/123.log
    

       基本操作

    #列出
    [root@chyuanliuNJ ~]# crontab -l
    0 3 * * * /bin/bash  /usr/local/sbin/123.sh  >>/tmp/123.log 2>>/tmp/123.log
    #指定用户列出
    [root@chyuanliuNJ ~]# crontab -u root -l
    0 3 * * * /bin/bash  /usr/local/sbin/123.sh  >>/tmp/123.log 2>>/tmp/123.log
    #删除
    [root@chyuanliuNJ ~]# crontab -r
    [root@chyuanliuNJ ~]# crontab -u root -l
    no crontab for root
    

    Linux系统服务管理-chkconfig

       管理服务,控制服务开机启动、指定级别启动等。centos7中已经不用,趋势移弃。

    [root@chyuanliuNJ ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    agentwatch      0:off   1:off   2:on    3:on    4:on    5:on    6:off
    cloudmonitor    0:off   1:off   2:on    3:on    4:on    5:on    6:off
    mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    staragentctl    0:off   1:off   2:on    3:on    4:on    5:on    6:off
    

      服务脚本在 /etc/init.d目录下

      停止服务

    [root@chyuanliuNJ ~]# chkconfig aegis off
    [root@chyuanliuNJ ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:off   3:off   4:off   5:off   6:off
    agentwatch      0:off   1:off   2:on    3:on    4:on    5:on    6:off
    cloudmonitor    0:off   1:off   2:on    3:on    4:on    5:on    6:off
    mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
    netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
    network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off
    staragentctl    0:off   1:off   2:on    3:on    4:on    5:on    6:off
    
    #在某级别为开服务还是关闭服务
    

       指定开某服务的某级别

    [root@chyuanliuNJ ~]# chkconfig aegis --level 3 on
    [root@chyuanliuNJ ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:off   3:on    4:off   5:off   6:off
    ... ...
    
    [root@chyuanliuNJ ~]# chkconfig aegis --level 245 on
    [root@chyuanliuNJ ~]# chkconfig --list
    
    Note: This output shows SysV services only and does not include native
          systemd services. SysV configuration data might be overridden by native
          systemd configuration.
    
          If you want to list systemd services use 'systemctl list-unit-files'.
          To see services enabled on particular target use
          'systemctl list-dependencies [target]'.
    
    aegis           0:off   1:off   2:on    3:on    4:on    5:on    6:off
    ... ...
    

       在服务列表加入服务

    Sytemd管理服务

      

    systemctl  list-units  --all  --type=service
    [root@chyuanliuNJ ~]# systemctl  list-units  --all  --type=service
      UNIT                       LOAD      ACTIVE   SUB     DESCRIPTION
      aegis.service              loaded    active   running LSB: aegis update.
      agentwatch.service         loaded    active   exited  SYSV: Starts and stops g
      aliyun.service             loaded    active   running auto run aliyunservice o
      apachectl.service          loaded    active   running (null)
      atd.service                loaded    active   running Job spooling tools
    ● auditd.service             loaded    failed   failed  Security Auditing Servic
      brandbot.service           loaded    inactive dead    Flexible Branding Servic
      cloud-config.service       loaded    active   exited  Apply the settings speci
      cloud-final.service        loaded    active   exited  Execute cloud user/final
      cloud-init-local.service   loaded    active   exited  Initial cloud-init job (
    ● cloud-init-upgrade.service loaded    failed   failed  cloud-init upgrade servi
      cloud-init.service         loaded    active   exited  Initial cloud-init job (
      cloudmonitor.service       loaded    active   running LSB: @app.long.name@
      cpupower.service           loaded    inactive dead    Configure CPU power rela
      crond.service              loaded    active   running Command Scheduler
      dbus.service               loaded    active   running D-Bus System Message Bus
    ● display-manager.service    not-found inactive dead    display-manager.service
      dracut-shutdown.service    loaded    inactive dead    Restore /run/initramfs
    ... ...
    
    几个常用服务相关命令
    systemctl  enable  crond.service    让服务开机启动
    systemctl  disable  crond  不让开机启动
    systemctl  status    crond    查看状态
    systemctl  stop       crond     停止服务
    systemctl   start    crond      启动服务
    systemctl   restart    crond    重启服务
    systemctl    is-enabled  crond   检查服务是否开机启动
    

      unit介绍

      系统所有unit,分有很多类型

    service     系统服务,比如httpd
    target     多个unit组成的组,之前
    device    硬件设备
    mount    文件系统挂载点
    automount    自动挂载点
    path    文件或路径
    scope   不是由systemd启动的外部进程
    slice        进程组
    snapshot     systemd快照
    socket    进程间通信套接字
    swap      swap文件
    timer     定时器
    
    [root@chyuanliuNJ ~]# ls /usr/lib/systemd/system
    arp-ethers.service
    atd.service
    auditd.service
    autovt@.service
    basic.target
    basic.target.wants
    blk-availability.service
    bluetooth.target
    brandbot.path
    brandbot.service
    chrony-dnssrv@.service
    chrony-dnssrv@.timer
    ... ...
    

       target类似运行级别

    [root@chyuanliuNJ system]# ls -l runlevel*
    lrwxrwxrwx  1 root root   15 Aug 18 11:57 runlevel0.target -> poweroff.target
    lrwxrwxrwx  1 root root   13 Aug 18 11:57 runlevel1.target -> rescue.target
    lrwxrwxrwx  1 root root   17 Aug 18 11:57 runlevel2.target -> multi-user.target
    lrwxrwxrwx  1 root root   17 Aug 18 11:57 runlevel3.target -> multi-user.target
    lrwxrwxrwx  1 root root   17 Aug 18 11:57 runlevel4.target -> multi-user.target
    lrwxrwxrwx  1 root root   16 Aug 18 11:57 runlevel5.target -> graphical.target
    lrwxrwxrwx  1 root root   13 Aug 18 11:57 runlevel6.target -> reboot.target
    ... ...
    #target就是多个Unit组成的一个组,形成的一个target
    

       unit的相关命令

    [root@chyuanliuNJ system]# systemctl list-units   #列出正运行的unit
    ... ...
      sockets.target            loaded active active    Sockets
      swap.target               loaded active active    Swap
      sysinit.target            loaded active active    System Initialization
      timers.target             loaded active active    Timers
      systemd-tmpfiles-clean.timer loaded active waiting   Daily Cleanup of Tem
    
    LOAD   = Reflects whether the unit definition was properly loaded.
    ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
    SUB    = The low-level unit activation state, values depend on unit type.
    
    102 loaded units listed. Pass --all to see loaded but inactive units, too.
    To show all installed unit files use 'systemctl list-unit-files'.
    
    [root@chyuanliuNJ system]# systemctl list-units --all   #列出所有,包括失败的或者inactive的
    
    #列出所有inactive的unit
    [root@chyuanliuNJ system]# systemctl list-units --all --state=inactive
    
    #列出状态为active的service
    [root@chyuanliuNJ system]# systemctl list-units --type=service
    
    #查看某个服务是否为active
    [root@chyuanliuNJ system]# systemctl is-active crond.service
    active
    

      target介绍

      系统为了方便管理用target来管理unit

    #一个target就是多个Unit组合,用target来管理unit,列出所有的target
    [root@chyuanliuNJ system]# systemctl list-unit-files --type=target
    UNIT FILE                 STATE
    basic.target              static
    bluetooth.target          static
    cloud-config.target       static
    cryptsetup-pre.target     static
    cryptsetup.target         static
    ctrl-alt-del.target       disabled
    default.target            enabled
    emergency.target          static
    final.target              static
    getty.target              static
    graphical.target          static
    halt.target               disabled
    ... ...
    

       查看指定target下面有哪些unit

    [root@chyuanliuNJ system]# systemctl list-dependencies multi-user.target
    multi-user.target
    ● ├─aegis.service
    ● ├─agentwatch.service
    ● ├─apachectl.service
    ● ├─atd.service
    ● ├─auditd.service
    ● ├─brandbot.path
    ● ├─cloud-config.service
    ● ├─cloud-final.service
    ● ├─cloud-init-local.service
    ... ...
    #查看指定target下面有哪些unit
    

       查看系统默认的target

    [root@chyuanliuNJ system]# systemctl get-default
    multi-user.target
    

       设置默认的target,同样也会创建一个软链接

    [root@chyuanliuNJ system]# systemctl set-default multi-user.target
    Removed symlink /etc/systemd/system/default.target.
    Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
    

       一个service属于一种类型的unit,多个Unit组成了一个target,一个target里面包含了多个service

    [root@chyuanliuNJ system]# cat /usr/lib/systemd/system/sshd.service
    [Unit]
    Description=OpenSSH server daemon
    Documentation=man:sshd(8) man:sshd_config(5)
    After=network.target sshd-keygen.service
    Wants=sshd-keygen.service
    
    [Service]
    Type=notify
    EnvironmentFile=/etc/sysconfig/sshd
    ExecStart=/usr/sbin/sshd -D $OPTIONS
    ExecReload=/bin/kill -HUP $MAINPID
    KillMode=process
    Restart=on-failure
    RestartSec=42s
    RestartPreventExitStatus=255
    
    [Install]
    WantedBy=multi-user.target
    
  • 相关阅读:
    Python单元测试之unittest基础
    linux--硬链接和软链接
    12-8 istio核心功能实战-----可观察性(程访问遥测插件)
    12-7 istio核心功能实战-----可观察性(网络可视化)
    12-6 istio核心功能实战-----可观察性(分布式追踪)
    12-3 部署面向生产的istio-----核心组件
    12 ServiceMesh代表作istio-----12-1 ServiceMes、Istio架构原理
    11-7 Grafana看板和邮件报警
    11-6 监控落地
    11-4 部署前奏-Helm&Operator
  • 原文地址:https://www.cnblogs.com/chyuanliu/p/7976479.html
Copyright © 2020-2023  润新知