1. 程式人生 > >日常運維(六)

日常運維(六)

crontab chkconfig systemd unit target

crontab

crontab 任務計劃 crond服務
crontab -u -e -l -r
5個*格式:分,時,日,月,周 user command
/etc/crontab 配置文件

crontab -e 編輯配置文件
crontab -l 查看任務計劃
crontab -r 刪除
crontab -u 指定用戶

文件 /var/spool/cron/usermane 每個用戶的定時任務
格式範圍是

分,0-59
時,0-23
日,1-31
月,1-12
周,1-6 (0、7表示周日)

可用格式1-5 表示一個範圍
可用格式*/2表示被2整除的數字,比如小時,那就是每隔2個小時
要保證服務是啟動狀態 systemctl start crond.service

`0 3 * * * /bin/bash /root/sh/123.sh  >>/tmp/123.log  2>>/tmp/321.log`
每天的3點00分 執行一個123.sh腳本,把正確結果放入123.log,錯誤結果放入321.log裏

chkconfig

chkconfig 系統服務管理 Centos 7 還能兼容使用

chkconfig - -list       # 查看所有服務
chkconfig network off        #開機關閉服務  on開機自啟動
chkconfig - -level 2 off         #2級別開機關閉

首先先放到/etc/init.d 目錄下

chkconfig - -add       #123 手動加入開機自啟動
chkconfig - -del        #123 手動刪除

6以及之前的版本
chkconfig 使用的服務管理的機制是 SysV
7個運行級別 runlevel

0 關機
1 單用戶
2 和3級別只少了NFS服務
3 多用戶級別
4 保留
5 圖形界面
6 重啟 reboot 

systemd

centos 7 使用的服務管理機制是systemd 、target

systemd 系統管理服務

systemctl list-unit-files    #查看所有的包括service、target、socket服務
systemctl list-unlis - -all - -type=service    #只查看所有service服務
systemctl list-units - -type=service    #只查看所有service服務但不包括未激活的任務
systemctl enable crond.service    #開機自啟動
systemctl disable crond.service    #關閉自啟動
systemctl status crond.service    #查看指定服務狀態
systemctl start crond.service    #啟動服務
systemctl stop crond,service     #關閉服務
systemctl restart crond.service    #重啟服務
systemctl is-enable crond     #檢查服務是否開機自啟動

unit (systemd)

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

service     #系統服務
target     #多個unit(service)組成的組
device     #硬件設備
mount     #文件系統掛載點
automount     #自動掛載點
path     #文件或路徑
scope     #不是由systemd啟動的外部進程      
slice     #進程組
snapshot systemd     #快照
socket     #進程間通信套接字
swapswap     #文件
timer     #定時器

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
systemctl is-active crond.service     #查看摸個服務是否為active
systemctl is-enable crond.service     #查看摸個服務是否為enable

target (systemd)

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

systemctl list-unit-files - -type=target
systemctl list-dependencies multi-user.target     #查看指定target下面有哪些unit 
systemclt get-default      #查看系統默認的target
systemctl set-default multi-user.target

一個service屬於一種類型的unit
多個unit組成了一個target
一個target裏面包含了多個service
cat /usr/lib/systemd/system/sshd.service 看[install] 部分

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

擴展

  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

日常運維(六)