天天看點

linux日常運維(crond,systemd,chkconfing,unit,target)

1、任務計劃:crond

[root@litongyao ~]# cat /etc/crontab                             (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

# |  |  |  |  |

# *  *  *  *  * user-name  command to be executed

(分鐘)(小時)(日期)(月份)(星期)【星期天=0】  使用者(不寫的話預設是root) 指令(這裡的指令必須是絕對路徑)

[root@litongyao ~]# crontab -e                (修改crontab的配置檔案)

0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log2 2>/tmp/1234.log

(每天3點鐘執行/usr/local/sbin/123.sh這個腳本,正确的日志追加到/tmp/123.log下,錯誤的日志追加到/tmp/1234.log下。)

0 3 1-10 */2 2,5 /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log2 2>/tmp/1234.log

(雙數月1-10号周二和周五的淩晨三點運作這個腳本,并追加日志)

要想讓服務正常啟動,則需要啟動服務:

[root@litongyao ~]# systemctl start crond

[root@litongyao ~]# ps aux | grep cron

root       537  0.0  0.1 126236  1612 ?        Ss   07:23   0:00 /usr/sbin/crond -n

root      6506  0.0  0.1 125336  1116 ?        Ss   15:01   0:00 /usr/sbin/anacron -s

root      6625  0.0  0.0 112680   972 pts/0    S+   15:12   0:00 grep --color=auto cron

服務啟動,還可以用

[root@litongyao ~]# systemctl status crond                 (檢視服務啟動情況)

[root@litongyao ~]# crontab -l                         (檢視任務計劃)

其實-l所檢視的目錄就是/var/spool/cron/(這裡會有不同使用者的檔案名,主要是寫任務計劃是使用者是誰的就在誰的目錄下)

[root@litongyao ~]# crontab -r                       (删除任務計劃)

[root@litongyao ~]# crontab -l

no crontab for root

二、linux系統管理chkconfing

centos6和之前的版本會用倒chkconfig,centos7時已經不用,為了相容,我們還是要掌握

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

0級别    關機狀态

1級别    單使用者狀态

2級别    比3少一個nfs服務

3級别    多使用者模式,但是不帶圖像

4級别    保留級别

5級别    多使用者級别。帶圖像

6級别    重新開機

[root@litongyao ~]# chkconfig network off          (設定開機不啟動)

[root@litongyao ~]# chkconfig --level 3 network off             (設定3級别network為關閉狀态)

[root@litongyao ~]# chkconfig --level 345 network off            (設定3.4.5級别,network為關閉狀态)

添加系統服務啟動:(服務啟動腳本放到必須在/etc/init.d檔案下)

舉例:(複制一個network的啟動腳本改名為123,添加服務123.用List檢視。)

linux日常運維(crond,systemd,chkconfing,unit,target)

删除系統服務啟動:

linux日常運維(crond,systemd,chkconfing,unit,target)

三、systemd管理服務

centos7後,使用systemd服務,在之前使用sysv服務。chkconfig在7中也能使用,這一小節則教我們使用systemctl

[root@litongyao ~]# systemctl --all --type=service     (檢視所有的服務,如果去掉all,則未激活的服務不顯示)

[root@litongyao ~]# systemctl is-enabled crond           (檢查服務是否開機啟動)

enabled

[root@litongyao ~]# systemctl disable crond              (不讓開機啟動)

Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

[root@litongyao ~]# systemctl enable crond               (設定開機啟動)

Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

我們不難發現,其實設定開機啟動是給/usr/lib/systemd/system/crond.service.做了一個軟連接配接/etc/systemd/system/multi-user.target.wants/crond.service。

[root@litongyao ~]# ll /etc/systemd/system/multi-user.target.wants/crond.service

lrwxrwxrwx 1 root root 37 12月  4 16:16 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

[root@litongyao ~]# systemctl status crond              (檢視服務的運作狀況)

● crond.service - Command Scheduler

   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

   Active: active (running) since 四 2017-11-30 15:50:14 CST; 4 days ago

 Main PID: 537 (crond)

   CGroup: /system.slice/crond.service

           └─537 /usr/sbin/crond -n

11月 30 15:50:14 litongyao systemd[1]: Started Command Scheduler.

11月 30 15:50:14 litongyao systemd[1]: Starting Command Scheduler...

11月 30 15:50:14 litongyao crond[537]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% if used.)

11月 30 15:50:14 litongyao crond[537]: (CRON) INFO (running with inotify support)

[root@litongyao ~]# systemctl stop crond                      (停止服務)

[root@litongyao ~]# systemctl start crond                      (開啟服務)

四、unit介紹

在系統/usr/lib/systemd/system下存放着所有的unit

unit分為以下幾個類型:

 service 系統服務

 target 多個unit組成的組

 device 硬體裝置

 mount 檔案系統挂載點

 automount 自動挂載點

 path 檔案或路徑

 scope 不是由systemd啟動的外部程序

 slice 程序組

 snapshot systemd快照

 socket 程序間通信套接字

 swap  swap檔案

 timer 定時器

[root@litongyao ~]# cd /usr/lib/systemd/system

[root@litongyao system]# ls -l runlevel*                      (會顯示出來centos7的6個等級)

lrwxrwxrwx. 1 root root 15 10月 20 08:10 runlevel0.target -> poweroff.target

lrwxrwxrwx. 1 root root 13 10月 20 08:10 runlevel1.target -> rescue.target

lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel2.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel3.target -> multi-user.target

lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel4.target -> multi-user.target

lrwxrwxrwx. 1 root root 16 10月 20 08:10 runlevel5.target -> graphical.target

lrwxrwxrwx. 1 root root 13 10月 20 08:10 runlevel6.target -> reboot.target

[root@litongyao system]# systemctl list-units                  (列出正在運作的unit)

[root@litongyao system]# systemctl list-units                  (列出所有,包括失敗的或者inactive的)

[root@litongyao system]# systemctl list-units --all --state=inactive   (列出inactive的unit)

[root@litongyao system]# systemctl list-units --type=service        (列出狀态為active的service)

[root@litongyao system]# systemctl is-active crond              (檢視某個服務是否為active)

五、target介紹

系統為了友善管理用target來管理unit

 systemctl list-unit-files --type=target

 systemctl list-dependencies multi-user.target             //檢視指定target下面有哪些unit

 systemctl get-default                             //檢視系統預設的target

 systemctl set-default multi-user.target

 一個service屬于一種類型的unit

 多個unit組成了一個target

 一個target裡面包含了多個service

 cat /usr/lib/systemd/system/sshd.service //看[install]部分

本文轉自 小新銳 51CTO部落格,原文連結:http://blog.51cto.com/13407306/2047276,如需轉載請自行聯系原作者