计划任务crond、系统服务管理工具chkconfig/systemctl

本文索引:

  • 计划任务 crond服务/crontab命令
    • 常用参数
    • cron样例
  • 系统服务管理chkconfig
    • 基本使用
  • 系统服务管理systemd
    • unit介绍
    • unit相关命令
    • target介绍
    • 小结

任务计划 crontab

在某个时间执行某些命令或脚本,做到自动化运维

crond服务配置文件

[root@localhost ~]# 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
常用参数
  • -l 列出计划任务表
# 初始为设置,计划任务表为空
[root@localhost ~]# crontab -l
no crontab for root
  • -e 编辑计划任务表
# 编辑
[root@localhost ~]# crontab -e
# 进入编辑模式,按i键后输入
0 3 * * 1-5 /usr/bin/tar -cvf /home/user1 >> user.log 2>&1

按esc退出编辑模式,:wq保存退出

# 查看验证
[root@localhost ~]# crontab -l
0 3 * * 1-5 /usr/bin/tar -cvf /home/user1 >> user.log 2>&1

  • -u USER (可以配合其他参数使用)
[root@localhost ~]# crontab -u user1 -e

[root@localhost ~]# crontab -u user1 -l
0 8 * * * echo "good morning"

默认不指定-u,设置或显示的是当前用户
  • -r 删除用户的计划任务表(配合-u指定用户)
[root@localhost ~]# crontab -r -u user1
[root@localhost ~]# crontab -u user1 -l
no crontab for user1
cron样例
[root@localhost ~]# man 5 crontab
...
EXAMPLE CRON FILE
       # use /bin/sh to run commands, no matter what /etc/passwd says
       SHELL=/bin/sh
       # mail any output to `paul', no matter whose crontab this is
       MAILTO=paul
       #
       CRON_TZ=Japan
       # run five minutes after midnight, every day
       5 0 * * *       $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
       # run at 2:15pm on the first of every month -- output mailed to paul
       15 14 1 * *     $HOME/bin/monthly
       # run at 10 pm on weekdays, annoy Joe
       0 22 * * 1-5    mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
       23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
       5 4 * * sun     echo "run at 5 after 4 every sunday"
...

系统服务管理chkconfig

centos6及之前版本使用

在centos7中已经不再使用chkconfig来进行系统服务的管理,改为systemctl命令了,所以只显示了部分服务

基本使用
  • 显示管理的服务
[root@localhost ~]# 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@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# 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,4,5级应该写成345(中间没有空格和逗号)
[root@localhost ~]# chkconfig --level 345  network off
[root@localhost ~]# 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:关

  • 向管理列表添加服务
# 将新服务的启动脚本加入到/etc/init.d/目录下(这里就简单拷贝下做个示范)
[root@localhost ~]# cp /etc/init.d/network /etc/init.d/ssh

# 添加新服务至管理列表
[root@localhost ~]# chkconfig --add ssh
[root@localhost ~]# 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:关
ssh            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
  • 删除管理列表中的服务
[root@localhost etc]# chkconfig --del ssh
[root@localhost etc]# 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(centos7)

[root@localhost ~]# systemctl list-units --all --type=service
  UNIT                      LOAD      ACTIVE   SUB     DESCRIPTION
  auditd.service            loaded    active   running Security Auditing 
  brandbot.service          loaded    inactive dead    Flexible Branding 
  chronyd.service           loaded    active   running NTP client/server
  cpupower.service          loaded    inactive dead    Configure CPU powe
  crond.service             loaded    active   running Command Scheduler
  dbus.service              loaded    active   running D-Bus System Messa
● display-manager.service   not-found inactive dead    display-manager.se
  dracut-shutdown.service   loaded    inactive dead    Restore /run/initr
  ebtables.service          loaded    inactive dead    Ethernet Bridge Fi
  emergency.service         loaded    inactive dead    Emergency Shell
  ...
  systemd-vconsole-setup.service loaded    active   exited  Setup Virtual
  tuned.service             loaded    active   running Dynamic System Tun
  vmtoolsd.service          loaded    active   running Service for virtua

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB
SUB    = The low-level unit activation state, values depend on unit type.

83 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 76-91/91 (END)

# 不加--all,只列出active状态的服务
[root@localhost ~]# systemctl list-units --type=service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service            loaded active running Security Auditing Servi
  chronyd.service           loaded active running NTP client/server
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message Bu
  firewalld.service         loaded active running firewalld - dynamic fir
  getty@tty1.service        loaded active running Getty on tty1
...
  • 设置服务开机启动/不启动
# 开机启动,服务后可加可不加.service,系统会创建服务脚本文件的软链接
[root@localhost ~]# systemctl enable crond
[root@localhost ~]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

# crond.service脚本内容如下
[root@localhost ~]# cat /usr/lib/systemd/system/crond.service 
[Unit]
Description=Command Scheduler
After=auditd.service systemd-user-sessions.service time-sync.target

[Service]
EnvironmentFile=/etc/sysconfig/crond
ExecStart=/usr/sbin/crond -n $CRONDARGS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process

[Install]
WantedBy=multi-user.target


# 设置开机不启动,系统移除服务脚本的软链接
[root@localhost ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

  • 查看服务状态
[root@localhost ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 四 2017-10-26 17:51:07 CST; 2h 28min ago
 Main PID: 525 (crond)
   CGroup: /system.slice/crond.service
           └─525 /usr/sbin/crond -n

10月 26 17:51:07 localhost.localdomain systemd[1]: Started Command Sc...
10月 26 17:51:07 localhost.localdomain systemd[1]: Starting Command S...
10月 26 17:51:08 localhost.localdomain crond[525]: (CRON) INFO (RANDO...
10月 26 17:51:10 localhost.localdomain crond[525]: (CRON) INFO (runni...
Hint: Some lines were ellipsized, use -l to show in full.
  • 停止服务
[root@localhost ~]# systemctl stop crond
  • 启动服务
[root@localhost ~]# systemctl start crond
  • 重启服务
[root@localhost ~]# systemctl restart crond
  • 检查服务是否开机启动
[root@localhost ~]# systemctl is-enabled crond
enabled
unit介绍

列出系统内所有unit

[root@localhost ~]# ls /usr/lib/systemd/system
arp-ethers.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service
chrony-dnssrv@.timer
chronyd.service
chrony-wait.service
...

unit分为以下类型 类型 | 说明 | 举例 --- | --- | --- service | 系统服务 | crond.service target | 多个unit组成的组 | timers.target device | 硬件设备 | mount | 文件系统挂载点 | dev-mqueue.mount automount | 自动挂载点 | path | 文件或路径 | systemd-ask-password-console.path scope | 不是由systemd启动的外部进程 | slice | 进程组 | system.slice snapshot | systemd快照 | socket | 进程间通信套接字 | systemd-shutdownd.socket swap | swap文件 | timer | 定时器 | systemd-readahead-done.timer

unit相关命令
  • 只显示active状态的unit;加上--all参数全部显示
[root@localhost system]# systemctl list-units
  UNIT                      LOAD   ACTIVE SUB       DESCRIPTION
  proc-sys-fs-binfmt_misc.automount loaded active waiting   Arbitrary Exe
  sys-devices-pci0000:00-0000:00:07.1-ata2-host2-target2:0:0-2:0:0:0-bloc
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:0-0:0:0:0-block-sda
  sys-devices-pci0000:00-0000:00:10.0-host0-target0:0:1-0:0:1:0-block-sdb
  sys-devices-pci0000:00-0000:00:11.0-0000:02:01.0-net-ens33.device loade
  sys-devices-pci0000:00-0000:00:11.0-0000:02:02.0-sound-card0.device loa
  sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged 
  sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged 
  sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged 
  sys-devices-pnp0-00:05-tty-ttyS0.device loaded active plugged   /sys/de
  sys-module-configfs.device loaded active plugged   /sys/module/configfs
  sys-subsystem-net-devices-ens33.device loaded active plugged   82545EM 
lines 1-16
  • 只显示状态为inactive的unit(配合--type=service再次只刷选出服务)
[root@localhost system]# systemctl list-units --all --state=inactive
  UNIT                      LOAD      ACTIVE   SUB  DESCRIPTION
  proc-sys-fs-binfmt_misc.mount loaded    inactive dead Arbitrary Executa
  sys-fs-fuse-connections.mount loaded    inactive dead FUSE Control File
  tmp.mount                 loaded    inactive dead Temporary Directory
  systemd-ask-password-console.path loaded    inactive dead Dispatch Pass
  brandbot.service          loaded    inactive dead Flexible Branding Ser
  cpupower.service          loaded    inactive dead Configure CPU power r
● display-manager.service   not-found inactive dead display-manager.servi
  dracut-shutdown.service   loaded    inactive dead Restore /run/initramf
  ebtables.service          loaded    inactive dead Ethernet Bridge Filte
  emergency.service         loaded    inactive dead Emergency Shell
● exim.service              not-found inactive dead exim.service

  • 只显示状态为active的服务
[root@localhost system]# systemctl list-units --type=service
  UNIT                      LOAD   ACTIVE SUB     DESCRIPTION
  auditd.service            loaded active running Security Auditing Servi
  chronyd.service           loaded active running NTP client/server
  crond.service             loaded active running Command Scheduler
  dbus.service              loaded active running D-Bus System Message Bu
  firewalld.service         loaded active running firewalld - dynamic fir
  getty@tty1.service        loaded active running Getty on tty1
● kdump.service             loaded failed failed  Crash recovery kernel a
  kmod-static-nodes.service loaded active exited  Create list of required
  network.service           loaded active exited  LSB: Bring up/down netw
  NetworkManager-wait-online.service loaded active exited  Network Manage
  NetworkManager.service    loaded active running Network Manager
  polkit.service            loaded active running Authorization Manager
  postfix.service           loaded active running Postfix Mail Transport 
  rhel-dmesg.service        loaded active exited  Dump dmesg to /var/log/
  rhel-import-state.service loaded active exited  Import network configur
lines 1-16

[root@localhost system]# systemctl list-units --state=active
  • 查看服务是否启动
[root@localhost system]# systemctl is-active crond.service
active
target介绍

为了方便管理用target来管理unit,一个target是多个unit的组合

  • 列出系统内所有的target
[root@localhost 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  
...
  • 查看指定target下面的units组成

target下还可以有target

[root@localhost system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─irqbalance.service
● ├─kdump.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
...
  • 查看系统默认的target
[root@localhost system]# systemctl get-default 
multi-user.targe
  • 设置系统默认的target(类似于centos6内的修改系统运行级别
[root@localhost 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.

CentOS6中在 /etc/inittab 文件中可以修改系统的运行级别,而在CentOS7中使用了类似的target来管理运行级

# /usr/lib/systemd/system目录下
[root@localhost system]# ls -l runlevel*.target
lrwxrwxrwx. 1 root root 15 10月 18 02:32 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 10月 18 02:32 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 10月 18 02:32 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 10月 18 02:32 runlevel6.target -> reboot.target
小结
  • 一个service属于一种类型的unit
  • 同类型的多个unit组成一个target
  • 一个target内包含多个service

查看service属于哪个target(看Install块)

[root@localhost 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=forking
PIDFile=/var/run/sshd.pid
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

转载于:https://my.oschina.net/LuCastiel/blog/1584784

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值