日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

34.任务计划cron chkconfig systemctl管理服务 unit target

發布時間:2024/4/13 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 34.任务计划cron chkconfig systemctl管理服务 unit target 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

10.23 linux任務計劃cron

10.24 chkconfig工具

10.25 systemd管理服務

10.26 unit介紹

10.27 target介紹

?

?

10.23 linux任務計劃cron:

在linux中任務計劃是必不可少的,因為可能我們凌晨的時候去做一些事情。可能是備份數據或是重啟服務啊,這個操作的過程可能是一個腳本,也有可能是一個單獨的命令。不管是什么,總得有一個時間要去執行他所以任務計劃必不可少。

?

?

~. cat /etc/crontab 這個文件下是任務計劃的配置文件,他會定義shell 環境變量、發送郵件給誰

[root@axinlinux-01 ~]# 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) 第一位為表示分鐘,范圍是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

這5個星表示它的 位

user-name 在root用戶下定義任務就是root。不寫的話就是root

command to be executed 表示要執行的命令,怎么定義呢,寫法為:

crontab -e,這樣就進入了crontab的配置文件當中

我們寫好的他的任務,怎么確定他的唯一性呢。用周來確定。因為今年的周一跟明年的周一是不一樣的

?

~1. crontab -u 、 -e 、 -l 、 -r

綜上所述,

-e 進入 crontab的配置文件當中。用法跟vim一樣的。按 a 進入編輯模式

-l 可以查看寫的任務計劃,列出

-r 刪除任務

-u 指定用戶 例如,crontab -u root -r

~3. 文件 /var/spool/cron/username

crontab的文件在這個路徑里面。是用戶對應的文件,比如root的就會寫上root的,其他的就會寫上其他的用戶名字,以用戶的名字命名的一個文件

~2. 格式:分 時 日 月 周

(user command 進入編輯模式之后,以分時日月周的模式來寫,以空格間隔。后面跟命令)

例如 0 3 * * * /binsbin /usr/local/sbin/123.sh (比如寫了一個腳本,就這樣寫任務計劃。每天3點執行這個腳本)

0 3 * * * /binsbin /usr/local/sbin/123.sh > /tmp/123.log 2> /tmp/123.log(每天3三點將這個腳本正確的和錯誤的都輸出到這個文件里來。也可以是追加>>

*表示相對應的第幾位,表示所有,每天都執行、每月都執行、每周都執行

?

~4. 分范圍0-59,時范圍0-23,日范圍1-31,月范圍1-12,周范圍0-6

~5. 可用格式1-5表示一個范圍 1到5

0 3 1-5 * * /binsbin /usr/local/sbin/123.sh

1-5號的每天3點執行這個腳本

~6. 可用格式1,2,3表示1或者2或者3

0 0 0 */2 2,5 /binsbin /usr/local/sbin/123.sh

每兩個月的周二和周五來執行這個腳本

以逗號分隔,周二和周五

~7. 可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時。只要是被2整除的

0 3 1-5 */2 * /binsbin /usr/local/sbin/123.sh

每兩個月,1-5號的3點執行這個腳本

~8. 要保證服務是啟動狀態

systemctl start crond.service,輸入這個才會讓他啟動這個服務

啟動之后可以用 ps aux | grep crond 查看一下,如果有這個服務就代表他已經啟動了

也可以用 systemctl status crond 來查看crond的狀態。有active (running)并顯示為綠色表示已經啟動。相反的我們systemctl stop crond 停掉crond,就會有顯示inactive (dead)并沒有顏色

?

需要注意的是,我們在寫完任務計劃的時候。必須要啟動這個任務計劃。 systemctl start crond。

我們在寫任務的時候,有用到命令,要寫命令的絕對路徑才可以生效

任務計劃里寫上正確和錯誤的輸出文件,以方便我們有據可查

?

?

?

----------------------------------------------------------------------------------------------------------------------------------------------------

?

?

10.24 chkconfig工具:

?

linux系統的服務管理,像我們之前接觸到的 crond、firewalld等等都是服務,那我們可以用chkconfig來管理這些服務。怎么樣控制這個服務啟動,怎么控制這個服務開機啟動,怎么讓這個服務在指定的級別去啟動

chkconfig是centos6之前的版本用的

?

~1. 我們先來執行 chkconfig --list 列出chkconfig管理的服務

[root@axinlinux-01 ~]# chkconfig --list

?

注:該輸出結果只顯示 SysV 服務,并不包含 6以前的版本服務機制是sysv,所以只顯示sysv服務

原生 systemd 服務。SysV 配置數據 7以后得版本為systemd服務機制,所以不會顯示

可能被原生 systemd 配置覆蓋。

?

要列出 systemd 服務,請執行 'systemctl list-unit-files'。所以想要看systemd服務的要執行該命令

查看在具體 target 啟用的服務請執行 target是systemd服務的另一個概念

'systemctl list-dependencies [target]'。

sysv服務機制的只剩下這兩個了

netconsole 0:關 1:關 2:關 3:關 4:關 5:關 6:關 。0-6狀態各表示6以前版本的運行級別

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

0 關機狀態 1單用戶 2多用戶模式(少了nfs服務) 3多用戶模式(不帶圖形)

4保留級別(暫時沒用) 5 多用戶帶圖形 6重啟

?

~2. 這些服務都是為一個服務的腳本,目錄在

/etc/init.d/ 這個里面

[root@axinlinux-01 ~]# ls /etc/init.d/

functions netconsole network README

后期我們學其他服務的時候,還會用到這個路徑。把一些啟動文件放進來,用chkconfig來管理

?

~3. chkconfg --level 3 network off 指定network服務的3級別關閉

多個級別同時關閉或打開,不用間隔直接寫就好。例如:345

0和6級別不會開啟服務,因為分別處于關機和重啟。1級別也不會開,處于單用戶模式

?

~4. chkconfig --add 123 把123服務加入到這個服務列表里面來

[root@axinlinux-01 ~]# cd /etc/init.d 前提是要在這個目錄里的服務,才可以進入到列表里來

[root@axinlinux-01 init.d]# ls

functions netconsole network README

[root@axinlinux-01 init.d]# cp network 123 為保險我們復制一個123,為什么要復制因為文件是有格式的,才能被識別

[root@axinlinux-01 init.d]# chkconfig --add 123 把123加入到列表里

[root@axinlinux-01 init.d]# chkconfig --list 查看一下

?

注:該輸出結果只顯示 SysV 服務,并不包含

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。

?

要列出 systemd 服務,請執行 'systemctl list-unit-files'。

查看在具體 target 啟用的服務請執行

'systemctl list-dependencies [target]'。

?

123 0:關 1:關 2:開 3:開 4:開 5:開 6:關 123服務

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

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

?

~5. chkconfig --del 123 把123服務在列表里刪除

?

----------------------------------------------------------------------------------------------------------------------------------------------------

?

10.25 systemd管理服務:

?

systemd是centos7系統的服務管理機制

?

~1. systemctl list-unit-files

我們在chkconfig --list的時候,不會列出systemd的服務。但是會提示我們

“要列出 systemd 服務,請執行 'systemctl list-unit-files'”

我們執行這個systemctl list-unit-files,(按空格往下翻)。會發現他不僅顯示servicehaiyou target等等服務,會比較亂。

systemctl list-units --all --type=service(只用這個查看systend服務,只查看service的)

因為我們看的僅僅只是service服務,所以我們可以用 systemctl list-units --all --type=service。這樣列出的服務就只有service

?

?

~2.幾個常用的服務的命令(重點,常用!!!!)

systemctl enable crond.service 讓crond服務開機啟動(后綴.service可省略)

syttemctl disable crond.service 不讓crond服務開啟啟動(后綴.service可省略)

systemctl status crond 查看crond狀態

含有active (running)且為綠色,表示已啟動

systemctl stop crond 停止crond服務

systemctl start crond 啟動crond服務

systemctl restart crond 重啟crond這個服務

systemctl is-enabled crond 檢查crond這個服務是否開機啟動

顯示enabled即為開機啟動 顯示disabled即為非開機啟動

?

?

----------------------------------------------------------------------------------------------------------------------------------------------------

?

?

?

10.26 unit介紹:

(systemd管理服務)

什么叫unit?

通過上一節,我們學到了systemctl,簡單知道了 enabled disabled。如果這個服務為 enabled,他就會創建一個軟連接,這個軟連接的目錄為

ls /usr/lib/systemd/system

[root@axinlinux-01 ~]# ls /usr/lib/systemd/system

arp-ethers.service machine.slice sysinit.target

auditd.service machines.target sysinit.target.wants

autovt@.service messagebus.service sys-kernel-config.mount

basic.target microcode.service sys-kernel-debug.mount

basic.target.wants multi-user.target syslog.socket

blk-availability.service multi-user.target.wants syslog.target.wants

會發現他有很多的類型,像target、mount、wants(目錄)。而這些類型的文件就叫做unit

?

系統所有unit分為以下類型:

~ service 系統服務

~ target 多個組成的組

之間講到6以前的版本,有7個運行級別。而7也有類似的,就是target文件

[root@axinlinux-01 ~]# cd !$

cd /usr/lib/systemd/system 同樣在這個路徑里

[root@axinlinux-01 system]# ls -l runlevle* 查看runlevel文件

ls: 無法訪問runlevle*: 沒有那個文件或目錄

[root@axinlinux-01 system]# ls -l runlevel*

同樣也有7個運行級別的target,通過也是軟連接。他的源就是后面的文件。跟6的7個運行級別,基本對應起來了

lrwxrwxrwx. 1 root root 15 5月 29 07:11 runlevel0.target -> poweroff.target

lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel1.target -> rescue.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel2.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel3.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 5月 29 07:11 runlevel4.target -> multi-user.target

lrwxrwxrwx. 1 root root 16 5月 29 07:11 runlevel5.target -> graphical.target

lrwxrwxrwx. 1 root root 13 5月 29 07:11 runlevel6.target -> reboot.target

?

~ device 硬件設備

~ mount 文件系統的掛載點

~ automount 自動掛載點

~ path 文件或路徑

~ scope 不是由systend啟動的外部進程 比較陌生,熟悉就好

~ slice 進程組

~ snapshot systemd快照

~ socket 進程間通信套接字

~ swap swap文件

~ timer 定時器

?

unit相關的命令:

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

~ systemctl list-units --all 列出所有,包括失敗的或者inactive的

~ systemctl list-units --all --state=inactive 列出inactive(閑置的)的unit

~ systemctl lit-units --type=service 列出狀態為active(活躍的)的service

~ systemctl is-active crond.service 查看某個服務是否為active

?

?

----------------------------------------------------------------------------------------------------------------------------------------------------

?

?

10.27 target介紹:

(systemd管理服務)

?

一個target是由多個unit組合起來的,那么他們是什么聯系起來的:

系統為了方便管理,用target來管理unit

?

~ systemctl list-unit-files --type=target 這個命令可以列出系統里面所有的target

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

~ systemctl get-default 查看系統默認的target

就是multi-user.target

~ systemctl set-default multi-user.target 設定默認的target(multi-user.target)

重新設定的時候就會創建一個新的軟連接

?

! 一個service 屬于其中一種類型的unit

多個unit組成了一個target

一個target里面包含了多個service

cat /usr/lib/systemd/system/sshd.service 看install部分 ,可看到他屬于哪個target。例如:

cat這個目錄,查看sshd.service。看install部分就會知道 WantedBy=multi-user.target

?

總結:

系統可以說是由多種unit組成的,這么多的unit為了方便管理,那么我們把他們歸類,歸類成若干個組,我們把它叫做taeget。也就是說target是有多個unit的組成的。那service屬于其中一種類型的unit,一個target里面包含了多個service

?

轉載于:https://my.oschina.net/u/3866149/blog/1859058

總結

以上是生活随笔為你收集整理的34.任务计划cron chkconfig systemctl管理服务 unit target的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。