34.任務計劃cron chkconfig systemctl管理服務 unit target

10.23 linux任務計劃cronlinux

10.24 chkconfig工具shell

10.25 systemd管理服務vim

10.26 unit介紹centos

10.27 target介紹bash

 

 

10.23 linux任務計劃cron:ssh

在linux中任務計劃是必不可少的,由於可能咱們凌晨的時候去作一些事情。多是備份數據或是重啓服務啊,這個操做的過程多是一個腳本,也有多是一個單獨的命令。不論是什麼,總得有一個時間要去執行他因此任務計劃必不可少。socket

 

 

~. cat /etc/crontab 這個文件下是任務計劃的配置文件,他會定義shell 環境變量、發送郵件給誰工具

[root@axinlinux-01 ~]# cat /etc/crontabcentos7

SHELL=/bin/bash shelldebug

PATH=/sbin:/bin:/usr/sbin:/usr/bin 變量,命令的路徑

MAILTO=root 發送郵件給誰

 

# For details see man 4 crontabs

 

# Example of job definition: (下面是他的格式)

# .---------------- minute (0 - 59) 第一位爲表示分鐘,範圍是0-59

# | .------------- hour (0 - 23) 小時,0-23

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

# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 月份1-12,能夠寫數字也能夠寫英文的簡寫

# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 星期,0-6。週日能夠寫成0或7

# | | | | |

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

這5個星表示它的 位

user-name 在root用戶下定義任務就是root。不寫的話就是root

command to be executed 表示要執行的命令,怎麼定義呢,寫法爲:

crontab -e,這樣就進入了crontab的配置文件當中

咱們寫好的他的任務,怎麼肯定他的惟一性呢。用周來肯定。由於今年的週一跟明年的週一是不同的

 

~1. crontab -u 、 -e 、 -l 、 -r

綜上所述,

-e 進入 crontab的配置文件當中。用法跟vim同樣的。按 a 進入編輯模式

-l 能夠查看寫的任務計劃,列出

-r 刪除任務

-u 指定用戶 例如,crontab -u root -r

~3. 文件 /var/spool/cron/username

crontab的文件在這個路徑裏面。是用戶對應的文件,好比root的就會寫上root的,其餘的就會寫上其餘的用戶名字,以用戶的名字命名的一個文件

~2. 格式:分 時 日 月 周

(user command 進入編輯模式以後,以分時日月周的模式來寫,以空格間隔。後面跟命令)

例如 0 3 * * * /binsbin /usr/local/sbin/123.sh (好比寫了一個腳本,就這樣寫任務計劃。天天3點執行這個腳本)

0 3 * * * /binsbin /usr/local/sbin/123.sh > /tmp/123.log 2> /tmp/123.log(天天3三點將這個腳本正確的和錯誤的都輸出到這個文件裏來。也能夠是追加>>

*表示相對應的第幾位,表示全部,天天都執行、每個月都執行、每週都執行

 

~4. 分範圍0-59,時範圍0-23,日範圍1-31,月範圍1-12,周範圍0-6

~5. 可用格式1-5表示一個範圍 1到5

0 3 1-5 * * /binsbin /usr/local/sbin/123.sh

1-5號的天天3點執行這個腳本

~6. 可用格式1,2,3表示1或者2或者3

0 0 0 */2 2,5 /binsbin /usr/local/sbin/123.sh

每兩個月的週二和週五來執行這個腳本

以逗號分隔,週二和週五

~7. 可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時。只要是被2整除的

0 3 1-5 */2 * /binsbin /usr/local/sbin/123.sh

每兩個月,1-5號的3點執行這個腳本

~8. 要保證服務是啓動狀態

systemctl start crond.service,輸入這個纔會讓他啓動這個服務

啓動以後能夠用 ps aux | grep crond 查看一下,若是有這個服務就表明他已經啓動了

也能夠用 systemctl status crond 來查看crond的狀態。有active (running)並顯示爲綠色表示已經啓動。相反的咱們systemctl stop crond 停掉crond,就會有顯示inactive (dead)並無顏色

 

須要注意的是,咱們在寫完任務計劃的時候。必需要啓動這個任務計劃。 systemctl start crond。

咱們在寫任務的時候,有用到命令,要寫命令的絕對路徑才能夠生效

任務計劃裏寫上正確和錯誤的輸出文件,以方便咱們有據可查

 

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.24 chkconfig工具:

 

linux系統的服務管理,像咱們以前接觸到的 crond、firewalld等等都是服務,那咱們能夠用chkconfig來管理這些服務。怎麼樣控制這個服務啓動,怎麼控制這個服務開機啓動,怎麼讓這個服務在指定的級別去啓動

chkconfig是centos6以前的版本用的

 

~1. 咱們先來執行 chkconfig --list 列出chkconfig管理的服務

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

 

注:該輸出結果只顯示 SysV 服務,並不包含 6之前的版本服務機制是sysv,因此只顯示sysv服務

原生 systemd 服務。SysV 配置數據 7之後得版本爲systemd服務機制,因此不會顯示

可能被原生 systemd 配置覆蓋。

 

要列出 systemd 服務,請執行 'systemctl list-unit-files'。因此想要看systemd服務的要執行該命令

查看在具體 target 啓用的服務請執行 target是systemd服務的另外一個概念

'systemctl list-dependencies [target]'。

sysv服務機制的只剩下這兩個了

netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關 。0-6狀態各表示6之前版本的運行級別

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

0 關機狀態 1單用戶 2多用戶模式(少了nfs服務) 3多用戶模式(不帶圖形)

4保留級別(暫時沒用) 5 多用戶帶圖形 6重啓

 

~2. 這些服務都是爲一個服務的腳本,目錄在

/etc/init.d/ 這個裏面

[root@axinlinux-01 ~]# ls /etc/init.d/

functions netconsole network README

後期咱們學其餘服務的時候,還會用到這個路徑。把一些啓動文件放進來,用chkconfig來管理

 

~3. chkconfg --level 3 network off 指定network服務的3級別關閉

多個級別同時關閉或打開,不用間隔直接寫就好。例如:345

0和6級別不會開啓服務,由於分別處於關機和重啓。1級別也不會開,處於單用戶模式

 

~4. chkconfig --add 123 把123服務加入到這個服務列表裏面來

[root@axinlinux-01 ~]# cd /etc/init.d 前提是要在這個目錄裏的服務,才能夠進入到列表裏來

[root@axinlinux-01 init.d]# ls

functions netconsole network README

[root@axinlinux-01 init.d]# cp network 123 爲保險咱們複製一個123,爲何要複製由於文件是有格式的,才能被識別

[root@axinlinux-01 init.d]# chkconfig --add 123 把123加入到列表裏

[root@axinlinux-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:關 123服務

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

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

 

~5. chkconfig --del 123 把123服務在列表裏刪除

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

10.25 systemd管理服務:

 

systemd是centos7系統的服務管理機制

 

~1. systemctl list-unit-files

咱們在chkconfig --list的時候,不會列出systemd的服務。可是會提示咱們

「要列出 systemd 服務,請執行 'systemctl list-unit-files'」

咱們執行這個systemctl list-unit-files,(按空格往下翻)。會發現他不只顯示servicehaiyou target等等服務,會比較亂。

systemctl list-units --all --type=service(只用這個查看systend服務,只查看service的)

由於咱們看的僅僅只是service服務,因此咱們能夠用 systemctl list-units --all --type=service。這樣列出的服務就只有service

 

 

~2.幾個經常使用的服務的命令(重點,經常使用!!!!)

systemctl enable crond.service 讓crond服務開機啓動(後綴.service可省略)

syttemctl disable crond.service 不讓crond服務開啓啓動(後綴.service可省略)

systemctl status crond 查看crond狀態

含有active (running)且爲綠色,表示已啓動

systemctl stop crond 中止crond服務

systemctl start crond 啓動crond服務

systemctl restart crond 重啓crond這個服務

systemctl is-enabled crond 檢查crond這個服務是否開機啓動

顯示enabled即爲開機啓動 顯示disabled即爲非開機啓動

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

10.26 unit介紹:

(systemd管理服務)

什麼叫unit?

經過上一節,咱們學到了systemctl,簡單知道了 enabled disabled。若是這個服務爲 enabled,他就會建立一個軟鏈接,這個軟鏈接的目錄爲

ls /usr/lib/systemd/system

[root@axinlinux-01 ~]# ls /usr/lib/systemd/system

arp-ethers.service machine.slice sysinit.target

auditd.service machines.target sysinit.target.wants

autovt@.service messagebus.service sys-kernel-config.mount

basic.target microcode.service sys-kernel-debug.mount

basic.target.wants multi-user.target syslog.socket

blk-availability.service multi-user.target.wants syslog.target.wants

會發現他有不少的類型,像target、mount、wants(目錄)。而這些類型的文件就叫作unit

 

系統全部unit分爲如下類型:

~ service 系統服務

~ target 多個組成的組

之間講到6之前的版本,有7個運行級別。而7也有相似的,就是target文件

[root@axinlinux-01 ~]# cd !$

cd /usr/lib/systemd/system 一樣在這個路徑裏

[root@axinlinux-01 system]# ls -l runlevle* 查看runlevel文件

ls: 沒法訪問runlevle*: 沒有那個文件或目錄

[root@axinlinux-01 system]# ls -l runlevel*

一樣也有7個運行級別的target,經過也是軟鏈接。他的源就是後面的文件。跟6的7個運行級別,基本對應起來了

lrwxrwxrwx. 1 root root 15 5月 29 07:11 runlevel0.target -> poweroff.target

lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel1.target -> rescue.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel2.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel3.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel4.target -> multi-user.target

lrwxrwxrwx. 1 root root 16 5月 29 07:11 runlevel5.target -> graphical.target

lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel6.target -> reboot.target

 

~ device 硬件設備

~ mount 文件系統的掛載點

~ automount 自動掛載點

~ path 文件或路徑

~ scope 不是由systend啓動的外部進程 比較陌生,熟悉就好

~ slice 進程組

~ snapshot systemd快照

~ socket 進程間通訊套接字

~ swap swap文件

~ timer 定時器

 

unit相關的命令:

~ systemctl list-units 列出列出正在運行的unit

~ systemctl list-units --all 列出全部,包括失敗的或者inactive的

~ systemctl list-units --all --state=inactive 列出inactive(閒置的)的unit

~ systemctl lit-units --type=service 列出狀態爲active(活躍的)的service

~ systemctl is-active crond.service 查看某個服務是否爲active

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

10.27 target介紹:

(systemd管理服務)

 

一個target是由多個unit組合起來的,那麼他們是什麼聯繫起來的:

系統爲了方便管理,用target來管理unit

 

~ systemctl list-unit-files --type=target 這個命令能夠列出系統裏面全部的target

~ systemctl list-dependencies multi-user.target 指定查看target(multi-user.target)下面有哪些unit

~ systemctl get-default 查看系統默認的target

就是multi-user.target

~ systemctl set-default multi-user.target 設定默認的target(multi-user.target)

從新設定的時候就會建立一個新的軟鏈接

 

! 一個service 屬於其中一種類型的unit

多個unit組成了一個target

一個target裏面包含了多個service

cat /usr/lib/systemd/system/sshd.service 看install部分 ,可看到他屬於哪一個target。例如:

cat這個目錄,查看sshd.service。看install部分就會知道 WantedBy=multi-user.target

 

總結:

系統能夠說是由多種unit組成的,這麼多的unit爲了方便管理,那麼咱們把他們歸類,歸類成若干個組,咱們把它叫作taeget。也就是說target是有多個unit的組成的。那service屬於其中一種類型的unit,一個target裏面包含了多個service

相關文章
相關標籤/搜索