本文索引:
- 计划任务 crond服务/crontab命令
- 常用参数
- cron样例
- 系统服务管理chkconfig
- 基本使用
- 系统服务管理systemd
- unit介绍
- unit相关命令
- target介绍
- 小结
任务计划 crontab
在某个时间执行某些命令或脚本,做到自动化运维
crond服务配置文件
[root@localhost ~]# cat /etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=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 -lno 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 -l0 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 -l0 8 * * * echo "good morning"默认不指定-u,设置或显示的是当前用户
- -r 删除用户的计划任务表(配合-u指定用户)
[root@localhost ~]# crontab -r -u user1[root@localhost ~]# crontab -u user1 -lno 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 virtuaLOAD = Reflects whether the unit definition was properly loaded.ACTIVE = The high-level unit activation state, i.e. generalization of SUBSUB = 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.serviceCreated 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 SchedulerAfter=auditd.service systemd-user-sessions.service time-sync.target[Service]EnvironmentFile=/etc/sysconfig/crondExecStart=/usr/sbin/crond -n $CRONDARGSExecReload=/bin/kill -HUP $MAINPIDKillMode=process[Install]WantedBy=multi-user.target# 设置开机不启动,系统移除服务脚本的软链接[root@localhost ~]# systemctl disable crondRemoved 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 -n10月 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 crondenabled
unit介绍
列出系统内所有unit
[root@localhost ~]# ls /usr/lib/systemd/systemarp-ethers.serviceauditd.serviceautovt@.servicebasic.targetbasic.target.wantsblk-availability.servicebluetooth.targetbrandbot.pathbrandbot.servicechrony-dnssrv@.servicechrony-dnssrv@.timerchronyd.servicechrony-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 configurlines 1-16[root@localhost system]# systemctl list-units --state=active
- 查看服务是否启动
[root@localhost system]# systemctl is-active crond.serviceactive
target介绍
为了方便管理用target来管理unit,一个target是多个unit的组合
- 列出系统内所有的target
[root@localhost system]# systemctl list-unit-files --type=targetUNIT FILE STATE basic.target static bluetooth.target static cryptsetup-pre.target static cryptsetup.target static ctrl-alt-del.target disableddefault.target enabled emergency.target static final.target static getty.target static graphical.target static halt.target disabledhibernate.target static hybrid-sleep.target static initrd-fs.target static ...
- 查看指定target下面的units组成
target下还可以有target
[root@localhost system]# systemctl list-dependencies multi-user.targetmulti-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.targetRemoved 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*.targetlrwxrwxrwx. 1 root root 15 10月 18 02:32 runlevel0.target -> poweroff.targetlrwxrwxrwx. 1 root root 13 10月 18 02:32 runlevel1.target -> rescue.targetlrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel2.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel3.target -> multi-user.targetlrwxrwxrwx. 1 root root 17 10月 18 02:32 runlevel4.target -> multi-user.targetlrwxrwxrwx. 1 root root 16 10月 18 02:32 runlevel5.target -> graphical.targetlrwxrwxrwx. 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 daemonDocumentation=man:sshd(8) man:sshd_config(5)After=network.target sshd-keygen.serviceWants=sshd-keygen.service[Service]Type=forkingPIDFile=/var/run/sshd.pidEnvironmentFile=/etc/sysconfig/sshdExecStart=/usr/sbin/sshd $OPTIONSExecReload=/bin/kill -HUP $MAINPIDKillMode=processRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target