任務計劃cron、服務管理工具chkconfig/systemctl

10月30日任務node

10.23 linux任務計劃cronlinux

10.24 chkconfig工具centos

10.25 systemd管理服務bash

10.26 unit介紹session

10.27 target介紹運維

 

任務計劃 crontab

在某個時間執行某些命令或腳本,作到自動化運維dom

cron服務配置文件ssh

[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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

經常使用參數

  • -l 列出計劃任務表
# 初始爲設置,計劃任務表爲空
[root@localhost ~]# crontab -l
no crontab for root
  • -e 編輯計劃任務表
# 編輯
[root@localhost ~]# crontab -e
# 進入編輯模式,按i鍵後輸入
0 3 * * 1-5 /usr/bin/tar -cvf /home/user1 >> user.log 2>&1

按esc退出編輯模式,:wq保存退出

# 查看驗證
[root@localhost ~]# crontab -l
0 3 * * 1-5 /usr/bin/tar -cvf /home/user1 >> user.log 2>&1
  • -u USER (能夠配合其餘參數使用)
[root@localhost ~]# crontab -u user1 -e

[root@localhost ~]# crontab -u user1 -l
0 8 * * * echo "good morning"

默認不指定-u,設置或顯示的是當前用戶
  • -r 刪除用戶的計劃任務表(配合-u指定用戶)
[root@localhost ~]# crontab -r -u user1
[root@localhost ~]# crontab -u user1 -l
no crontab for user1

cron樣例

[root@localhost ~]# man 5 crontab
...
EXAMPLE CRON FILE
       # use /bin/sh to run commands, no matter what /etc/passwd says
       SHELL=/bin/sh
       # mail any output to `paul', no matter whose crontab this is
       MAILTO=paul
       #
       CRON_TZ=Japan
       # run five minutes after midnight, every day
       5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # run at 2:15pm on the first of every month -- output mailed to paul
       15 14 1 * *     $HOME/bin/monthly
       # run at 10 pm on weekdays, annoy Joe
       0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
       23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
       5 4 * * sun     echo "run at 5 after 4 every sunday"
...

系統服務管理chkconfig

centos6及以前版本使用socket

在centos7中已經再也不使用chkconfig來進行系統服務的管理,改成systemctl命令了,因此只顯示了部分服務工具

基本使用

  • 顯示管理的服務
[root@localhost ~]# 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:關
  • 關閉在指定運行級的上的服務
[root@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# 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:關

# 要同時關閉多個運行級的服務,如3,4,5級應該寫成345(中間沒有空格和逗號)
[root@localhost ~]# chkconfig --level 345  network off
[root@localhost ~]# 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:關
  • 向管理列表添加服務
# 將新服務的啓動腳本加入到/etc/init.d/目錄下(這裏就簡單拷貝下作個示範)
[root@localhost ~]# cp /etc/init.d/network /etc/init.d/ssh

# 添加新服務至管理列表
[root@localhost ~]# chkconfig --add ssh
[root@localhost ~]# 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:關
ssh            	0:關	1:關	2:開	3:開	4:開	5:開	6:關
  • 刪除管理列表中的服務
[root@localhost etc]# chkconfig --del ssh
[root@localhost etc]# 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:關

系統服務管理systemd(centos7)

[root@localhost ~]# systemctl list-units --all --type=service
  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service            loaded    active   running Security Auditing 
  brandbot.service          loaded    inactive dead    Flexible Branding 
  chronyd.service           loaded    active   running NTP client/server
  cpupower.service          loaded    inactive dead    Configure CPU powe
  crond.service             loaded    active   running Command Scheduler
  dbus.service              loaded    active   running D-Bus System Messa
● display-manager.service   not-found inactive dead    display-manager.se
  dracut-shutdown.service   loaded    inactive dead    Restore /run/initr
  ebtables.service          loaded    inactive dead    Ethernet Bridge Fi
  emergency.service         loaded    inactive dead    Emergency Shell
  ...
  systemd-vconsole-setup.service loaded    active   exited  Setup Virtual
  tuned.service             loaded    active   running Dynamic System Tun
  vmtoolsd.service          loaded    active   running Service for virtua

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB
SUB    = The low-level unit activation state, values depend on unit type.

83 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 76-91/91 (END)

# 不加--all,只列出active狀態的服務
[root@localhost ~]# systemctl list-units --type=service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service            loaded active running Security Auditing Servi
  chronyd.service           loaded active running NTP client/server
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message Bu
  firewalld.service         loaded active running firewalld - dynamic fir
  getty@tty1.service        loaded active running Getty on tty1
...
  • 設置服務開機啓動/不啓動
# 開機啓動,服務後可加可不加.service,系統會建立服務腳本文件的軟連接
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

# crond.service腳本內容以下
[root@localhost ~]# cat /usr/lib/systemd/system/crond.service 
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target


# 設置開機不啓動,系統移除服務腳本的軟連接
[root@localhost ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
  • 查看服務狀態
[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 四 2017-10-26 17:51:07 CST; 2h 28min ago
 Main PID: 525 (crond)
   CGroup: /system.slice/crond.service
           └─525 /usr/sbin/crond -n

10月 26 17:51:07 localhost.localdomain systemd[1]: Started Command Sc...
10月 26 17:51:07 localhost.localdomain systemd[1]: Starting Command S...
10月 26 17:51:08 localhost.localdomain crond[525]: (CRON) INFO (RANDO...
10月 26 17:51:10 localhost.localdomain crond[525]: (CRON) INFO (runni...
Hint: Some lines were ellipsized, use -l to show in full.
  • 中止服務
[root@localhost ~]# systemctl stop crond
  • 啓動服務
[root@localhost ~]# systemctl start crond
  • 重啓服務
[root@localhost ~]# systemctl restart crond
  • 檢查服務是否開機啓動
[root@localhost ~]# systemctl is-enabled crond
enabled

unit介紹

列出系統內全部unit

[root@localhost ~]# ls /usr/lib/systemd/system
arp-ethers.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service
chrony-dnssrv@.timer
chronyd.service
chrony-wait.service
...

unit分爲如下類型 類型 | 說明 | 舉例 --- | --- | --- service | 系統服務 | crond.service target | 多個unit組成的組 | timers.target device | 硬件設備 | mount | 文件系統掛載點 | dev-mqueue.mount automount | 自動掛載點 | path | 文件或路徑 | systemd-ask-password-console.path scope | 不是由systemd啓動的外部進程 | slice | 進程組 | system.slice snapshot | systemd快照 | socket | 進程間通訊套接字 | systemd-shutdownd.socket swap | swap文件 | timer | 定時器 | systemd-readahead-done.timer

unit相關命令

  • 只顯示active狀態的unit;加上--all參數所有顯示
[root@localhost system]# systemctl list-units
  UNIT                      LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount loaded active waiting   Arbitrary Exe
  sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-bloc
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loade
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loa
  sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged 
  sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged 
  sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged 
  sys-devices-pnp0-00:05-tty-ttyS0.device loaded active plugged   /sys/de
  sys-module-configfs.device loaded active plugged   /sys/module/configfs
  sys-subsystem-net-devices-ens33.device loaded active plugged   82545EM 
lines 1-16
  • 只顯示狀態爲inactive的unit(配合--type=service再次只刷選出服務)
[root@localhost system]# systemctl list-units --all --state=inactive
  UNIT                      LOAD      ACTIVE   SUB  DESCRIPTION
  proc-sys-fs-binfmt_misc.mount loaded    inactive dead Arbitrary Executa
  sys-fs-fuse-connections.mount loaded    inactive dead FUSE Control File
  tmp.mount                 loaded    inactive dead Temporary Directory
  systemd-ask-password-console.path loaded    inactive dead Dispatch Pass
  brandbot.service          loaded    inactive dead Flexible Branding Ser
  cpupower.service          loaded    inactive dead Configure CPU power r
● display-manager.service   not-found inactive dead display-manager.servi
  dracut-shutdown.service   loaded    inactive dead Restore /run/initramf
  ebtables.service          loaded    inactive dead Ethernet Bridge Filte
  emergency.service         loaded    inactive dead Emergency Shell
● exim.service              not-found inactive dead exim.service
  • 只顯示狀態爲active的服務
[root@localhost system]# systemctl list-units --type=service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service            loaded active running Security Auditing Servi
  chronyd.service           loaded active running NTP client/server
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message Bu
  firewalld.service         loaded active running firewalld - dynamic fir
  getty@tty1.service        loaded active running Getty on tty1
● kdump.service             loaded failed failed  Crash recovery kernel a
  kmod-static-nodes.service loaded active exited  Create list of required
  network.service           loaded active exited  LSB: Bring up/down netw
  NetworkManager-wait-online.service loaded active exited  Network Manage
  NetworkManager.service    loaded active running Network Manager
  polkit.service            loaded active running Authorization Manager
  postfix.service           loaded active running Postfix Mail Transport 
  rhel-dmesg.service        loaded active exited  Dump dmesg to /var/log/
  rhel-import-state.service loaded active exited  Import network configur
lines 1-16

[root@localhost system]# systemctl list-units --state=active
  • 查看服務是否啓動
[root@localhost system]# systemctl is-active crond.service
active

target介紹

爲了方便管理用target來管理unit,一個target是多個unit的組合

  • 列出系統內全部的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  
final.target              static  
getty.target              static  
graphical.target          static  
halt.target               disabled
hibernate.target          static  
hybrid-sleep.target       static  
initrd-fs.target          static  
...
  • 查看指定target下面的units組成

target下還能夠有target

[root@localhost system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
...
  • 查看系統默認的target
[root@localhost system]# systemctl get-default 
multi-user.targe
  • 設置系統默認的target(相似於centos6內的修改系統運行級別
[root@localhost system]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

CentOS6中在 /etc/inittab 文件中能夠修改系統的運行級別,而在CentOS7中使用了相似的target來管理運行級

# /usr/lib/systemd/system目錄下
[root@localhost system]# ls -l runlevel*.target
lrwxrwxrwx. 1 root root 15 10月 18 02:32 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 10月 18 02:32 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 10月 18 02:32 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 10月 18 02:32 runlevel6.target -> reboot.target

小結

  • 一個service屬於一種類型的unit
  • 同類型的多個unit組成一個target
  • 一個target內包含多個service

查看service屬於哪一個target(看Install塊)

[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]
WantedBy=multi-user.target
相關文章
相關標籤/搜索