八週一次課 10.23 linux任務計劃cron 10.24 chkconfig工具 10.25 systemd管理服務 10.26 unit介紹 10.27 target介紹

10.23 linux任務計劃cron

任務計劃書寫格式linux

[root@linux-5 ~]# cat /etc/crontab 
SHELL=/bin/bash                      //定義shell變量
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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed   //編輯格式
5個*表明5個時間單位:分 時 日 月 周 
「*」還表明所有的意思,如在第三位使用*表明每日,第四位使用「*」則表明每個月
user-name   用戶名(不寫用戶名,默認是root)
command     想要執行的命令
分範圍0-59,時範圍0-23,日範圍1-31,月範圍1-12,星期1-6(星期日可用0或7表示)
可用格式1-5表示一個範圍1到5
可用格式1,2,3表示1或者2或者3
可用格式*/2表示被2整除的數字,好比小時,那就是每隔2小時

• 定義任務計劃shell

crontab -e

注:定義任務計劃時,編輯須要執行的命令的格式,最好均採用絕對路徑,不然易出現問題vim

• 例:每一個偶數月的1-10號中的週二和週五的03時00分執行腳本123.sh,並將正確日誌追加劇定向到/tmp/123.log,錯誤日誌追加劇定向到/tmp/456.logcentos

0 3 1-10 */2 2,5 /bin/bash /user/loacl/sbin/123.sh >>/tmp/123.log 2>>/tmp/456.log

• 要保證服務是啓動狀態,才能保證任務計劃可以正常使用bash

systemctl start crond

• 檢查服務是否正常啓動socket

[root@linux-5 ~]# ps aux |grep cron
root        566  0.0  0.0 126232  1664 ?        Ss   12:42   0:00 /usr/sbin/crond -n
root       1707  0.0  0.0 112676   984 pts/0    S+   13:52   0:00 grep --color=auto cron

[root@linux-5 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2018-05-11 12:42:52 CST; 1h 11min ago  //已經啓動
 Main PID: 566 (crond)
   CGroup: /system.slice/crond.service
           └─566 /usr/sbin/crond -n

5月 11 12:42:52 linux-5 systemd[1]: Started Command Scheduler.
5月 11 12:42:52 linux-5 systemd[1]: Starting Command Scheduler...
5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 93% if used.)
5月 11 12:42:52 linux-5 crond[566]: (CRON) INFO (running with inotify support)

• 列出任務計劃ide

crontab -l

• 任務計劃的文件位置(備份任務計劃時,可直接拷貝該文件)工具

/var/spool/cron/username

• 指定用戶centos7

crontab -u username

• 刪除任務計劃spa

crontab -r

10.24 chkconfig工具

chkconfig:centos6以及6之前版本所使用的服務管理工具

• 列出全部服務

[root@linux-5 ~]# 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:關

7個啓動級別(centos6及之前):

0級別:關機

1級別: 單用戶

2級別:多用戶模式(不帶nfs服務)

3級別: 多用戶模式(不帶圖形)

4級別: 保留級別

5級別: 多用戶(帶有圖形)

6級別: 重啓

• 指定某一服務的某個級別開啓或關閉

chkconfig --level 級別數 服務名稱 on/off

• 指定某一服務的多個級別開啓或關閉

chkconfig --level 345 服務名稱 on/off   //345級別開啓或關閉

• 添加某一服務到服務列表

chkconfig --add network

• 從服務列表刪除某一服務

chkconfig --del network

注:自定義服務文件必須放置在/etc/init.d目錄下,且文件內容有必定的限制

[root@linux-5 init.d]# vim !$
vim network

#! /bin/bash
#
# network       Bring up/down networking
#
# chkconfig: 2345 10 90                                              //不能丟
# description: Activates/Deactivates all network interfaces configured to \  //不能丟
#              start at boot time.                                   //不能丟
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO

# Source function library.

10.25 systemd管理服務

systemd:centos7的服務管理機制

• 列出全部類型爲service的units

systemctl list-units --all --type=service

注:--all會顯示出inactive的service

• 讓服務開機啓動

[root@linux-5 ~]# systemctl enable crond.service              //.service可加可不加
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

注:讓服務開機啓動會使系統生成一個軟連接/etc/systemd/system/multi-user.target.wants/crond.service,真正的配置文件目錄位於/usr/lib/systemd/system/crond.service.

• 不讓服務開機啓動

[root@linux-5 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

注:不讓服務開機啓動,會將軟連接清除

• 查看狀態

systemctl status crond

• 中止服務

systemctl stop crond

• 啓動服務

systemctl start crond

• 重啓服務

systemctl restart crond

• 檢查服務是否開機啓動

[root@linux-5 ~]# systemctl is-enabled crond 
enabled

10.26 unit介紹

• unit的類型

ls /usr/lib/systemd/system //系統全部unit

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

• 列出正在運行的unit

systemctl list-units

• 列出全部,包括失敗的或者inactive的unit

systemctl list-units --all

• •列出inactive的unit

systemctl list-units --all --state=inactive

• 列出狀態爲active的service

systemctl list-units --type=service

• 查看某個服務是否爲active/enable

systemctl is-active/enable 服務名稱

10.27 target介紹

target:系統爲了方便管理用target來管理unit,一個service屬於unit的一種類型,多個unit組成了一個target。

• 列出unit文件中的target文件

systemctl list-unit-files --type=target

• 查看指定target下面有哪些unit

systemctl list-dependencies 服務名稱.target

• 查看系統默認的target

systemctl get-default

• 修改系統默認的target(可修改系統的啓動等級)

systemctl set-default multi-user.target

注:修改完成後一樣會生成軟連接文件

• 查看service屬於哪個target

cat /usr/lib/systemd/system/服務名稱.service //看[install]部分
相關文章
相關標籤/搜索