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/478722
2. xinetd服(默認機器沒有安裝這個服務,需要yum install xinetd安裝) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定義啓動腳本 http://www.jb51.net/article/100457.htm
一、linux任務計劃cron
linux中任務計劃必不可以少,它可以定時執行一些備份數據、重啓服務器等操作,操作可能是一個腳本或者命令。
crontab命令:
- 格式:分 時 日 月 周 user command
分範圍:0-59,時範圍:0-60,日範圍:0-31,月範圍1-12,周範圍:1-7
指定範圍:1-5 ,指定1到5範圍
可以用1,2,3表示1或者2或者3
*/2表示被2整除的數字,比如小時,那就是每隔2小時
- 文件/var/spool/cron/username
- 可用格式1-5表示一個範圍1到5
- 可用格式1,2,3表示1或者2或者3
- 可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時
- 要保證服務是啓動狀態
- systemctl start crond.service
- 任務計劃的配置文件:
cat /etc/crontab
文件中有幾個環境變量:
- SHELL=/bin/bash
- PATH:環境變量,命令的路徑
- MAILTO:發郵件給誰
下面的是crontab格式說明:
# 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 //需要執行的命令
- 新增定時任務:
crontab -e //進入編輯模式,和vim一樣,按i進行編輯
新增一個凌晨3點執行的腳本,*代表所有的意思。並且把日誌和錯誤日誌輸出
新增一個1-10號,雙月執行的腳本,也就是每兩個月纔會執行的腳本,*/2這樣就可以符合條件了
0 3 1-10 */2 * /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log
新增一個週二、週五才執行的腳本:
0 3 * * 2,5 /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log
如果想用年份可以用星期確定唯一性,比如說今年的6月18號和明年的6月18號的星期肯定是不同的
- 需要啓動crond服務,定時任務纔會生效
systemctl start crond.service //啓動crond服務
- 檢查是否啓動成功
ps aux |grep cron
或者:systemctl start crond 查看,如果狀態是綠色就說明成功了
- 任務計劃不執行的原因分析
寫了定時任務,crond服務也是正常,但是就是不執行。
原因可能爲:腳本中命令未使用絕對路徑,解決:要麼將命令寫一個絕對路徑,要麼將命令的路徑加入到PATH變量裏面去
建議追加一個日誌,這樣就可以根據日誌來判斷。
- 任務計劃備份
crontab文件存在位置/var/spool/cron/username,需要備份的時候直接複製即可。
二、chkconfig工具
chkconfig工具是linux服務的管理工具,cron、iptables、firewalld、mysql等都是服務。chkconfig可以管理這些服務開機是否啓動等。
chkconfig工具在centos6和之前的版本中使用,centos7中已經逐漸的不在使用了,爲了過度centos7還可以使用,但是後面趨勢是會被遺棄。
- 列出系統所有服務
chkconfig --list
0-6 表示7個啓動級別(centos6及以前):0:關機 1: 單用戶 2:多用戶模式(不帶nfs服務) 3: 多用戶模式(不帶圖形)
4: 保留級別 5: 多用戶(帶有圖形) 6: 重啓
- 需要注意的是:啓動腳本需要放到/etc/init.d/目錄下
- 關閉netword服務
chkconfig network off
- 指定某一服務的某個級別開啓或關閉
chkconfig --level 3 network off //指定第三級別network服務關閉
chkconfig --level 345 network on //指定network中的3,4,5級別開啓
- 將一個腳本加入到服務列表列表中
首先把腳本放到/etc/init.d/目錄下
腳本名稱沒有要求,但是格式有要求:
1、必須是一個shell腳本
2、需指定運行級別,第10位開啓,第90關閉
示例:init.d目錄下 cp network服務 隨便起一個名字,進行添加
[root@wxy01 init.d]# cp network 123
[root@wxy01 init.d]# chkconfig --add 123
結果:
刪除:chkconfig --del 123
三、systemd管理服務
- systemctl list-units --all --type=service
- 幾個常用的服務相關的命令
- systemctl enable crond.service //讓服務開機啓動
- systemctl disable crond //不讓開機啓動
- systemctl status crond //查看狀態
- systemctl stop crond //停止服務
- systemctl start crond //啓動服務
- systemctl restart crond //重啓服務
- systemctl is-enabled crond //檢查服務是否開機啓動
在centos6或之前的版本中用chkconfig工具去管理系統的服務,在centos7中使用systemd。
- 列出所有的service
- 設置服務開機啓動
systemctl enable crond.service //.service 可以不加
- 關閉開機啓動
systemctl disable crond
- 查看狀態
systemctl status crond
- 啓動服務
systemctl start crond
- 停止服務
systemctl stop crond
- 檢查服務是否開機啓動
systemctl is-enabled crond
四、unit介紹
lssystemctl 狀態如果是enabled 會在 /usr/lib/systemd/system 目錄下生產軟連接,這些文件都叫做unit。
unit分爲以下類型:
- service 系統服務
- target 多個unit組成的組
- device 硬件設備
- mount 文件系統掛載點
- automount 自動掛載點
- path 文件或路徑
- scope 不是由systemd啓動的外部進程
- slice 進程組
- snapshot systemd快照
- socket 進程間通信套接字
- swap swap文件
- timer 定時器
unit相關的命令
列出正在運行的unit:
systemctl list-units
列出所有的unit:
systemctl list-units --all
列出inactive狀態的unit
systemctl list-units --all --state=inactive
列出狀態爲active的service
systemctl list-units --type=service
查看某個服務是否爲active
systemctl is-active crond.service
五、target介紹
- 系統爲了方便管理target來管理unit
target相關的命令
列出系統中所有的target:
systemctl list-unit-files --type=target
查看指定target下面有哪些unit
systemctl list-dependencies multi-user.target
查看系統默認的target
systemctl get-default
設置默認的target
systemctl set-default multi-user.target
- 一個service屬於一種類型的unit
- 多個unit組成了一個target
- 一個target裏面包含了多個service
- cat /usr/lib/systemd/system/sshd.service 看[install]部分
/