linux任務計劃、chkconfig,systemctl介紹及常用命令 ,unit和target介紹 原

10月30日任務
10.23 linux任務計劃cron
10.24 chkconfig工具
10.25 systemd管理服務
10.26 unit介紹
10.27 target介紹
 
 
10.23 linux任務計劃cron
  • crontab -u 、-e、-l  、 -r
  • 格式:分 時 日 月 周  user command
  • 分範圍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@centos6 ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/
 
# 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
#crontab -e 定義一個任務計劃0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>tmp/123.log
[root@centos6 ~]crontab -e
要想讓任務生效,需要執行systemctl start crond 命令,因爲本地系統是centos 6不支持systemctl 。
[root@centos6 ~]# service  crond start
[root@centos6 ~]# ps aux |grep cron
root      1201  0.0  0.1 116908  1304 ?        Ss   Oct16   0:04 crond
root     19782  0.0  0.0 103312   880 pts/0    S+   12:24   0:00 grep --color=auto cron
[root@centos6 ~]# service crond status
crond (pid  1201) is running...
 
 
10.24 chkconfig工具
Linux系統服務管理-chkconfig
  • chkconfig --list
  • chkconfig --level 3 network off
  • chkconfig --level 345 network off
  • chkconfig --del network
  • chkconfig --add network
 
 
[root@zgxlinux-aliyun ~]# 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]'.


aegis              0:off    1:off    2:on    3:on    4:on    5:on    6:off

agentwatch         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@zgxlinux-aliyun ~]# LANG="zh_CN.UTF-8"

[root@zgxlinux-aliyun ~]# chkconfig --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

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

[root@zgxlinux-aliyun ~]# chkconfig network off

[root@zgxlinux-aliyun ~]# chkconfig --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

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


[root@zgxlinux-aliyun ~]# chkconfig network on

[root@zgxlinux-aliyun ~]# chkconfig --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

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

[root@zgxlinux-aliyun ~]# chkconfig --level 3 network off

[root@zgxlinux-aliyun ~]# chkconfig --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

network            0:關    1:關    2:開    3:關    4:開    5:開    6:關
#添加和刪除一個服務,必須要進入/etc/init.d/這個目錄。
[root@zgxlinux-aliyun ~]# cd /etc/init.d/

[root@zgxlinux-aliyun init.d]# ls

aegis  agentwatch  functions  netconsole  network  README

[root@zgxlinux-aliyun init.d]# cp network 123

[root@zgxlinux-aliyun init.d]# ls

123  aegis  agentwatch  functions  netconsole  network  README

[root@zgxlinux-aliyun init.d]# chkconfig --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

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

[root@zgxlinux-aliyun init.d]# chkconfig --add 123

[root@zgxlinux-aliyun 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:關

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

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

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

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

[root@zgxlinux-aliyun init.d]# chkconfig --del 123

[root@zgxlinux-aliyun init.d]# chkconfig  --list


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

原生 systemd 服務。SysV 配置數據

可能被原生 systemd 配置覆蓋。


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

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

      'systemctl list-dependencies [target]'。


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

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

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

network            0:關    1:關    2:開    3:關    4:開    5:開    6:關
 
10.25 systemd管理服務
  • systemctl list-units --all --type=service //列出所有的服務類型爲service
  • 幾個常用的服務相關的命令
  • systemctl enable crond.servuce  //讓服務開機啓動
  • systemctl disable crond   //不讓開機啓動
  • systemctl status crond    //查看狀態
  • systemctl stop crond    //停止服務
  • systemctl start crond    //啓動服務
  • systemctl restart crond   //重啓服務
  • systemctl is-enabled crond   //檢查服務是否開機啓動
 
 
[root@zgxlinux-aliyun ~]# systemctl enable crond

[root@zgxlinux-aliyun ~]# systemctl status crond

● crond.service - Command Scheduler

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

   Active: inactive (dead)


10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Started Command Scheduler.

10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Starting Command Scheduler...

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (RANDOM_DELAY will be...)

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (running with inotify...)

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopping Command Scheduler...

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopped Command Scheduler.

Hint: Some lines were ellipsized, use -l to show in full.

[root@zgxlinux-aliyun ~]# systemctl enable crond.servuce

Failed to execute operation: No such file or directory

[root@zgxlinux-aliyun ~]# systemctl disable crond

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

[root@zgxlinux-aliyun ~]# systemctl status crond

● crond.service - Command Scheduler

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

   Active: inactive (dead)


10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Started Command Scheduler.

10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Starting Command Scheduler...

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (RANDOM_DELAY will be...)

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (running with inotify...)

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopping Command Scheduler...

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopped Command Scheduler.

Hint: Some lines were ellipsized, use -l to show in full.

[root@zgxlinux-aliyun ~]# systemctl enable crond

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

[root@zgxlinux-aliyun ~]# systemctl enable crond

[root@zgxlinux-aliyun ~]# systemctl disable crond

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

[root@zgxlinux-aliyun ~]# systemctl status crond

● crond.service - Command Scheduler

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

   Active: inactive (dead)


10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Started Command Scheduler.

10月 30 13:51:43 zgxlinux-aliyun systemd[1]: Starting Command Scheduler...

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (RANDOM_DELAY will be...)

10月 30 13:51:43 zgxlinux-aliyun crond[470]: (CRON) INFO (running with inotify...)

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopping Command Scheduler...

10月 30 15:02:40 zgxlinux-aliyun systemd[1]: Stopped Command Scheduler.

Hint: Some lines were ellipsized, use -l to show in full.

[root@zgxlinux-aliyun ~]# systemctl stop

Too few arguments.

[root@zgxlinux-aliyun ~]# systemctl is-enabled crond

disabled

[root@zgxlinux-aliyun ~]# systemctl enable crond

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

[root@zgxlinux-aliyun ~]# systemctl is-enabled crond

enabled
 
10.26 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   //列出正在運行的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
 
 
10.27 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]部分
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章