1. 程式人生 > >10.23-10.27 corn, chkconfig, systemd, unit, target

10.23-10.27 corn, chkconfig, systemd, unit, target

corn chkconfig systemd unit target

八周一次課(3月26日)
10.23 linux任務計劃cron
10.24 chkconfig工具
10.25 systemd管理服務
10.26 unit介紹
10.27 target介紹

擴展
1. anacron 技術分享圖片http://blog.csdn.net/strikers1982/article/details/4787226
2. xinetd服(默認機器沒有安裝這個服務,需要yum install xinetd安裝) 技術分享圖片http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定義啟動腳本 技術分享圖片http://www.jb51.net/article/100457.htm


10.23 linux任務計劃cron


介紹

技術分享圖片任務計劃,在linux上是必不可少的。


任務計劃的配置文件

[root@centos7-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



MAILTO=root 發送郵給指定root用戶

SHELL=/bin/bash 使用的shell

PATH=/sbin:/bin:/usr/sbin:/usr/bin 環境變量,使用的命令路徑


日期

***** 從左到右:

分鐘

月(可以英文簡寫,jan,feb,mar,apr)

周幾(0-6,0和7表示星期日,可以英文簡寫sun,mon,tue,wed,thu,fri,sat)

用戶(不寫用戶名表示root)

最後一行是命令


格式演示

進入任務計劃配置文件命令#crontab -e,這操作跟vim用法一樣,只是換了方式來打開配置文件,但是不能直接用vi/vim來修改配置文件。

[root@centos7-01 ~]# crontab -e


演示示例,淩晨3點去執行任務(執行命令,在日期後面放上即可)。

任務內容為,執行腳本/bin/bash 腳本名字/usr/local/sbin123.sh

指定輸出日誌,正確輸出到從定向文件/tmp/123.log,錯誤的輸出定義到/tmp/123.log 如果每天都需要操作記錄,利用來追加>>

以下參數:

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


範圍用法,

分範圍0-59,時範圍0-23,日範圍1-31,月範圍1-12,周1-7

雙月(能整除2的月份)的1-10號每周2周5的3點執行

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


啟動crontab服務

[root@centos7-01 ~]# systemctl start crond

[root@centos7-01 ~]# ps ux |grep cron

root 567 0.0 0.1 126236 1616 ? Ss 16:29 0:00 /usr/sbin/crond -n

root 2211 0.0 0.0 123208 780 ? Ss 17:01 0:00 /usr/sbin/anacron -s

root 2354 0.0 0.0 112676 980 pts/0 S+ 17:19 0:00 grep --color=auto cron


此命令也可以查看crond服務狀態

[root@centos7-01 ~]# systemctl status crond

技術分享圖片

要保證服務是啟動狀態,

否則會無效。


註意

如果發現計劃不生效

有可能是沒有用絕對路徑,

鍵入絕對路徑(最穩),也可以在腳本裏添加變量,然後後面參數敲入相應參數即可。

每寫一個任務計劃,都要追加日誌(正確錯誤輸出,)。


#crontab -l 列出計劃任務

例如,

#crontab -e 添加以下計劃任務

1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f

[root@centos7-01 ~]# crontab -l

1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f

crontab -l可以查看計劃任務。


crontab的文件路徑/cron/下的文件,就是對應的用戶,例如root就以root命名文件。

#cat /var/spool/cron/root


crontab

-l 列出任務計劃

-e 編輯任務計劃

-r 刪除任務計劃

-u 指定用戶



10.24 chkconfig工具


Linux系統服務管理-chkconfig


技術分享圖片

chkconfig是一種服務。

chkconfig是centos7以前使用的版本。

sysv centos7以前的服務管理基制。


systmd centos7的服務管理基制。ps進程排第一,init在centos6進程排第一。


chkconfig命令


[root@centos701 ~]# 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:關

chkconfig在centos7下面只剩下這2個服務。

chkconfig服務腳本的目錄,MySQL以後也會用到此腳本

[root@centos701 ~]# ls /etc/init.d/

functions netconsole network README


更改network服務狀態,例如off它們

[root@centos701 ~]# chkconfig network off

[root@centos701 ~]# 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:關


再次開啟利用on

[root@centos701 ~]# chkconfig network on

[root@centos701 ~]# 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:關


此處數字0-6 是一個運行級別 一共7個級別

0表示關機

1單用戶 centos6 及之前的系統版本實用,如果定位至1級別,開機就會自動進入單用戶模式。

2多用戶模式,不帶圖形 2比3少了一個nfs服務

3多用戶模式,不帶圖形

4保留級別

5多用戶,帶圖形

6重啟 如果定位6級別,啟動就會重啟。


更改inittab文件,可以更改運行級別。

#vi /etc/inittab

技術分享圖片

指定某個級別關閉或開啟,

[root@centos701 ~]# chkconfig --level 3 network off

[root@centos701 ~]# 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:關


如果需要幾個級別一起開啟或者關閉,可以同一條命令連著級別來寫,例如格式:

# chkconfig --level 345 network on 此處345 on 表示運行級別的345都開啟

[root@centos701 ~]# chkconfig --level 345 network on

[root@centos701 ~]# 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:關


把腳本加入到服務列表

chkconfig --add 服務

示例:

[root@centos701 ~]# cd /etc/init.d/

[root@centos701 init.d]# ls

functions netconsole network README

[root@centos701 init.d]# cp network 123

[root@centos701 init.d]# ls

123 functions netconsole network README

[root@centos701 init.d]# chkconfig --add 123

[root@centos701 init.d]# chkconfig --list


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

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

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



文件名無條件要求,但是文件內容有要求,具體參考# vi network

技術分享圖片

shell腳本,

2345啟動順序,

運行級別啟動順序第十位10位啟動

第90位關閉

描述


刪除123服務

chkconfig --del 123



10.25 systemd管理服務


技術分享圖片

systemd是centos7服務管理機制


列出所有service,以及描述

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

技術分享圖片


幾個常用的服務相關的命令


讓服務開機啟動 (.service可以不用加)

#systemctl enable crond.service

生成軟鏈接文件


不讓開機啟動

#systemctl disable crond



查看狀態

#systemctl status crond



停止服務

#systemctl stop crond



啟動服務

#systemctl start crond



重啟服務

#systemctl restart crond



檢查服務是否開機啟動

#systemctl is-enabled crond





10.26 unit介紹


Linux系統服務管理-systemd


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

技術分享圖片

service 系統服務



target 多個unit組成的組

單用戶,emygrcym model,centos6 7個運行級別。centos7有類似的級別

[root@centos701 system]# cd /usr/lib/systemd/system

[root@centos701 system]# ls -l runlevel*

lrwxrwxrwx. 1 root root 15 3月 8 05:05 runlevel0.target -> poweroff.target

lrwxrwxrwx. 1 root root 13 3月 8 05:05 runlevel1.target -> rescue.target

lrwxrwxrwx. 1 root root 17 3月 8 05:05 runlevel2.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 3月 8 05:05 runlevel3.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 3月 8 05:05 runlevel4.target -> multi-user.target

lrwxrwxrwx. 1 root root 16 3月 8 05:05 runlevel5.target -> graphical.target

lrwxrwxrwx. 1 root root 13 3月 8 05:05 runlevel6.target -> reboot.target

上面 runlevel0.target -> poweroff.target中的runlevel10.target是軟鏈接,真正的文件是poweroff.targt

target是由多個unit組成的一個組,而unit又由多個services組成的組。


不用深究以下幾點

device 硬件設備

mount 文件系統掛載點

automount 自動掛載點

path 文件或路徑

scope 不是由systemd啟動的外部進程


slice 進程組

snapshot systemd快照

socket 進程間通信套接字

swap swap文件

timer 定時器


system的服務

#ls /usr/lib/systemd/system

技術分享圖片


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

不加--all的話active列會顯示全部active和failed(個別)

技術分享圖片


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

[root@centos701 system]# systemctl is-active crond.service

active



10.27 target介紹

介紹

技術分享圖片

系統為了方便管理用target來管理unit


顯示系統所有的target

#systemctl list-unit-files --type=target

技術分享圖片

查看指定target下面有哪些unit

#systemctl list-dependencies multi-user.target

技術分享圖片

大部分都是.service


查看系統默認的target

#systemctl get-default

[root@centos701 system]# systemctl get-default

multi-user.target


設定默認運行級別。

設定開機運行級別,相當於修改chkconfing的默認開機運行級別。

#systemctl set-default multi-user.target

[root@centos701 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



cat /usr/lib/systemd/system/sshd.service //看[install]部分


總結:

系統由多種units組成的,unit是由target去管理,也就是說target由很多unit組成的。


10.23-10.27 corn, chkconfig, systemd, unit, target