Linux系统任务计划及系统服务管理

1. Linux系统的任务计划

Linux任务计划都是通过crontab命令来完成的,其常用的选项有:

-u:表示指定用户,不加-u选项则为当前用户;

-e:表示指定计划任务;

-l:表示列出计划任务;

-r:表示删除计划任务;

任务计划的配置文件在/etc/crontab下;

[root@yuioplvlinux-128 ~]# 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

从左到右依次为:分(0-59)、时(0-23)、日(1-31)、月(1-12)、周(0-6);

使用命令“crontab -e”来编写任务计划,输入以下内容:

01 10 05 06 * echo "ok" > /root/cron.log

表示在6月5日这一天的10点01分执行命令:echo "ok" > /root/cron.log;

也可以按时间范围指定任务计划;

01 10 1-10 */2 2,5 echo "ok" > /root/cron.log
表示在2、4、6、8、10、12月的1到10日的周二和周五的10点01分执行命令:echo "ok" > /root/cron.log;

制定计划任务后,需要启动服务,使用命令“systemctl start crond”;

查看服务启动的命令如下:

[root@yuioplvlinux-128 ~]# ps aux | grep cron
root       574  0.0  0.1 126260  1636 ?        Ss   11:45   0:00 /usr/sbin/crond -n
root      1296  0.0  0.0 112676   980 pts/0    R+   12:19   0:00 grep --color=auto cron

若是有这两个进程,说明服务已经启动;

也可以使用如下命令查看:

[root@yuioplvlinux-128 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 六 2018-05-12 11:45:33 CST; 34min ago
 Main PID: 574 (crond)
   CGroup: /system.slice/crond.service
           └─574 /usr/sbin/crond -n

5月 12 11:45:33 yuioplvlinux-128 systemd[1]: Started Command Scheduler.
5月 12 11:45:33 yuioplvlinux-128 systemd[1]: Starting Command Scheduler...
5月 12 11:45:33 yuioplvlinux-128 crond[574]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 94% if used.)
5月 12 11:45:34 yuioplvlinux-128 crond[574]: (CRON) INFO (running with inotify support)

看第3行的‘active’是否为‘(running)’状态,是的话说明服务也是启动状态。

命令“crontab -e”实际上是打开了/var/spool/cron/username文件(如果用户为root,则打开的是/var/spool/cron/root),打开这个文件使用了vim编辑器,保存的时候输入‘:wq’即可,但是不能直接去编辑该文件

[root@yuioplvlinux-128 ~]# crontab -l
01 10 05 06 * echo "ok" > /root/cron.log
[root@yuioplvlinux-128 ~]# crontab -r
[root@yuioplvlinux-128 ~]# crontab -l
no crontab for root

删除任务计划使用命令“crontab -r”,但是这个删除选项要慎用,因为它会删除所有的计划,如果只想删除一条计划,使用-e选项进入编辑删除即可。

2. Linux系统服务管理

2.1 chkconfig服务管理工具

CentOS6上的服务管理管理工具为chkconfig,Linux系统所有的预设服务器都可以通过查看/etc/init.d目录得到;

[root@yuioplvlinux-128 ~]# ls /etc/init.d
functions  netconsole  network  README

因为CentOS7不在延续CentOS6的服务版本管理方案了,但是还可以接着使用chkconfig这个命令;

可以使用命令“chkconfig --list”列出所有的服务及其每个级别的开启状态;

[root@yuioplvlinux-128 ~]# 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:关

可以看到一个提示,它提示该命令输出的内容并没有包含CentOS7原生systemd服务,而这里仅仅列出SysV服务,也就是说,在早期的CentOS版本(CentOS7之前)采用的服务管理都是SysV,而CentOS7换成了systemd。

这里的级别(数字0-6)为系统启动级别,运行级别0、1和6被系统保留。其中,0作为shutdown动作,1作为重启值单用户模式,6为重启。

在CentOS系统中,2表示无NFS支持的多用户模式,3表示完全多用户模式(最常用的级别),4保留给用户自定义,5表示图形登录方式。

关闭network服务;

[root@yuioplvlinux-128 ~]# chkconfig network off
[root@yuioplvlinux-128 ~]# 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:关

关闭network的某一个级别;

[root@yuioplvlinux-128 ~]# chkconfig network on   #开启network服务
[root@yuioplvlinux-128 ~]# 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@yuioplvlinux-128 ~]# chkconfig --level 3 network off
[root@yuioplvlinux-128 ~]# 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:关

--level选项后面还可以指定多个级别;

[root@yuioplvlinux-128 ~]# chkconfig --level 45 network off
[root@yuioplvlinux-128 ~]# 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:关

chkconfig可以把某个服务加入到系统服务或删除,使用命令“chkconfig --add”和“chkconfig --del”;

[root@yuioplvlinux-128 ~]# cd /etc/init.d   #切换至/etc.init.d目录
[root@yuioplvlinux-128 init.d]# ls
functions  netconsole  network  README
[root@yuioplvlinux-128 init.d]# cp network 123   #复制network服务,命名为123
[root@yuioplvlinux-128 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@yuioplvlinux-128 init.d]# chkconfig --add 123   #添加123服务
[root@yuioplvlinux-128 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:关
[root@yuioplvlinux-128 init.d]# chkconfig --del 123   #删除123服务
[root@yuioplvlinux-128 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:关

2.2 systemd服务管理

2.2.1 systemd介绍

SysV只能一个一个地启动,而systemd支持多个服务并发启动;

列出系统所有的服务,这里只列出了一部分;

[root@yuioplvlinux-128 init.d]# systemctl list-units --all --type=service
  UNIT                                                  LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service                                        loaded    active   running Security Auditing Service
  brandbot.service                                      loaded    inactive dead    Flexible Branding Service
  chronyd.service                                       loaded    active   running NTP client/server
  cpupower.service                                      loaded    inactive dead    Configure CPU power related settings
  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 daemon
  dracut-shutdown.service                               loaded    inactive dead    Restore /run/initramfs
  ebtables.service                                      loaded    inactive dead    Ethernet Bridge Filtering tables
  emergency.service                                     loaded    inactive dead    Emergency Shell
● exim.service                                          not-found inactive dead    exim.service
  firewalld.service                                     loaded    active   running firewalld - dynamic firewall daemon
  [email protected]                                    loaded    active   running Getty on tty1
  ip6tables.service                                     loaded    inactive dead    IPv6 firewall with ip6tables
● ipset.service                                         not-found inactive dead    ipset.service
  iptables.service                                      loaded    inactive dead    IPv4 firewall with iptables
  irqbalance.service                                    loaded    inactive dead    irqbalance daemon
● kdump.service                                         loaded    failed   failed  Crash recovery kernel arming
  kmod-static-nodes.service                             loaded    active   exited  Create list of required static device nodes for th
● lvm2-activation.service                               not-found inactive dead    lvm2-activation.service
  lvm2-lvmetad.service                                  loaded    active   running LVM2 metadata daemon
  lvm2-lvmpolld.service                                 loaded    inactive dead    LVM2 poll daemon
  lvm2-monitor.service                                  loaded    active   exited  Monitoring of LVM2 mirrors, snapshots etc. using d

常用命令:

 systemctl enable crond.service   #让服务开机启动

systemctl disable crond   #不让开机启动

systemctl status crond   #查看状态

systemctl stop crond   #停止服务

systemctl start crond   #启动服务

systemctl restart crond   #重启服务

systemctl is-enabled crond   #检查服务是否开机启动

启动服务时,会生成一个软链接文件,当服务关闭时,这个软链接文件就会被删除。

[root@yuioplvlinux-128 ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[root@yuioplvlinux-128 ~]# ll /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 5月  12 13:35 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@yuioplvlinux-128 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@yuioplvlinux-128 ~]# ll /etc/systemd/system/multi-user.target.wants/crond.service
ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

2.2.2 unit

服务对应的启动脚本文件在/usr/lib/systemd/system下,这里只列出了一部分;

[root@yuioplvlinux-128 ~]# ls /usr/lib/systemd/system 
arp-ethers.service                      lvm2-lvmetad.service                           suspend.target
auditd.service                          lvm2-lvmetad.socket                            swap.target
[email protected]                         lvm2-lvmpolld.service                          sys-fs-fuse-connections.mount
basic.target                            lvm2-lvmpolld.socket                           sysinit.target
basic.target.wants                      lvm2-monitor.service                           sysinit.target.wants
blk-availability.service                [email protected]                           sys-kernel-config.mount
bluetooth.target                        machine.slice                                  sys-kernel-debug.mount
brandbot.path                           machines.target                                syslog.socket
brandbot.service                        messagebus.service                             syslog.target.wants
[email protected]                  microcode.service                              sysstat.service
[email protected]                    multi-user.target                              systemd-ask-password-console.path
chronyd.service                         multi-user.target.wants                        systemd-ask-password-console.service
chrony-wait.service                     NetworkManager-dispatcher.service              systemd-ask-password-plymouth.path
console-getty.service                   NetworkManager.service                         systemd-ask-password-plymouth.service
console-shell.service                   NetworkManager-wait-online.service             systemd-ask-password-wall.path
[email protected]                network-online.target                          systemd-ask-password-wall.service

这些文件可以归类为以下几种:

1)service:系统服务;

2)target:多个unit组成的组;

3)device:硬件设备;

4)mount:文件系统挂载点;

5)automount:自动挂载点;

6)path:文件或路径;

7)scope:不是由systemd启动的外部进程;

8)slice:进程组;

9)snapshot:systemd快照;

10)socket:进程间通信的套接字;

11)swap:swap文件;

12)timer:定时器;

每种类型的文件都为一个unit,正是这些unit才组成了系统的各个资源;


列出系统正在运行的unit,这里只列出了一部分;

[root@yuioplvlinux-128 system]# systemctl list-units 
  UNIT                                                  LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount                     loaded active waiting   Arbitrary Executable File Formats File System Automou
  sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-block-sr0.device loaded active plugged   VMware_Virtual_IDE_CDRO
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda1.device loaded active plugged   VMware_Virtual_S 1
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda2.device loaded active plugged   VMware_Virtual_S 2
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda-sda3.device loaded active plugged   VMware_Virtual_S 3
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb1.device loaded active plugged   LVM PV iDr0vu-MSJf-jgsB
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb2.device loaded active plugged   LVM PV cVKVN4-uPZz-Jbop
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb-sdb3.device loaded active plugged   LVM PV VKlWDQ-XYmZ-Osam
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb.device loaded active plugged   VMware_Virtual_S
  sys-devices-pci0000:00-0000:00:11.0-0000:02:00.0-usb2-2\x2d2-2\x2d2.1-2\x2d2.1:1.0-bluetooth-hci0-rfkill0.device loaded active plug
  sys-devices-pci0000:00-0000:00:11.0-0000:02:00.0-usb2-2\x2d2-2\x2d2.1-2\x2d2.1:1.0-bluetooth-hci0.device loaded active plugged   /s
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loaded active plugged   82545EM Gigabit Ethernet Controller (Copp
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loaded active plugged   ES1371/ES1373 / Creative Labs CT2518 (A
  sys-devices-pci0000:00-0000:00:11.0-0000:02:05.0-net-ens37.device loaded active plugged   82545EM Gigabit Ethernet Controller (Copp
  sys-devices-platform-floppy.0-block-fd0.device        loaded active plugged   /sys/devices/platform/floppy.0/block/fd0
  sys-devices-platform-serial8250-tty-ttyS2.device      loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS2
  sys-devices-platform-serial8250-tty-ttyS3.device      loaded active plugged   /sys/devices/platform/serial8250/tty/ttyS3
  sys-devices-pnp0-00:06-tty-ttyS0.device               loaded active plugged   /sys/devices/pnp0/00:06/tty/ttyS0
  sys-devices-pnp0-00:07-tty-ttyS1.device               loaded active plugged   /sys/devices/pnp0/00:07/tty/ttyS1
  sys-devices-virtual-block-dm\x2d0.device              loaded active plugged   /sys/devices/virtual/block/dm-0
  sys-module-configfs.device                            loaded active plugged   /sys/module/configfs

其它的常用命令:

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

2.2.3 target

target类似于CentOS里面的启动级别,但target支持多个target同时启动,target其实是多个unit,系统启动就是启动多个unit,为了方便管理,就用target管理这些unit。

查看当前系统所有的target,这里只列出了一部分;

[root@yuioplvlinux-128 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  
initrd-root-fs.target     static  
initrd-switch-root.target static  
initrd.target             static  

查看指定的target下面有哪些unit,这里只列出了一部分;

[root@yuioplvlinux-128 system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.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

查看系统默认的target;

[root@yuioplvlinux-128 ~]# systemctl get-default 
multi-user.target

设置默认级别的target;

[root@yuioplvlinux-128 ~]# 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.

前面讲过,一个service属于一种类型的unit,而多个unit组成了一个target,所有一个target里面包含了多个service;

查看一个service属于哪个target,看[Install]部分。

[root@yuioplvlinux-128 ~]# 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


猜你喜欢

转载自blog.csdn.net/yuioplv/article/details/80290204