1. 程式人生 > >cron,chkconfig,systemd,unit,target介紹

cron,chkconfig,systemd,unit,target介紹

serve config play sysconfig auditd 默認 port ssa tar

cron任務計劃

技術分享圖片
1.配置文件路徑及模板:

[root@weix-01 ~]# 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

2.編輯該模板:

[root@weix-01 ~]# crontab -e
no crontab for root - using an empty one
0 3 1-10 */2 2,5  /bin/bash /usr/local/123.sh >> /tmp/123.log 2>> /tmp/123.log

3.啟動服務並查看:

[root@weix-01 ~]# systemctl start crond
[root@weix-01 ~]# ps aux |grep crond
root        564  0.0  0.1 126236  1636 ?        Ss   16:18   0:00 /usr/sbin/cron d -n
root       1051  0.0  0.0 112676   984 pts/0    S+   16:27   0:00 grep --color=a uto crond
[root@weix-01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset : enabled)
   Active: active (running) since 一 2018-01-29 16:18:19 CST; 9min ago
 Main PID: 564 (crond)
   CGroup: /system.slice/crond.service
           └─564 /usr/sbin/crond -n

1月 29 16:18:19 weix-01 systemd[1]: Started Command Scheduler.
1月 29 16:18:19 weix-01 systemd[1]: Starting Command Scheduler...
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (RANDOM_DELAY will be scal....)
1月 29 16:18:19 weix-01 crond[564]: (CRON) INFO (running with inotify support)
Hint: Some lines were ellipsized, use -l to show in full.

4.常用用法:

[root@weix-01 ~]# crontab -e                   # -e編輯
no crontab for root - using an empty one
crontab: installing new crontab
[root@weix-01 ~]# corntab -l                  # -l 列出
-bash: corntab: 未找到命令
[root@weix-01 ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@weix-01 ~]# cat /var/spool/cron/root              #文件存放路徑
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@weix-01 ~]# crontab -r                     # -r 刪除
[root@weix-01 ~]# crontab -l
no crontab for root

chkconfig工具

技術分享圖片
1.列出所有服務:

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

2.將指定服務開機啟動或不啟動:

[root@weix-01 ~]# chkconfig network off
[root@weix-01 ~]# 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@weix-01 ~]# chkconfig network on
[root@weix-01 ~]# 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.指定某個服務某個級別開關:

[root@weix-01 ~]# chkconfig --level 35 network off
[root@weix-01 ~]# 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@weix-01 ~]# chkconfig --level 35 network on
[root@weix-01 ~]# 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:關

4.自定義添加啟動腳本:腳本要在init.d路徑下,且文件格式有要求

[root@weix-01 ~]# cd /etc/init.d/
[root@weix-01 init.d]# cp network  123
[root@weix-01 init.d]# ls -l
總用量 48
-rwxr-xr-x. 1 root root  7293 1月  29 16:55 123
-rw-r--r--. 1 root root 17500 5月   3 2017 functions
-rwxr-xr-x. 1 root root  4334 5月   3 2017 netconsole
-rwxr-xr-x. 1 root root  7293 5月   3 2017 network
-rw-r--r--. 1 root root  1160 8月   5 14:38 README
[root@weix-01 init.d]# 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@weix-01 init.d]# chkconfig --add 123
[root@weix-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:關
netconsole      0:關    1:關    2:關    3:關    4:關    5:關    6:關
network         0:關    1:關    2:開    3:開    4:開    5:開    6:關
#! /bin/bash
#
# network       Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to #              start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO

# Source function library.
. /etc/init.d/functions

if [ ! -f /etc/sysconfig/network ]; then
    exit 6
fi

. /etc/sysconfig/network

5.刪除啟動腳本:

[root@weix-01 init.d]# chkconfig --del 123
[root@weix-01 init.d]# 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

技術分享圖片
1.查看服務:

[root@weix-01 ~]# systemctl list-units --all --type=service
  UNIT                        LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service              loaded    active   running Security Auditing Servic
  brandbot.service            loaded    inactive dead    Flexible Branding Servic
  chronyd.service             loaded    active   running NTP client/server
  cpupower.service            loaded    inactive dead    Configure CPU power rela
  crond.service               loaded    active   running Command Scheduler
  dbus.service                loaded    active   running D-Bus System Message Bus
● display-manager.service     not-found inactive dead    display-manager.service
  dm-event.service            loaded    inactive dead    Device-mapper event daem
  dracut-shutdown.service     loaded    inactive dead    Restore /run/initramfs
  ebtables.service            loaded    inactive dead    Ethernet Bridge Filterin
  emergency.service           loaded    inactive dead    Emergency Shell
● exim.service                not-found inactive dead    exim.service

2.讓服務開機啟動,不啟動:

[root@weixing01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@weixing01 ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

3.查看狀態:

[root@weixing01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2018-01-29 22:21:00 CST; 4min 4s ago
 Main PID: 542 (crond)
   CGroup: /system.slice/crond.service
           └─542 /usr/sbin/crond -n

1月 29 22:21:00 weixing01 systemd[1]: Started Command Scheduler.
1月 29 22:21:00 weixing01 systemd[1]: Starting Command Scheduler...
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (RANDOM_DELAY will be s...)
1月 29 22:21:00 weixing01 crond[542]: (CRON) INFO (running with inotify s...)
Hint: Some lines were ellipsized, use -l to show in full.

4.停止,啟動,重啟服務:

[root@weixing01 ~]# systemctl stop crond
[root@weixing01 ~]# systemctl start crond
[root@weixing01 ~]# systemctl restart crond

5.查看服務是否開機啟動:

[root@weixing01 ~]# systemctl is-enabled crond
enabled
[root@weixing01 ~]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@weixing01 ~]# systemctl is-enabled crond
disabled

unit

技術分享圖片

1.七個運行級別:

[root@weixing01 system]# ls -l runlevel*
lrwxrwxrwx. 1 root root 15 11月 15 02:02 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 11月 15 02:02 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 11月 15 02:02 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 11月 15 02:02 runlevel6.target -> reboot.target

技術分享圖片

target

技術分享圖片

1.查找target服務:

[root@weixing01 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  

2.查看指定target下面有哪些unit:

[root@weixing01 system]# systemctl list-dependencies multi-user.target 
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rsyslog.service
● ├─sshd.service
● ├─sysstat.service
● ├─systemd-ask-password-wall.path
● ├─systemd-logind.service
● ├─systemd-readahead-collect.service
● ├─systemd-readahead-replay.service
● ├─systemd-update-utmp-runlevel.service
● ├─systemd-user-sessions.service
● ├─tuned.service
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
lines 1-28

3.查看系統默認target:

[root@weixing01 system]# systemctl  get-default 
multi-user.target

4.設置開機啟動target:

[root@weixing01 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.

5.一個service屬於一種類型unit,多個unit組成target,一個target裏面包含多個service。
6.查看一個service屬於哪個target:看install部分

[root@weixing01 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=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

cron,chkconfig,systemd,unit,target介紹