1. 程式人生 > >2018-03-26 Linux學習

2018-03-26 Linux學習

Linux學習

10.23 Linux任務計劃

crontab -u、-e、-l、-r
格式:分 時 日 月 周 user command
文件  /var/spool/cron/username
分範圍 0-59;時範圍 0-23;日範圍 0-31;月範圍 0-12;周範圍1-6
可用格式 1-5 表示一個範圍1到5
可用格式 1,2,3 表示1或2或3
可用格式 */2 表示被2整除的數字,比如小時,那就是每隔2小時
要保證服務是啟動狀態
systemctl start crond.service

    [root@aming-01 ~]# 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  編輯任務
crontab -l  列出任務
crontab -u root -l  列出指定用戶任務

10.24 chkconfig 工具

    chkconfig --list
    chkconfig --level 3 network off
    chkconfig --level 345 network off
    chkconfig --del network
    chkconfig --add network

        [root@aming-01 ~]# chkconfig --list

        註:該輸出結果只顯示 SysV 服務,並不包含
        原生 systemd 服務。SysV 配置數據
        可能被原生 systemd 配置覆蓋。 

              要列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。
              查看在具體 target 啟用的服務請執行
              ‘systemctl list-dependencies [target]‘。

        netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關
        network         0:關 1:關 2:開 3:開 4:開 5:開 6:關

10.25 systemd

systemctl list-units --all --type=service

幾個常用的服務相關的命令
    systemctl enable crond.service    //讓服務開機啟動
    systemctl disable crond    //禁止開機啟動
    systemctl status crond    //查看狀態
    systemctl stop crond      //停止服務
    systemctl start crond    //啟動服務
    systemctl restart crond   //重啟服務
    systemctl is-enable crond   //檢查服務是否啟動

        [root@aming-01 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service 
        [Unit]
        Description=Command Scheduler
        After=auditd.service systemd-user-sessions.service time-sync.target

        [Service]
        EnvironmentFile=/etc/sysconfig/crond
        ExecStart=/usr/sbin/crond -n $CRONDARGS
        ExecReload=/bin/kill -HUP $MAINPID
        KillMode=process

        [Install]
        WantedBy=multi-user.target

        [root@aming-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service 
        lrwxrwxrwx. 1 root root 37 2月  28 05:03 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

10.26 unit 介紹

ls /usr/lib/systemd/system //系統所有 unit,分為以下類型

service  系統服務
target   多個unit組成的組
device   硬件設備
mount    文件系統掛載點
automount  自動掛載點
path     文件或路徑
scope    不是由systemd啟動的外部進程
slice    進程組
snapshot systemd 快照
socket   進程間通信套接字
swap     swap 文件
timer    定時器

    [root@aming-01 system]# cd /usr/lib/systemd/system
    [root@aming-01 system]# ls -l runlevel*
    lrwxrwxrwx. 1 root root 15 3月   2 04:46 runlevel0.target -> poweroff.target
    lrwxrwxrwx. 1 root root 13 3月   2 04:46 runlevel1.target -> rescue.target
    lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel2.target -> multi-user.target
    lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel3.target -> multi-user.target
    lrwxrwxrwx. 1 root root 17 3月   2 04:46 runlevel4.target -> multi-user.target
    lrwxrwxrwx. 1 root root 16 3月   2 04:46 runlevel5.target -> graphical.target
    lrwxrwxrwx. 1 root root 13 3月   2 04:46 runlevel6.target -> reboot.target

unit 相關的命令
systemctl list-units    //列出正在運行的unit
systemctl list-units --all   //列出所有,包括失敗的或者inactive的
systemctl list-units --all --state=inactive   //列出inactive的unit
systemctl list-units --type=service   //列出狀態為active的service
systemctl is-active crond.service  //查看某個服務是否為active

    [root@aming-01 system]# systemctl is-active crond.service 
    active
    [root@aming-01 system]# systemctl is-enabled crond.service 
    enabled

10.27 target 介紹

系統為了方便管理用 target 來管理 unit
systemctl list-unit-files --type=target
systemctl list-dependencies multi-user.target  //查看指定 target 下面有哪些 unit
systemctl get-default   //查看系統默認的target
systemctl set-default multi-user.target   //設置系統默認 target
一個 service 屬於一種類型的 unit
多個 unit 組成了一個 target
一個 target 裏面包含了多個 service
cat /usr/lib/systemd/system/sshd.service   //看 [install] 部分

    [root@aming-01 system]# systemctl get-default
    multi-user.target
    [root@aming-01 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.

    [root@aming-01 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

    [Install]
    WantedBy=multi-user.target

2018-03-26 Linux學習