1. 程式人生 > >計劃任務及服務管理

計劃任務及服務管理

計劃任務 服務管理 systemd

Linux任務計劃
  • 任務計劃的配置文件及詳解
    [root@akuilinux02 ~]# cat /etc/crontab 
    SHELL=/bin/bash  ##定義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   再加上用戶和命令
  • 制定任務計劃的格式
    • 分範圍0-59,時範圍0-23,日範圍0-31,月範圍0-12,周1-6
    • 格式1,2,3表示1或者2或者3
    • 格式*/2表示被2整除的數字,比如小時就是每隔2小時
      [root@akuilinux02 ~]# crontab -e
      no crontab for root - using an empty one
      0 3 * * * /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log  ##每天的三點,這裏的命令要寫絕對路徑
      0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log ##1-10號的3點,每兩個月(雙月),周2和周五
  • 制定任務計劃的條件
    • 啟動服務crond
      [root@akuilinux02 ~]# systemctl start crond
      [root@akuilinux02 ~]# ps aux |grep crond
      root       587  0.0  0.0 126264  1644 ?        Ss   08:40   0:00 /usr/sbin/crond -n
      root      1321  0.0  0.0 112680   984 pts/0    S+   09:10   0:00 grep --color=auto crond
      [root@akuilinux02 ~]# systemctl status crond
      ● crond.service - Command Scheduler
      Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
      Active: active (running) since 六 2018-05-12 08:40:05 CST; 32min ago
    • 遵照格式編寫,命令要寫絕對路徑,要輸出到日誌文件
    • crontab命令
    • crontab -e編寫任務計劃
    • crontab -r刪除任務計劃
    • crontab -l查看任務計劃
    • crontab -u root -l指定用戶
    • /var/spool/cron/root任務計劃的保存文件,用戶名為用戶的文件
      [root@akuilinux02 ~]# crontab -e
      crontab: installing new crontab
      [root@akuilinux02 ~]# crontab -l
      1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
      [root@akuilinux02 ~]# cat /var/spool/cron/root 
      1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
      [root@akuilinux02 ~]# crontab -r
      [root@akuilinux02 ~]# crontab -l -u root
      no crontab for root

      系統服務管理chkconfig

  • 7以前的版本使用的,管理的服務稱為SysV服務,7以後為systemd服務
    [root@akuilinux02 ~]# 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:關
  • 服務所在的目錄
    [root@akuilinux02 ~]# ls /etc/init.d/
    functions  netconsole  network  README
  • chkconfig --list列出服務狀態
  • chkconfig network on 開啟服務開機啟動
    • 開啟的為默認級別啟動
  • chkconfig --level 345 network off指定級別
  • 自定義啟動腳本並開機啟動
    
    [root@akuilinux02 ~]# cp /etc/init.d/network /etc/init.d/123
    [root@akuilinux02 ~]# ls /etc/init.d/
    123  functions  netconsole  network  README
    [root@akuilinux02 ~]# chkconfig --add 123
    [root@akuilinux02 ~]# chkconfig --list

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

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

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


- chkconfig --del 123刪除服務
# 系統服務管理systemd
- 列出服務
  - systemctl list-unit-files列出所有的服務及服務狀態
  - systemctl list-units --all --type=service列出所有service服務包括了未激活和激活的,去掉--all就不會列出未激活的
- 控制服務啟動命令
  - 

計劃任務及服務管理