Linux基礎(day36)

10.23 linux任務計劃cron

Linux任務計劃

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

crontab命令

  • crontab -e //編輯
  • crontab -l //列出 -crontab -r //刪除
  • crontab -u username -l //指定用戶

任務計劃

  • crontab -e //編輯
  • 在linux系統中,系統計劃是必不可少的,好比備份數據,重啓服務等
    • 操做過程,多是一個腳本,有多是一個單獨的命令,在特定的時間去執行它,因此任務計劃是不可缺乏的
  • 在windows中都是使用的我的電腦,因此任務計劃不多見,幾乎用不到

linux中計劃的配置文件

  • cat /etc/crontab //查看任務計劃的配置文件
    • 文件中會定義幾個變量
      • SHELL=/bin/bash
      • PATH環境變量,它命令的路徑
      • MAILTO發郵件給誰
[root@hanfeng ~]# 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        //星期,0或7都表示週日,也能夠寫成英文的簡寫
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed        //用戶,不寫用戶就是root 最後一列,是你要執行的命令

[root@hanfeng ~]#
  • crontab -e 進入到crontab的配置文件中,用法和vim同樣
    • 按 i 進入編輯模式
    • 分鐘,小時,日,月,星期,而後後面跟具體的命令
    • 凌晨3點去執行,* 表示全部的意思
天天凌晨三點,執行123.sh腳本文件,正確的和錯誤的日誌都輸出到123.log文件中

0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >/tmp/123.log 2>/tmp/123.log

由於是天天三點執行腳本,因此能夠寫成追加,天天都去記錄日誌

0 3 * * *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

若想1-10號,雙月去執行該腳本,後面就不在執行了——>只要 被2 整除,就符合條件

0 3 1-10 */2 *  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

只要周2和周5執行該文件

0 3 1-10 */2 2,5  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
  • 爲何沒有年份?
  • 用星期肯定你的惟一性,好比說今年的6月18號和明年的6月18號的星期確定是不一樣的,這樣就能夠肯定某一天的惟一性

啓動crond服務

  • 若想要這個任務正常使用,還須要去啓動crond服務
    • systemctl start crond.service //啓動crond服務
  • 若想檢查服務是否成功啓動,
    • 方法一:可以使用ps aux |grep cron 命令查看
      • 如有這個進程,說明這個服務已經啓動了
    • 方法二:使用systemctl start crond 查看狀態
      • 如果 綠色 ,則表示該服務已經啓動了
      • 如果該服務已經停掉了,則不會有顏色

任務計劃不執行的緣由分析

  • 在寫了一個計劃,放入到配置文件中,但就是不執行
  • 不執行的緣由頗有多是你寫的腳本里面,沒有使用 絕對路徑 的緣由致使不執行
    • 由於頗有可能,你在使用的命令不在PATH裏面,因此要麼將命令寫一個絕對路徑,要麼將命令的路徑加入到PATH變量裏面去
  • 建議:在寫一個腳本的時候,都要寫追加一個日誌,這樣能夠保證這個任務有據可查,再不執行的時候,查看錯誤日誌便可

任務計劃備份

  • crontab -l //列出
  • crontab文件存在位置/var/spool/cron/username
  • 在須要備份的時候,直接把這個文件,或者目錄拷貝下便可

10.24 chkconfig工具

Linux系統服務管理-chkconfig

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

chkconfig工具

  • crond、iptables、firewalld、nginx、httpd、mysql等等,都屬於服務。
  • chkconfig工具,在centos6和以前的版本中,控制服務的啓動;但在centos7中不多使用了,但爲了兼容以前的版本,依然可使用,但在將來的趨勢中, 有可能就會被遺棄了,如今就是過分的做用。
  • chkconfig --list //列出全部的系統服務
    • 表示chkconfig工具在centos6或以前的版本中,使用的服務的管理的機制叫 SysV,而centos7的版本中,使用的是 systemd 服務
[root@hf-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:關
[root@hf-01 ~]#

chkconfig命令

  • 服務的腳本存放在 /etc/init.d/ 下面
    • 啓動腳本存放該目錄下
[root@hf-01 ~]# ls /etc/init.d/
functions  netconsole  network  README
[root@hf-01 ~]#
  • chkconfig --list //列出全部的服務
  • chkconfig network off //將network服務關閉
[root@hf-01 ~]# chkconfig network off
[root@hf-01 ~]# chkconfig --list    //會看到2,3,4級別關閉了

注意:該輸出結果只顯示 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@hf-01 ~]# chkconfig network on
[root@hf-01 ~]# chkconfig --list    //會看到2,3,4級別又開啓了

注意:該輸出結果只顯示 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@hf-01 ~]#
  • 在系統中有七個級別等級列表:html

    • 等級0表示:表示關機
    • 等級1表示:單用戶模式
    • 等級2表示:多用戶模式,少nfs服務
    • 等級3表示:多用戶模式,不帶圖形
    • 等級4表示:是一種保留的級別
    • 等級5表示:帶圖形界面的多用戶模式
    • 等級6表示:從新啓動
  • 在centos6中的 /etc/inittab 中定義開機的級別mysql

  • 在centos7中,已經沒有用了,不須要定義開機的級別了linux

chkconfig命令,指定某一級別開啓/關閉

  • chkconfig --level 3 network off //指定network中的3級別關閉
[root@hf-01 ~]# chkconfig --level 3 network off        //指定network中的3級別關閉
[root@hf-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:關
[root@hf-01 ~]#
  • chkconfig --level 345 network on //指定network中的3,4,5級別開啓
[root@hf-01 ~]# chkconfig --level 345 network on        //指定network中的3,4,5級別開啓
[root@hf-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:關
[root@hf-01 ~]#
  • 0和1和6級別不能設置成開
    • 0級別在關機狀態是不可能開啓的
    • 1級別是單用戶模式,服務是不可能開啓的
    • 6級別在重啓的時候,是不可能開啓的——>重啓至關於先關閉在啓動(重啓的那一刻是先關閉纔對)。

將一個腳本加入到服務列表中

  1. 首先將啓動腳本放入到 /etc/init.d 這個目錄下——>只有在這個目錄下,才能夠添加到服務列表中去
  2. 文件名稱無所謂,但內容有格式要求
    • 首先是是一個shell腳本
    • 而後chkconfig指定運行級別啓動順序,第10位啓動,第90位關閉
    • 下面代碼爲它的固定格式,必需要有的!!!
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
#              start at boot time.
  • 例子:
[root@hf-01 ~]# cd /etc/init.d
[root@hf-01 init.d]# ls
functions  netconsole  network  README
[root@hf-01 init.d]# cp network 123
[root@hf-01 init.d]# ls -l
總用量 40
-rwxr-xr-x  1 root root  7293 12月  5 05:27 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 10月 20 11:07 README
[root@hf-01 init.d]# 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@hf-01 init.d]# chkconfig --add 123    //將123加入到服務列表中
[root@hf-01 init.d]# 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:關
[root@hf-01 init.d]# chkconfig --del 123    //刪除服務列表中的腳本
[root@hf-01 init.d]# 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@hf-01 init.d]#
  • chkconfig --del network //刪除服務列表中的腳本
  • chkconfig --add network //增長服務列表中的腳本

10.25 systemd管理服務

Linux系統服務管理-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-enabled crond //檢查服務是否開機啓動

systemd工具

  • systemd是centos7管理的一個服務機制,在centos6或以前的版本中可使用chkconfig工具去管理系統的服務,在centos7中,也可使用,但會提示使用 systemctl list-unit-files ,用它來查看全部的服務。
  • systemctl list-unit-files //查看全部的服務
[root@hf-01 init.d]# 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@hf-01 init.d]# systemctl list-unit-files    //查看全部的服務,裏面不只有service,還有socket,還有target
UNIT FILE                                   STATE   
proc-sys-fs-binfmt_misc.automount           static  
dev-hugepages.mount                         static  
dev-mqueue.mount                            static  
proc-sys-fs-binfmt_misc.mount               static  
sys-fs-fuse-connections.mount               static  
sys-kernel-config.mount                     static  
sys-kernel-debug.mount                      static  
tmp.mount                                   disabled
brandbot.path                               disabled
等等

systemd相關的命令

  • systemctl list-units --all --type=service //列出全部的service
    • 會列出全部的service
    • 列出描述信息,是不是loaded,是不是active
    • 按 空格 往下翻
    • 如果不加 --all ,則就不會列出 未激活的active
[root@hf-01 ~]# systemctl list-units --all --type=service    //列出全部的service
  UNIT                           LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                 loaded    active   running Security Auditing Service
  avahi-daemon.service           loaded    active   running Avahi mDNS/DNS-SD Stack
  brandbot.service               loaded    inactive dead    Flexible Branding Service
  cpupower.service               loaded    inactive dead    Configure CPU power related 
  crond.service                  loaded    active   running Command Scheduler

等等等,只截取了一部分
並在最下面,會告訴你 LOAD,ACTIVE,SUB是什麼意思
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.

還會提醒,若想列出全部的 unit files,請使用 systemctl list-unit-files 命令
  • systemctl enable crond.service //讓服務開機啓動——>service可省略
  • systemctl disable crond //不讓開機啓動
[root@hf-01 ~]# systemctl enable crond.service        //讓服務開機啓動
[root@hf-01 ~]# systemctl disable crond.service        //不讓開機啓動
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@hf-01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@hf-01 ~]#
  • systemctl status crond //查看狀態
[root@hf-01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 二 2017-12-05 01:37:49 CST; 5h 15min ago
 Main PID: 574 (crond)
   CGroup: /system.slice/crond.service
           └─574 /usr/sbin/crond -n

12月 05 01:37:49 hf-01 systemd[1]: Started Command Scheduler.
12月 05 01:37:49 hf-01 systemd[1]: Starting Command Scheduler...
12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (RANDOM_DELAY will be scaled with ...d.)
12月 05 01:37:50 hf-01 crond[574]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.
[root@hf-01 ~]#
  • systemctl stop crond //中止服務
  • systemctl start crond //啓動服務
  • systemctl restart crond //重啓服務
  • systemctl is-enabled crond //檢查服務是否開機啓動
[root@hf-01 ~]# systemctl is-enabled crond
enabled
[root@hf-01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@hf-01 ~]# systemctl is-enabled crond
disabled
[root@hf-01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@hf-01 ~]#
  • 而且能夠經過輸出信息,在 /etc/systemd/system/multi-user.target.wants/crond.service 得到service的配置文件內容
[root@hf-01 ~]# cat /etc/systemd/system/multi-user.target.wants/crond.service    得到service的配置文件內容
[Unit]
Description=Command Scheduler
After=syslog.target auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
KillMode=process

[Install]
WantedBy=multi-user.target

[root@hf-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service        //是一個軟鏈接,從軟連接的右邊到左邊
lrwxrwxrwx 1 root root 37 12月  5 06:55 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@hf-01 ~]# ls -l /usr/lib/systemd/system/crond.service    //這裏纔是文件真正的路徑
-rw-r--r--. 1 root root 263 6月  10 2014 /usr/lib/systemd/system/crond.service
[root@hf-01 ~]#

10.26 unit介紹

unit介紹

  • ls /usr/lib/systemd/system //系統全部unit,分爲如下類型:
    • service 系統服務
    • target 多個unit組成的組
    • device 硬件設備
    • mount 文件系統掛載點
    • automount 自動掛載點
    • path 文件或路徑
    • scope 不是由systemd啓動的外部進程
    • slice 進程組
    • snapshot systemd快照
    • socket 進程間通訊套接字
    • swap swap文件
    • timer 定時器

unit相關的命令

  • systemctl list-units //列出正在運行的unit
    • 並會提示,若要列出全部的units,則須要加 --all
  • systemctl list-units --all //列出全部,包括失敗的或者inactive的
  • systemctl list-units --all --state=inactive //列出inactive的unit
  • systemctl list-units --type=service //列出狀態爲active的service
    • 其中failed是一個特例,也會列出來
  • systemctl is-active crond.service //查看某個服務是否爲active

10.27 target介紹

target介紹

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

target相關命令

  • systemctl list-unit-files --type=target //列出系統中全部的target
[root@hf-01 system]# systemctl list-unit-files --type=target    //列出系統中全部的target
UNIT FILE                 STATE   
basic.target              static  
bluetooth.target          static  
cryptsetup-pre.target     static  
cryptsetup.target         static  
ctrl-alt-del.target       disabled
default.target            enabled 
emergency.target          static  
等等等,只截取了一部分
  • systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
  • systemctl get-default //查看系統默認的target
  • systemctl set-default multi-user.target //設置默認的target
[root@hf-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@hf-01 system]# ls /etc/systemd/system/default.target
/etc/systemd/system/default.target
[root@hf-01 system]# ls -l !$
ls -l /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 41 12月  5 07:49 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target
[root@hf-01 system]#
  • 一個service屬於一種類型的unit
  • 多個unit組成了一個target
  • 一個target裏面包含了多個service
  • cat /usr/lib/systemd/system/sshd.service 看[install]部分
[root@hf-01 system]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@hf-01 system]#
  • 只有multi-user.target 裏面的service能夠設置開機啓動。其餘的target設置成默認啓動沒法正常啓動。

擴展

  1. anacronnginx

  2. xinetd服務sql

  • 默認機器沒有安裝這個服務,須要yum install xinetd安裝
  1. systemd自定義啓動腳本
相關文章
相關標籤/搜索