linux系統服務管理
一、linux的任務計劃
1.crontab -u、-e、-l、-r
2.格式: 分時日月周user command
3.文件/ar/spool/cron/usemname
4.分範圍0-59,時範圍0-23,日範圍0-31,月範圍0-12,周1-6
5.可用格式1-5表示一個範圍1到5
6.可用格式1,2,3表示1或者2或者3
7.可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時
8要保證服務是啟動狀態
systemctlstartcrond.service
二、Linux系統服務管理-chkconfig
chkconfig—list
chkconfig--level 3 network off
chkconfig--level 345 network off
chkconfig--del network
chkconfig--add network
silence
1.chkconfig命令主要用來更新(啟動或停止)和查詢系統服務的運行級信息。謹記chkconfig不是立即自動禁止或激活一個服務,它只是簡單的改變了符號連接。
2.使用語法:
chkconfig [--add][--del][--list][系統服務] 或 chkconfig [--level <等級代號>][系統服務][on/off/reset]
chkconfig在沒有參數運行時,顯示用法。如果加上服務名,那麽就檢查這個服務是否在當前運行級啟動。如果是,返回true,否則返回false。如果在服務名後面指定了
參數用法:
--add 增加所指定的系統服務,讓chkconfig指令得以管理它,並同時在系統啟動的敘述文件內增加相關數據。
--del 刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述文件內刪除相關數據。
--level<等級代號> 指定讀系統服務要在哪一個執行等級中開啟或關畢。
等級0表示:表示關機
等級1表示:單用戶模式
等級2表示:無網絡連接的多用戶命令行模式
等級3表示:有網絡連接的多用戶命令行模式
等級4表示:不可用
等級5表示:帶圖形界面的多用戶模式
等級6表示:重新啟動
需要說明的是,level選項可以指定要查看的運行級而不一定是當前運行級。對於每個運行級,只能有一個啟動腳本或者停止腳本。當切換運行級時,init不會重新啟動已經啟動的服務,也不會再次去停止已經停止的服務。
chkconfig --list [name]:顯示所有運行級系統服務的運行狀態信息(on或off)。如果指定了name,那麽只顯示指定的服務在不同運行級的狀態。
chkconfig --add name:增加一項新的服務。chkconfig確保每個運行級有一項啟動(S)或者殺死(K)入口。如有缺少,則會從缺省的init腳本自動建立。
chkconfig --del name:刪除服務,並把相關符號連接從/etc/rc[0-6].d刪除。
chkconfig [--level levels] name:設置某一服務在指定的運行級是被啟動,停止還是重置。
運行級文件:
每個被chkconfig管理的服務需要在對應的init.d下的腳本加上兩行或者更多行的註釋。第一行告訴chkconfig缺省啟動的運行級以及啟動和停止的優先級。如果某服務缺省不在任何運行級啟動,那麽使用 - 代替運行級。第二行對服務進行描述,可以用\ 跨行註釋。
例如,random.init包含三行:
# chkconfig: 2345 20 80
# description: Saves and restores system entropy pool for \
# higher quality random number generation.
使用範例:
chkconfig --list #列出所有的系統服務
chkconfig --add httpd #增加httpd服務
chkconfig --del httpd #刪除httpd服務
chkconfig --level httpd 2345 on #設置httpd在運行級別為2、3、4、5的情況下都是on(開啟)的狀態
chkconfig --list #列出系統所有的服務啟動情況
chkconfig --list mysqld #列出mysqld服務設置情況
chkconfig --level 35 mysqld on #設定mysqld在等級3和5為開機運行服務,--level 35表示操作只在等級3和5執行,on表示啟動,off表示關閉
chkconfig mysqld on #設定mysqld在各等級為on,“各等級”包括2、3、4、5等級
如何增加一個服務:
1.服務腳本必須存放在/etc/ini.d/目錄下;
2.chkconfig --add servicename
在chkconfig工具服務列表中增加此服務,此時服務會被在/etc/rc.d/rcN.d中賦予K/S入口了;
3.chkconfig --level 35 mysqld on
修改服務的默認啟動等級。
三、Linux系統服務管理-systemd
systemctl list-units--all--type=servi ce
幾個常用的服務相關的命令
systemctl enable crond.service /讓服務開機啟動
systemctl disa ble crond //不讓開機啟動
systemctl status cro nd //查看狀態
systemctl stop crond //停止服務
systemctl start crond //啟動服務
systemctl restart crond //重啟服務
Systemctl is-enabled crond//檢查服務是否開機啟動
四、linux系統服務管理systemd
1.ls /usr/lib/systemd/system //系統所有unit,分為以下類型
service 系統服務
target 多個unit組成的組
device 硬件設備
文件系統掛載點
mount
automount 自動掛載點
path 文件或路徑
scope 不是由systemd啟動的外部進程
slice 進程組
snapshot systemd快照
socket 進程間通信套接字
swap swap文件
timer 定時器
2. unit相關的命令
systemctl list- units //列出正在運行的u nit
systemctl list-units--all //列出所有,包括失敗的或者inactive的
systemctl list-units--all--state=inactive //列出inactive的unit
systemctl list-units--type=service//列出狀態為active的service
systemct is-active crond.servi ce /查看某個服務是否為active
五、target
系統為了方便管理用target來管理u nit
systemctl list-unit-files--type=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.servic //看[instal]部分
linux系統服務管理