了解systemctl和chkconfig的服務管理工具
crontab計劃任務的計時方式:對大部分時間單位都清楚的劃分,具體可以劃分到分鐘,時間單位有分鐘,小時,日(一個月內的第幾天),月,周(一周的第幾天,計劃任務中可以是指定為每隔幾周來執行某些任務)
[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) 分鐘時間單位,從0-59分鐘 # | .------------- hour (0 - 23) 小時時間單位,從0-23點 # | | .---------- day of month (1 - 31) 日期時間單位,從1-31日 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 月的時間單位,從1-12月 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 周幾的時間單位,從0-6(0和7可以指定一周的第七天) # | | | | | # * * * * * user-name command to be executed
crontab計劃任務分為分\時\日\月\周為時間計算單位,*代表每隔多長時間就執行,全部為*時表示每分鐘執行一次
分:以分鐘為單位,可以寫0到59的分鐘數,也可以寫成每隔多少分鐘( 如每隔20分鐘執行操作:*/20 * * * * )
時:以小時為單位,可以取值0到59的數值,可以寫成每隔多少小時,也可以寫成如9-13點的時間段,配合分鐘設置使用,表示為9點到13點這段時間內,在每小時的第幾分鐘執行一次任務。
日:指定一個月內的第多少天,可以指定一段日期,表示為在這段日期內,每天都會執行一次計劃任務,可以使用格式1,2,3表示1和2和3的時間點上都執行計劃任務
月:指定一年內的第幾個月,可以指定N個月的時間段,表示為在這段日期內,每天都會執行一次計劃任務,也可以指定每隔幾個月執行一次計劃任務(如 : * * */2 * * 表示為每隔兩個月執行一次計劃任務),可以使用格式1,2,3表示1和2和3的時間點上都執行計劃任務
周: 一周的第幾天,從0-6的數值,0和7則表示為一周的第七天,同樣可以指定一段時間或每隔幾周執行一次計劃任務,這裏需要註意的是寫成*/2的意思則是每隔2周去執行,而不是這周會每隔兩天的意思。
crontab 選項 執行操作的命令
crontab -e 編輯計劃任務,向crontab配置文件中新添加一個計劃任務
crontab -l 列出系統中當前所有的計劃任務
crontab -r 刪除所有配置的計劃任務,清空所有計劃任務
crontab -u 指定某個用戶下的所有計劃任務配合-l 列出當前用戶的計劃任務
crontab 執行腳本當中的命令時,可能會執行不成功,原因是因為腳本中是直接使用命令來執行的,而不是使用命令的絕對路徑,這樣就會導致命令執行失敗,計劃任務同樣也會無法正確執行
centos7啟動crontab,必須啟動計劃任務進程才能夠正常運行計劃任務:
[root@localhost ~]# systemctl start crond.service
[root@localhost ~]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-07-17 01:13:53 CST; 12h ago
Main PID: 1015 (crond)
CGroup: /system.slice/crond.service
└─1015 /usr/sbin/crond -n
Jul 17 01:13:53 localhost.localdomain systemd[1]: Started Command Scheduler.
Jul 17 01:13:53 localhost.localdomain systemd[1]: Starting Command Scheduler...
Jul 17 01:13:53 localhost.localdomain crond[1015]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.)
Jul 17 01:13:53 localhost.localdomain crond[1015]: (CRON) INFO (running with inotify support)
[root@localhost ~]# ps -aux |grep crond
root 1015 0.0 0.1 126228 1592 ? Ss 09:07 0:00 /usr/sbin/crond -n
root 3752 0.0 0.0 112652 960 pts/0 R+ 13:54 0:00 grep --color=auto crond
備份計劃任務,計劃任務的配置文件存儲在/var/spool/cron/下,計劃任務會以用戶名的命名方式來存儲,cat該用戶名的配置文件可以查看計劃任務內容,備份可以直接拷貝該文件即可
[root@localhost ~]# crontab -l
1 5 */7 * * cp /etc/passwd /usr/local/src/
[root@localhost ~]# cat /var/spool/cron/root
1 5 */7 * * cp /etc/passwd /usr/local/src/
45 4 1,10,22 * * /etc/init.d/httpd restart
上面的例子表示每月1、10、22日的4:45重啟apache。
10 1 * * 6,0 /etc/init.d/httpd restart
上面的例子表示每周六、周日的1:10重啟apache。
0,30 18-23 * * * /etc/init.d/httpd restart
上面的例子表示在每天18:00至23:00之間每隔30分鐘重啟apache。
0 23 * * 6 /etc/init.d/httpd restart
上面的例子表示每星期六的23:00 重啟apache
```。
0 /1 /etc/init.d/httpd restart
每一小時重啟apache
0 23-7/1 * /etc/init.d/httpd restart
晚上11點到早上7點之間,每隔一小時重啟apache
0 11 4 * mon-wed /etc/init.d/httpd restart
每月的4號與每周一到周三的11點重啟apache
0 4 1 jan * /etc/init.d/httpd restart
一月一號的4點重啟apache
-----
# chkconfig服務管理工具
在Centos7中保留chkconfig服務管理的方式,我們可以用chkconfig新增自定義服務腳本文件來管理編譯安裝的服務
centos7系統中關於chkconfig的描述:
-----
註意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。
如果您想列出 systemd 服務,請執行 ‘systemctl list-unit-files‘。
欲查看對特定 target 啟用的服務請執行
‘systemctl list-dependencies [target]‘。
-----
-----
chkconfig的選項用法:
--add 增加所指定的系統服務,讓chkconfig管理它,並同時在系統啟動的敘述文件
--del 刪除所指定的系統服務,不再由chkconfig指令管理,並同時在系統啟動的敘述文件內刪除相關數據。
--level <運行級別> 指定讀系統服務要在哪一個執行等級中開啟或關閉
-----
0:表示關機
1:單用戶模式
2:無網絡連接的多用戶命令行模式
3:有網絡連接的多用戶命令行模式
4:不可用
5:帶圖形界面的多用戶模式
6:重新啟動
-----
使用範例,以httpd服務為例:
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 httpd #列出httpd服務設置情況
chkconfig --level 35 httpd on #設定httpd在等級3和5為開機運行服務,--level 35表示操作只在等級3和5執行,on表示啟動,off表示關閉
chkconfig httpd on #設定httpd在各等級為on,“各等級”包括2、3、4、5等級
-----
在chkconfig管理中增加一個服務:
服務腳本必須存放在/etc/ini.d/服務腳本目錄下 :
chkconfig --add 服務的腳本名稱
在chkconfig工具服務列表中增加此服務,此時服務會被在/etc/rc.d/rcN.d中賦予K/S入口了;
chkconfig --level 35 服務腳本名 on
修改服務的默認啟動等級。
-----
# systemd服務管理
systemd是Centos7版本及更高版本中新增的一個服務管理方式,跟centos6之前的chkconfig服務管理不同的地方是,增加了
-----
systemctl用法
使用systemctl list-units -all --type=service列出所有在運行狀態服務的詳細信息(正在運行和暫停的服務),--type=service是指定服務類型的內容,rinning正在運行的,inactive 不活躍的
-----
[root@localhost locales]# systemctl list-units -all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT
abrt-xorg.service loaded inactive dead ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
auditd.service loaded active running Security Auditing Service
-----
systemctl添加一個服務,並啟動、查看服務狀態、停止服務和重啟服務,這裏以crond服務為例
systemctl enable 添加服務,添加服務時如有軟連接會直接顯示出出來其連接路徑,如下(第二行內容):
-----
[root@localhost locales]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
systemctl start 例如啟動crond服務
[root@localhost locales]# systemctl start crond
systemctl status 查看服務的運行狀態,服務狀態如下
[root@localhost locales]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 二 2018-07-17 15:58:15 CST; 2s ago 這裏狀態為active(running)
Main PID: 3962 (crond)
CGroup: /system.slice/crond.service
└─3962 /usr/sbin/crond -n
systemctl stop 停止一個服務,並查看它的狀態信息
[root@localhost locales]# systemctl stop crond
[root@localhost locales]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: inactive (dead) since 二 2018-07-17 15:59:47 CST; 9s ago 這裏狀態為inactive(dead)
Process: 3962 ExecStart=/usr/sbin/crond -n $CRONDARGS (code=exited, status=0/SUCCESS)
Main PID: 3962 (code=exited, status=0/SUCCESS)
systemctl restart 重啟一個服務
[root@localhost locales]# systemctl restart crond
使用systemctl is-enabled 服務名 查看一個服務是否開機啟動
[root@localhost locales]# systemctl is-enabled crond
enabled
-----
# unit介紹
在Centos6中chkconfig管理的服務會擁有幾個運行級別的區分,而centos7中也有類似的啟動級別區分,systemctl啟動的腳本存放在/usr/lib/systemd/system/目錄下,查看系統所有的unit
-----
[root@localhost locales]# ls /usr/lib/systemd/system
abrt-ccpp.service lvm2-lvmetad.service [email protected]
abrtd.service lvm2-lvmetad.socket sshd.socket
abrt-oops.service lvm2-lvmpolld.service suspend.target
abrt-pstoreoops.service lvm2-lvmpolld.socket svnserve.service
abrt-vmcore.service lvm2-monitor.service swap.target
abrt-xorg.service [email protected] sys-fs-fuse-connections.mount
arp-ethers.service machine.slice sysinit.target
auditd.service machines.target sysinit.target.wants
-----
unit中的級別
runlevel0 poweroff 關機
runlevel1 rescue 單用戶救援
......234級別略
runlevel5 graphical 圖形化
runlevel6 reboot 重啟
-----
![](http://i2.51cto.com/images/blog/201807/17/fea79ae49d0aa1a688f218bf94eb356f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
-----
systemd中的unit類型,我們常見的有以.service結尾的,是為服務提供啟動停止管理的unit
service 系統服務
target 多個unit組成的組
device 硬件設備
mount 文件系統掛載點
automount 自動掛載點
path 文件或路徑
scope 不是由systemd啟動的外部進程
slice 進程組
snapshot systemd 快照
socket 進程間通信套接字
swap swap文件
timer 定時器
-----
幾個unit的管理使用命令
systemctl list-units 列出所有正在運行的unit
systemctl list-units --all 列出所有的unit包括失敗和不活躍的(inactive)
systemctl list-units --all --state=inactive 列出所有不活躍(inactive)service的 unit
systemctl list-units --type=service 列出所有正在運行的unit,狀態為active
systemctl is-active crond.service 查看某個服務是否為active
-----
# target管理
查看所有的target
[root@localhost system]# systemctl list-unit-files --type=target
UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static
ctrl-alt-del.target disabled
default.target enabled
emergency.target static
查看一個target下有哪些unit
[root@localhost system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─auditd.service
● ├─brandbot.path
● ├─crond.service
systemctl get-default 查看默認的target
systemctl set-default multi-user.target 指定默認的target,例如指定圖形界面的target啟動級別
一個service屬於一種類型的unit,多個unit組成一個target,target中包含多個service
查看service屬於哪個target(查看install部分的內容),如sshd.service
[root@localhost system]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=forking
PIDFile=/var/run/sshd.pid
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install] 從這部分查看屬於哪個target,這裏的.service中寫入的配置是服務啟動管理的內容
WantedBy=multi-user.target
了解systemctl和chkconfig的服務管理工具