八周一次课(3月26日)
10.23 linux任务计划cron
10.24 chkconfig工具
10.25 systemd管理服务
10.26 unit介绍
10.27 target介绍

扩展
1. anacron  10.23-10.27 corn, chkconfig, systemd, unit, target_systemdhttp://blog.csdn.net/strikers1982/article/details/4787226
2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)   10.23-10.27 corn, chkconfig, systemd, unit, target_systemdhttp://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定义启动脚本  10.23-10.27 corn, chkconfig, systemd, unit, target_systemdhttp://www.jb51.net/article/100457.htm


10.23 linux任务计划cron


介绍

10.23-10.27 corn, chkconfig, systemd, unit, target_chkconfig_04

任务计划,在linux上是必不可少的。


任务计划的配置文件

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

解释:

MAILTO=root 发送邮给指定root用户

SHELL=/bin/bash使用的shell

PATH=/sbin:/bin:/usr/sbin:/usr/bin环境变量,使用的命令路径


日期

*****从左到右:

分钟

月(可以英文简写,jan,feb,mar,apr)

周几(0-6,0和7表示星期日,可以英文简写sun,mon,tue,wed,thu,fri,sat)

用户(不写用户名表示root)

最后一行是命令


格式演示

进入任务计划配置文件命令#crontab -e,这操作跟vim用法一样,只是换了方式来打开配置文件,但是不能直接用vi/vim来修改配置文件。

crontab -e

演示示例,凌晨3点去执行任务(执行命令,在日期后面放上即可)。

任务内容为,执行脚本/bin/bash 脚本名字/usr/local/sbin123.sh 

指定输出日志,正确输出到从定向文件/tmp/123.log,错误的输出定义到/tmp/123.log 如果每天都需要操作记录,利用来追加>>

以下参数:

0 3 * * *  /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

范围用法,

分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7

双月(能整除2的月份)的1-10号每周2周5的3点执行

0 3 1-10 */2 2,5  /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

启动crontab服务

[root@centos7-01 ~]# systemctl start crond
[root@centos7-01 ~]# ps ux |grep cron
root       567  0.0  0.1 126236  1616 ?        Ss   16:29   0:00 /usr/sbin/crond -n
root      2211  0.0  0.0 123208   780 ?        Ss   17:01   0:00 /usr/sbin/anacron -s
root      2354  0.0  0.0 112676   980 pts/0    S+   17:19   0:00 grep --color=auto cron

此命令也可以查看crond服务状态

[root@centos7-01 ~]# systemctl status crond

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_05

要保证服务是启动状态,

否则会无效。


注意

如果发现计划不生效

有可能是没有用绝对路径,

键入绝对路径(最稳),也可以在脚本里添加变量,然后后面参数敲入相应参数即可。

每写一个任务计划,都要追加日志(正确错误输出,)。


#crontab -l 列出计划任务

例如,

#crontab -e 添加以下计划任务

1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@centos7-01 ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f


crontab -l可以查看计划任务。

crontab的文件路径/cron/下的文件,就是对应的用户,例如root就以root命名文件。

#cat /var/spool/cron/root 


crontab 

-l 列出任务计划

-e 编辑任务计划

-r 删除任务计划

-u 指定用户



10.24 chkconfig工具


Linux系统服务管理-chkconfig


10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_06

  • chkconfig是一种服务。

  • chkconfig是centos7以前使用的版本。

  • sysv centos7以前的服务管理基制。

  • systmd centos7的服务管理基制。ps进程排第一,init在centos6进程排第一。

chkconfig命令使用

[root@centos701 ~]# 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在centos7下面只剩下这2个服务。

chkconfig服务脚本的目录,MySQL以后也会用到此脚本

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

更改network服务状态,例如off它们

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

再次开启利用on

[root@centos701 ~]# chkconfig network on
[root@centos701 ~]# 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:关

此处数字0-6 是一个运行级别 一共7个级别

  1. 0表示关机 

  2. 1单用户 centos6 及之前的系统版本实用,如果定位至1级别,开机就会自动进入单用户模式。

  3. 2多用户模式,不带图形2比3少了一个nfs服务

  4. 3多用户模式,不带图形

  5. 4保留级别

  6. 5多用户,带图形

  7. 6重启如果定位6级别,启动就会重启。


更改inittab文件,可以更改运行级别。

vi /etc/inittab

10.23-10.27 corn, chkconfig, systemd, unit, target_corn_07

指定某个级别关闭或开启,

[root@centos701 ~]# chkconfig --level 3 network off
[root@centos701 ~]# 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 --level 345 network on此处345 on 表示运行级别的345都开启
[root@centos701 ~]# chkconfig --level 345 network on
[root@centos701 ~]# 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 --add 服务

示例:

[root@centos701 ~]# cd /etc/init.d/
[root@centos701 init.d]# ls
functions  netconsole  network  README
[root@centos701 init.d]# cp network 123
[root@centos701 init.d]# ls
123  functions  netconsole  network  README
[root@centos701 init.d]# chkconfig --add 123
[root@centos701 init.d]# chkconfig --list
123            0:关1:关2:开3:开4:开5:开6:关
netconsole     0:关1:关2:关3:关4:关5:关6:关
network        0:关1:关2:开3:开4:开5:开6:关

文件名无条件要求,但是文件内容有要求,具体参考

vim network

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_08

解释:

shell脚本,

2345启动顺序, 

运行级别启动顺序第十位10位启动 

第90位关闭

描述


删除123服务

chkconfig --del 123



10.25 systemd管理服务


10.23-10.27 corn, chkconfig, systemd, unit, target_corn_09

systemd是centos7服务管理机制


列出所有service,以及描述

#systemctl list-units --all --type=service

10.23-10.27 corn, chkconfig, systemd, unit, target_corn_10


几个常用的服务相关的命令

#让服务开机启动 (.service可以不用加)
systemctl enable crond.service 
#生成软链接文件
#不让开机启动
systemctl disable crond 
#查看状态
systemctl status crond 
#停止服务
systemctl stop crond 
#启动服务
systemctl start crond 
#重启服务
systemctl restart crond
#检查服务是否开机启动
systemctl is-enabled crond




10.26 unit介绍


Linux系统服务管理-systemd

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_11

  • service 系统服务 

  • target 多个unit组成的组

  • 单用户,emygrcym model,centos6 7个运行级别。centos7有类似的级别

  • ls /usr/lib/systemd/system //系统所有unit,分为以下类型

[root@centos701 system]# cd /usr/lib/systemd/system
[root@centos701 system]# ls -l runlevel*
lrwxrwxrwx. 1 root root 15 3月   8 05:05 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 3月   8 05:05 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 3月   8 05:05 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 3月   8 05:05 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 3月   8 05:05 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 3月   8 05:05 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 3月   8 05:05 runlevel6.target -> reboot.target

注释:

上面 runlevel0.target -> poweroff.target中的runlevel10.target是软链接,真正的文件是poweroff.targt

target是由多个unit组成的一个组,而unit又由多个services组成的组。


不用深究以下几点

  •  device 硬件设备

  •  mount 文件系统挂载点

  •  automount 自动挂载点

  •  path 文件或路径

  •  scope 不是由systemd启动的外部进程

  •  slice 进程组

  •  snapshot systemd快照

  •  socket 进程间通信套接字

  •  swap  swap文件

  •  timer 定时器


system的服务 ---> 系统所有unit,分为以下类型

ls /usr/lib/systemd/system
arp-ethers.service                      kmod-static-nodes.service                      sshd-keygen.service
atd.service                             local-fs-pre.target                            sshd.service
auditd.service                          local-fs.target                                sshd@.service
autovt@.service                         local-fs.target.wants                          sshd.socket
basic.target                            machine.slice                                  suspend.target
basic.target.wants                      machines.target                                swap.target
blk-availability.service                memcached.service                              sys-fs-fuse-connections.mount
bluetooth.target                        messagebus.service                             sysinit.target
brandbot.path                           microcode.service                              sysinit.target.wants
brandbot.service                        multi-user.target                              sys-kernel-config.mount
chrony-dn***v@.service                  multi-user.target.wants                        sys-kernel-debug.mount
chrony-dn***v@.timer                    NetworkManager-dispatcher.service              syslog.socket
chronyd.service                         NetworkManager.service                         syslog.target.wants
chrony-wait.service                     NetworkManager-wait-online.service             sysstat.service
cloud-config.service                    network-online.target                          systemd-ask-password-console.path
cloud-config.target                     network-online.target.wants                    systemd-ask-password-console.service
cloud-final.service                     network-pre.target                             systemd-ask-password-plymouth.path
cloud-init-local.service                network.target                                 systemd-ask-password-plymouth.service
cloud-init.service                      nscd.service                                   systemd-ask-password-wall.path
cloud-init-upgrade.service              nscd.socket                                    systemd-ask-password-wall.service
console-getty.service                   nss-lookup.target                              systemd-backlight@.service
console-shell.service                   nss-user-lookup.target                         systemd-binfmt.service
container-getty@.service                ntpdate.service                                systemd-bootchart.service
cpupower.service                        ntpd.service                                   systemd-firstboot.service
crond.service                           paths.target                                   systemd-fsck-root.service
cryptsetup-pre.target                   plymouth-halt.service                          systemd-fsck@.service
cryptsetup.target                       plymouth-kexec.service                         systemd-halt.service
ctrl-alt-del.target                     plymouth-poweroff.service                      systemd-hibernate-resume@.service
dbus-org.freedesktop.hostname1.service  plymouth-quit.service                          systemd-hibernate.service
dbus-org.freedesktop.import1.service    plymouth-quit-wait.service                     systemd-hostnamed.service
dbus-org.freedesktop.locale1.service    plymouth-read-write.service                    systemd-hwdb-update.service
dbus-org.freedesktop.login1.service     plymouth-reboot.service                        systemd-hybrid-sleep.service
dbus-org.freedesktop.machine1.service   plymouth-start.service                         systemd-importd.service
dbus-org.freedesktop.timedate1.service  plymouth-switch-root.service                   systemd-initctl.service
dbus.service                            polkit.service                                 systemd-initctl.socket
dbus.socket                             postfix.service                                systemd-journal-catalog-update.service
dbus.target.wants                       poweroff.target                                systemd-journald.service
debug-shell.service                     poweroff.target.wants                          systemd-journald.socket
default.target                          printer.target                                 systemd-journal-flush.service
default.target.wants                    proc-sys-fs-binfmt_misc.automount              systemd-kexec.service
dev-hugepages.mount                     proc-sys-fs-binfmt_misc.mount                  systemd-localed.service
dev-mqueue.mount                        quotaon.service                                systemd-logind.service
dracut-cmdline.service                  rc-local.service                               systemd-machined.service
dracut-initqueue.service                rdisc.service                                  systemd-machine-id-commit.service
dracut-mount.service                    reboot.target                                  systemd-modules-load.service
dracut-pre-mount.service                reboot.target.wants                            systemd-nspawn@.service
dracut-pre-pivot.service                remote-fs-pre.target                           systemd-poweroff.service
dracut-pre-trigger.service              remote-fs.target                               systemd-quotacheck.service
dracut-pre-udev.service                 rescue.service                                 systemd-random-seed.service
dracut-shutdown.service                 rescue.target                                  systemd-readahead-collect.service
ebtables.service                        rescue.target.wants                            systemd-readahead-done.service
ecs_mq.service                          rhel-autorelabel-mark.service                  systemd-readahead-done.timer
emergency.service                       rhel-autorelabel.service                       systemd-readahead-drop.service
emergency.target                        rhel-configure.service                         systemd-readahead-replay.service
eni.service                             rhel-dmesg.service                             systemd-reboot.service
final.target                            rhel-domainname.service                        systemd-remount-fs.service
firewalld.service                       rhel-import-state.service                      systemd-rfkill@.service
fstrim.service                          rhel-loadmodules.service                       systemd-shutdownd.service
fstrim.timer                            rhel-readonly.service                          systemd-shutdownd.socket
getty@.service                          rpcbind.target                                 systemd-suspend.service
getty.target                            rsyncd.service                                 systemd-sysctl.service
graphical.target                        rsyncd@.service                                systemd-timedated.service
graphical.target.wants                  rsyncd.socket                                  systemd-tmpfiles-clean.service
halt-local.service                      rsyslog.service                                systemd-tmpfiles-clean.timer
halt.target                             runlevel0.target                               systemd-tmpfiles-setup-dev.service
halt.target.wants                       runlevel1.target                               systemd-tmpfiles-setup.service
hibernate.target                        runlevel1.target.wants                         systemd-udevd-control.socket
htcacheclean.service                    runlevel2.target                               systemd-udevd-kernel.socket
httpd.service                           runlevel2.target.wants                         systemd-udevd.service
hybrid-sleep.target                     runlevel3.target                               systemd-udev-settle.service
initrd-cleanup.service                  runlevel3.target.wants                         systemd-udev-trigger.service
initrd-fs.target                        runlevel4.target                               systemd-update-done.service
initrd-parse-etc.service                runlevel4.target.wants                         systemd-update-utmp-runlevel.service
initrd-root-fs.target                   runlevel5.target                               systemd-update-utmp.service
initrd-switch-root.service              runlevel5.target.wants                         systemd-user-sessions.service
initrd-switch-root.target               runlevel6.target                               systemd-vconsole-setup.service
initrd-switch-root.target.wants         selinux-policy-migrate-local-changes@.service  system.slice
initrd.target                           serial-getty@.service                          system-update.target
initrd.target.wants                     shutdown.target                                teamd@.service
initrd-udevadm-cleanup-db.service       shutdown.target.wants                          timers.target
iprdump.service                         sigpwr.target                                  timers.target.wants
iprinit.service                         sleep.target                                   time-sync.target
iprupdate.service                       -.slice                                        tmp.mount
iprutils.target                         slices.target                                  tuned.service
irqbalance.service                      smartcard.target                               umount.target
kdump.service                           sockets.target                                 user.slice
kexec.target                            sockets.target.wants                           wpa_supplicant.service
kexec.target.wants                      sound.target


unit相关的命令

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_12

 systemctl list-units 列出正在运行的unit

 systemctl list-units --all列出所有,包括失败的或者inactive的


 systemctl list-units --all --state=inactive //列出inactive的unit

10.23-10.27 corn, chkconfig, systemd, unit, target_chkconfig_13

 systemctl list-units --type=service 列出状态为active的service

不加--all的话active列会显示全部active和failed(个别)

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_14


 systemctl is-active crond.service 查看某个服务是否为active

[root@centos701 system]#  systemctl is-active crond.service

active



10.27 target介绍

介绍 

10.23-10.27 corn, chkconfig, systemd, unit, target_chkconfig_15

系统为了方便管理用target来管理unit


显示系统所有的target

#systemctl list-unit-files --type=target

10.23-10.27 corn, chkconfig, systemd, unit, target_chkconfig_16

查看指定target下面有哪些unit

#systemctl list-dependencies multi-user.target 

10.23-10.27 corn, chkconfig, systemd, unit, target_systemd_17

大部分都是.service


查看系统默认的target

#systemctl get-default 

[root@centos701 system]# systemctl get-default

multi-user.target


设定默认运行级别。

设定开机运行级别,相当于修改chkconfing的默认开机运行级别。

#systemctl set-default multi-user.target
[root@centos701 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.

 一个service属于一种类型的unit

 多个unit组成了一个target

 一个target里面包含了多个service

 cat /usr/lib/systemd/system/sshd.service //看[install]部分

总结:

系统由多种units组成的,unit是由target去管理,也就是说target由很多unit组成的。