Linux學習-1030(定時任務、任務管理、)
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服務 隨便起一個名字,進行新增
[[email protected] init.d]# cp network 123
[[email protected] 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]部分
/