Linux 任務計劃和服務管理 原

Linux任務計劃cron

  • crontab -u/-e/-l/-r
  • 格式: 分 時 日 月 周 user command # 可以通過配置文件查看 cat /etc/crontab
  • 文件 /var/spool/cron/username
  • 分範圍0-59,時範圍0-23,日範圍0-31,月範圍0-12,周1-6
  • 可用格式1-5表示一個範圍1到5
  • 可用格式1,2,3表示1或者2或者3
  • 可用格式*/2表示被2整除的數字,比如小時,那就是每隔2小時
  • 要保證服務是啓動狀態
  • systemctl start crond.service
[root@test-a ~]# 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

[root@test-a ~]# crontab -e # 定時腳本內容: 10 10 10 * * /usr/bin/ls /tmp
crontab: installing new crontab

Linux 系統服務管理 — chkconfig

  • chkconfig --list # 列出所有的服務
  • chkconfig --level 3 network off
  • chkconfig --level 345 network off
  • chkconfig --del network
  • chkconfig --add network
  • 0 關機狀態 1 單用戶級別 2多用戶模式 3多用戶模式(多了nfs模式) 4保留級別 5多用戶(帶圖形) 6 重啓 (Centos7不再提倡使用級別模式)
[root@test-a ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@test-a ~]# ls /etc/init.d/  # 啓動腳本路徑
functions  iprdump  iprinit  iprupdate  netconsole  network  README

[root@test-a ~]# chkconfig network off
[root@test-a ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:off   3:off   4:off   5:off   6:off

[root@test-a ~]# chkconfig network on
[root@test-a ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@test-a ~]# chkconfig --level 3 network off
[root@test-a ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:off   4:on    5:on    6:off

[root@test-a ~]# chkconfig --level 345 network on
[root@test-a ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

[root@test-a ~]# cd /etc/init.d/
[root@test-a init.d]# cp network testserv
[root@test-a init.d]# chkconfig --add testserv
[root@test-a init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off
testserv        0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@test-a init.d]# chkconfig --del testserv
[root@test-a init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

iprdump         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprinit         0:off   1:off   2:on    3:on    4:on    5:on    6:off
iprupdate       0:off   1:off   2:on    3:on    4:on    5:on    6:off
netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

Linux 系統服務管理 — systemd

  • systemctl list-units --all --type=service # 列出所有的服務及信息

幾個常用的服務相關命令

  • systemctl enable crond.service # 讓服務開機啓動
  • systemctl disable crond # 不讓開機啓動
  • systemctl status crond # 查看服務狀態
  • systemctl stop crond # 啓動服務
  • systemctl start crond # 啓動服務
  • systemctl restart crond # 重啓服務
  • systemctl is-enabled crond # 查看服務是否開機啓動
[root@test-a init.d]# systemctl enable crond.service
[root@test-a init.d]# systemctl disable crond
rm '/etc/systemd/system/multi-user.target.wants/crond.service'
[root@test-a init.d]# systemctl is-enable crond
Unknown operation 'is-enable'.
[root@test-a init.d]# systemctl is-enabled crond
disabled
[root@test-a init.d]# systemctl enable crond.service
ln -s '/usr/lib/systemd/system/crond.service' '/etc/systemd/system/multi-user.target.wants/crond.service'
[root@test-a init.d]# systemctl is-enabled crond
enabled

unit介紹

ls /usr/lib/systemd/system # 可以查看系統所有unit,分爲以下類型

  • service 系統服務
  • target 多個unit組成的組
  • device 硬件設備
  • mount 文件系統掛載點
  • automount 自動掛載點
  • path 文件或路徑
  • scope 不是由systemd啓動的外部進程
  • slice 進程組
  • snapshot systemd 快照
  • socket 進程間通信套接字
  • swap swap文件
  • timer 定時器
[root@test-a ~]# ls /usr/lib/systemd/system/
arp-ethers.service                      remote-fs-pre.target
auditd.service                          remote-fs.target
[email protected]                         rescue.service
avahi-daemon.service                    rescue.target
avahi-daemon.socket  
...

[root@test-a ~]# ls /usr/lib/systemd/system/runlevel*target -l # 查看centos下的7個運行模式  
lrwxrwxrwx. 1 root root 15 Sep  7 09:50 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Sep  7 09:50 /usr/lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Sep  7 09:50 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Sep  7 09:50 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Sep  7 09:50 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Sep  7 09:50 /usr/lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Sep  7 09:50 /usr/lib/systemd/system/runlevel6.target -> reboot.target

unit 相關的命令

  • systemctl list-units # 列出正在運行的unit
  • systemctl list-units --all # 列出所有,包括失敗的或者inactive的
  • systemctl list-units --all --state=inactive # 列出inactive的unit
  • systemctl list-units --type=service # 列出狀態爲active的service
  • systemctl is-active crond.service # 查看某個服務是否爲active

target 介紹

系統爲了方便管理用target來管理unit

  • systemctl list-unit-files --type=target #查看系統中所有的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]部分
[root@test-a ~]# systemctl get-default
multi-user.target


[root@test-a ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
After=syslog.target network.target auditd.service

[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStartPre=/usr/sbin/sshd-keygen
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target   # 屬於multi-user.target
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章