linux管理日常(六) 服務管理

2021-08-29 12:08:19 字數 3694 閱讀 4088

10.23 linux任務計畫cron

10.24 chkconfig工具

10.25 systemd管理服務

10.26 unit介紹

10.27 target介紹

擴充套件anacron

xinetd服(預設機器沒有安裝這個服務,需要yum install xinetd安裝)

systemd自定義啟動指令碼

crontab是乙個服務,配置後可以定期執行命令或指令碼。使用crontab -e進入配置檔案,使用vi模式輸入計畫任務。一行為一條計畫,格式為:分 時 日 月 周 user command。配置好後儲存退出,剛剛編輯的文字儲存在了/var/spool/cron/user中。其中user為root或其他使用者。具體配置方法可以看crontab配置檔案:

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

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

時 可用格式1-5表示乙個範圍1到5

日 可用格式1,2,3表示1或者2或者3

月 可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時

命令最好使用絕對路徑,或者把命令的path寫入/etc/crontab中path

常用選項還有有: -u指定任務執行使用者、-l列出當前所有計畫任務、-r清除計畫任務。要保證crontab服務是啟動狀態,使用systemctl status crond檢視,未開啟的話:systemctl start crond.service

chkconfig是centos6中sys v使用的服務管理工具。centos7用systemd取代sys v,不過相容chkconfig。chkconfig配置開機啟動服務主要是在/etc/init.d目錄中完成。

chkconfig --list列出當前chkconfig管理的啟動任務:

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

注:該輸出結果只顯示 sysv 服務,並不包含

原生 systemd 服務。sysv 配置資料

可能被原生 systemd 配置覆蓋。

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

檢視在具體 target 啟用的服務請執行

'systemctl list-dependencies [target]'。

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

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

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

其中可以看到0-6七個級別:這時init劃分的級別,0代表關機,1代表單使用者模式,2代表受限多使用者模式,3代表多使用者模式,4是使用者自定義,5代表圖形模式,6代表重啟。我們可以在/etc/inittab更改啟動時選擇的服務級別。至於其他服務使用以下命令修改:

chkconfig --level 3 network off #將network服務3級別關掉

chkconfig --level 345 network off

chkconfig --del network

chkconfig --add network #自己編寫的指令碼需要先新增到/etc/init.d

現在systemd越來越流行,許多發行版都替換了sysv。systemd最基礎的是unit,通過控制unit實現服務管理。systemctl list-units --all可以檢視所有unit,選項 --type=service指定只顯示類別為service的unit。但是unit過於眾多不好管理,於是使用target管理unit。garget相當於unit的集合。

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

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

systemctl enable crond.service #讓服務開機啟動,以crond服務為例,service可以省略,下同

systemctl disable crond #不讓開機啟動

systemctl status crond #檢視狀態

systemctl stop crond #停止服務

systemctl start crond #啟動服務

systemctl restart crond #重啟服務

systemctl is-enabled crond #檢查服務是否開機啟動

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

上面說為了方便使用,用target來管理unit。systemd使用target參照sysv的七個級別組織了系統target:

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 #設定預設target

看完以上是不是感到迷糊?其實就是多個unit組成了乙個target,乙個target裡面包含了多個unit,service屬於一類unit。cat /usr/lib/systemd/system/sshd.service[install]部分指出該service所屬garget。

linux日常管理 linux日誌

通過系統日誌獲得相關資訊,出現錯誤,突發情況可以通過檢視日誌獲得有用的資訊。遇到故障,看日誌是最常用的方法,在日常工作中一定要養成看日誌的習慣。最核心的日誌在這個目錄下 日誌會無限生成,占用的記憶體會越來越大,有個配置檔案可以限制日誌的個數,時間,等資訊。配置檔案 weekly 日誌每週切割一次 r...

linux日常管理 rsync後台服務方式 1

rsync的另外一種方式,寫乙個配置檔案,放在etc下,通過乙個命令啟動他,它會監聽乙個埠,在客戶端和服務端進行通訊。遠端機器的配置檔案 ip是192.168.1.117 配置檔案的名字,寫成這個,會自動載入,不用指向。root wangshaojun vim etc rsyncd.conf por...

linux軟體管理(六)

教程主題 linux軟體管理 1 軟體包管理 在系統管理中,軟體包的管理是最重要的,是系統管理的基礎的基礎,只有我們學會軟體包的管理才能談得上其它的應用。rpmrpm是軟體管理程式,提供軟體的安裝 公升級 查詢 反安裝的功能。優點 a 安裝方便,軟體中所有資料都經過編譯和打包 b 查詢 公升級 反安...