1. 程式人生 > >八周1課 任務計劃cron,chkconfig工具,systemd管理服務,unit,target

八周1課 任務計劃cron,chkconfig工具,systemd管理服務,unit,target

cron chkconfig ystemd unit target

linux任務計劃
在linux中,任務計劃是必不可少的,在linux中怎樣設置任務計劃呢?
首先看一個文件
[root@localhost ~]# 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 (星期,0代表周日,也可以寫成7,也可以寫英文簡寫)

| | | | |

* user-name(用戶) command to be executed (要執行的命令)

這就是任務計劃的配置文件,在這個配置文件中他會定義幾個變量SHELL=/bin/bash、PATH=/sbin:/bin:/usr/sbin:/usr/bin、MAILTO=root(發送郵件誰)

在他下面的是格式。我們可以看到他下面有五個星號,所以分為五個位。

用命令crontab -e,這樣就進入了配置文件當中。它的用法和vim相同。

比如現在有一項任務,需要在淩晨3點去執行
0 3 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
這裏面的“
”代表所有的意思,第一個代表“分”,第二個代表“時”,第三個代表“日”。所以這是每天都執行。
然後後面接一個shell腳本,這個腳本的意思是每天把正確內容追加到一個指定的文件,錯誤的也追加到指定的文件。
任務計劃的格式很簡單,分時日月周後接具體的命令。

同時我們也可以用一個範圍去執行。
還是上面的那個腳本。
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
它的意思是每雙月的1-10日淩晨三點執行這個腳本。
但是我們發現這裏面沒有年份,如果我們想那一年執行一個腳本,這裏我們就用星期去確定它的唯一性。比如今年的6月18日和明年的6月18日所在的周肯定也不一樣。

想讓任務計劃crontab正常使用,我們還要去啟動服務,那麽怎樣去啟動服務呢?
systemctl start crond輸入這條命令,他就啟動了。
然後我們檢查一下他有沒有啟動
[root@localhost ~]# ps -aux |grep cron
root 560 0.0 0.1 126236 1656 ? Ss 22:37 0:00 /usr/sbin/crond -n
root 1253 0.0 0.0 112676 984 pts/0 S+ 23:37 0:00 grep --color=auto cron
如果出現了含有cron的字符串,就證明他已經啟動了。

我們也可以用systenctl status crond來查看
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running)(如果這裏是綠色的,就證明他已經啟動了,如果沒有顏色,就證明它沒有啟動) since 日 2018-03-25 22:37:48 CST; 1h 2min ago
Main PID: 560 (crond)
CGroup: /system.slice/crond.service
└─560 /usr/sbin/crond -n

3月 25 22:37:48 localhost.localdomain systemd[1]: Started Command Scheduler.
3月 25 22:37:48 localhost.localdomain systemd[1]: Starting Command Scheduler...
3月 25 22:37:48 localhost.localdomain crond[560]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.)
3月 25 22:37:49 localhost.localdomain crond[560]: (CRON) INFO (running with inotify support)

還有一種情況,寫了一個計劃,放到了配置文件中,但是沒有執行,這可能是寫的腳本中,用的是一個命令,而沒有用絕對路徑,這就容易造成命令不執行的現象。因為你用的這個命令並沒有在他的PATHZ中,所以解決的方法是要麽寫絕對路徑,要麽就將命令的路徑加入到PATH。

還有一個建議就是,我們每寫一個任務計劃,後面就追加一個任務日誌,正確輸出和錯誤輸入都要寫上,這樣才能保證我們的任務有據可查,如果不執行,是不是有錯誤,我麽只要查看任務日誌就能知曉。

任務日誌的備份
[root@localhost ~]# crontab -l
1 10 2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
這樣在我們寫完任務計劃後就能查看了。
它的問價在/var/spool/cron/這裏面會有對應用戶的crond,他會以用戶名字作為結尾,形成一個文件。所以我們如果要備份,直接備份這個文件就可以,或者將這個目錄整個拷貝一下就行。

如果我們想刪除一個任務計劃,就輸入crontab -r
我們也可以指定一個用戶,比如我們指定root用戶。crontab -u root -l

linux系統服務管理-chkconfig
linux中有很多服務,比如iptables、crontab、firewalld等等都是服務,這麽多的服務就需要有一個工具去管理。管理他怎樣啟動,怎樣開機啟動,怎麽樣讓他在指定的級別啟動。

列出chkconfig所有服務
chkconfig --list
[root@linletao-001 ~]# chkconfig --list

註:該輸出結果只顯示 SysV 服務,並不包含
原生 systemd 服務。SysV 配置數據
可能被原生 systemd 配置覆蓋。

  要列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。
  查看在具體 target 啟用的服務請執行
  ‘systemctl list-dependencies [target]‘。

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

chkconfig服務變更
chkconfig 服務名 off/on(關閉或者打開)
比如我們現在要關閉chkconfig中的network
chkconfig --list
[root@linletao-001 ~]# chkconfig --list
然後我們查詢一下
chkconfig --list
network 0:關 1:關 2:關 3:關 4:關 5:關 6:關
這樣在6個級別就全部關閉network了。
其中這6個數字的的含義是:0是關機狀態,1是單用戶狀態,2是多用戶,但是沒有NFS、3是多用戶模式,4是暫時沒用,5是多用戶的圖形模式,6是重啟。

指定某一個級別的關閉或者開啟。
例如我們把3級別關閉
[root@linletao-001 ~]# chkconfig --level network 3 off
然後我們查詢一下
[root@linletao-001 ~]# chkconfig --list
network 0:關 1:關 2:開 3:關 4:開 5:開 6:關
這樣network在3級別就關閉了。

如果我想在多個級別上關閉服務,就直接將級別的序號寫上,必要用“,”隔開
[root@linletao-001 ~]# chkconfig --level 35 network off
[root@linletao-001 ~]# chkconfig --list

network 0:關 1:關 2:開 3:關 4:開 5:關 6:關
這樣就在3,5級別將network關閉了。

將腳本加入到服務列表中
chkconfig --add 腳本名
比如我們要將123加入到chkconfig的服務列表中
[root@linletao-001 init.d]# chkconfig --add 123
然後我們查看一下
123 0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:關 4:開 5:關 6:關
這樣就添加了一項服務。

這時怎樣做到的呢?首先我們要將服務腳本放到init.d這個目錄下,只有放在這個目錄中,我們才能添加到服務中去。名字無所謂,但是文件內容有格式。

#! /bin/bash(必須是shell腳本)
#

network Bring up/down networking

#

chkconfig: 2345 10 90(第一組數字是運行級別,第二組是運行級別啟動順序,第三組是關閉順序)

description: Activates/Deactivates all network interfaces configured to \

start at boot time.(描述)

#
以上的內容是腳本必須要有的

將腳本從chkconfig列表中刪除。
chkconfig --del
我們還用123做例子
[root@linletao-001 init.d]# chkconfig --del 123
然後查詢
netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關
network 0:關 1:關 2:開 3:關 4:開 5:關 6:關
這樣名為123的服務就沒有了。

linux系統管理服務-systemd
它是centos7的管理機制。centos6主要使用chkconfig。

查看所有服務
systemctl list-unit-files
[root@linletao-001 init.d]# systemctl list-unit-files|head -10
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
這樣就查看了所有服務

還有回個命令,也可以查看,而且更加的清晰。
[root@linletao-001 init.d]# systemctl list-units --all --type=service|head -5
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
chronyd.service loaded active running NTP client/server
cpupower.service loaded inactive dead Configure CPU power related settings

開機啟動
systemctl enable 服務名

開機不啟動
systemctl disable 服務名

查看狀態
systemctl status crond

啟動服務
systemctl status 文件名

停止服務
systemctl stop 文件名

檢查服務是否開機啟動
systemctl is-enabled 文件名

unit的概念
systemd開啟和監督整個系統是基於unit的概念。unit是由一個與配置文件名同名的名字和類型組成的(例如:avahi.service unit有一個具有相同名字的配置文件,它是守護進程avahi的一個封裝單元)。

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

unit相關的命令

列出正在運行的unit
systemctl list-units

列出所有的unit,包括失敗的或者inactive的
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來管理unit。

列出系統中所有的target
systemctl list-unit-files --type=target

UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static

查看指定target下面有哪些unit
systemctl list-dependencies multi-user.target

查看系統默認的target
systemctl get-default
[root@linletao-001 init.d]# systemctl get-default
multi-user.target
這既是系統默認的target

設置默認target
systemctl set-default multi-user.target

八周1課 任務計劃cron,chkconfig工具,systemd管理服務,unit,target