10.23 linux任务计划cron
linux系统下,任务计划是必不可少的。
比如我们希望给系统备份,执行某一个任务,任务计划会帮助我们省去定时敲命令的工作。
[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
以上是cron的配置文件,他定义了几个变量,SHELL、PATH、MAILTO 注释里面是五个位置。
更改配置文件,可以使用命令crontab -e,用法和vim相同,意义与visudo类似,就是不希望你直接更改配置文件
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/234.log
这个表示每天的3点钟,以默认root的身份,执行命令。*表示任意数字。
0 3 1-10 */2 2,5 COMMAND
这个表示双数月的一号到十号中的周二或者周五执行命令。
启动服务
[root@localhost: ~]# systemctl start crond
[root@localhost: ~]# ps aux | grep crond
root 639 0.0 0.0 126280 1628 ? Ss 11:59 0:00 /usr/sbin/crond -n
root 2904 0.0 0.0 112704 972 pts/0 S+ 16:41 0:00 grep --color=auto crond
[root@localhost: ~]# systemctl status crond.service
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2018-07-17 11:59:19 CST; 4h 42min ago
Main PID: 639 (crond)
CGroup: /system.slice/crond.service
└─639 /usr/sbin/crond -n
Jul 17 11:59:19 localhost systemd[1]: Started Command Scheduler.
Jul 17 11:59:19 localhost systemd[1]: Starting Command Scheduler...
Jul 17 11:59:19 localhost crond[639]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 32% if used.)
Jul 17 11:59:19 localhost crond[639]: (CRON) INFO (running with inotify support)
任务计划没有执行,而任务计划crond已经启动了,那么大多数原因是没有在crontab的的PATH里面没有包括你的命令,或者没有写绝对路径。
任务最好写输出日志,检验任务是否执行。
[root@localhost: ~]# crontab -l
[root@localhost: ~]# crontab -e
crontab: installing new crontab
[root@localhost: ~]# crontab -l
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +30 | xargs rm -f
下述目录下就是 以用户名命名的文件。保存的就是某个用户的任务计划。
[root@localhost: ~]# ls /var/spool/cron/
root
-r删除cron计划,-u可以指定用户。
10.24 chkconfig工具
既然我们有那么多服务,我们就需要有一个服务管理工具,管理某些服务的启动方式,时间,级别。
[root@localhost: ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
chkconfig工具就是一个服务管理器,它的管理机制是SysV,7之后的版本用的是systemd。
这里只列出了SysV管理的服务。
top可以查看到7之后的pid=1的进程就是systemd;而6的第一个进程是init。
chkconfig管理的服务的脚本就在这里。
[root@localhost: ~]# ls /etc/init.d/
functions netconsole network README
上述--list列出的服务,表示的是某级别的开机后的状态是on/off。7之后基本不适用了。
[root@localhost: ~]# chkconfig network off
[root@localhost: ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:off 3:off 4:off 5:off 6:off
[root@localhost: ~]# chkconfig network on
[root@localhost: ~]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
也可以单独给某个运行级别on/off。
[root@localhost: ~]# chkconfig network --level 345 on
我们也可以添加自定义服务。
[root@localhost: ~]# cd /etc/init.d/
[root@localhost: init.d]# ls
functions netconsole network README
[root@localhost: init.d]# cp network 123
[root@localhost: init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost: init.d]# chkconfig --add 123
[root@localhost: init.d]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
123 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
一定要把shell脚本cp到目录,然后名字没有意义,但是内容重要。
#! /bin/bash
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90
# description: Activates/Deactivates all network interfaces configured to \
# start at boot time.
#
如果自定义服务没有chkconfig的描述,是不能被支持的。
[root@localhost: init.d]# chkconfig --add 123
service 123 does not support chkconfig
10.25systemd管理服务
这个是centos7的服务管理机制。
[root@localhost: init.d]# systemctl list-unit-files
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
systemd-ask-password-console.path static
systemd-ask-password-plymouth.path static
systemd-ask-password-wall.path static
session-2.scope static
abrt-ccpp.service enabled
abrt-oops.service enabled
这个会列出所有的服务以及各种各样的unit
如果只想列出service服务的话,可以使用这个命令
[root@localhost: init.d]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
123.service loaded inactive dead (null)
abrt-ccpp.service loaded active exited Install ABRT coredump hook
abrt-oops.service loaded active running ABRT kernel log watcher
abrt-vmcore.service loaded inactive dead Harvest vmcores for ABRT
abrt-xorg.service loaded inactive dead ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
atd.service loaded active running Job spooling tools
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
dmraid-activation.service loaded inactive dead Activation of DM RAID sets
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 active running irqbalance daemon
kdump.service loaded active exited Crash recovery kernel arming
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
libstoragemgmt.service loaded active running libstoragemgmt plug-in server daemon
● livesys.service not-found inactive dead livesys.service
● lvm2-activation-early.service not-found inactive dead lvm2-activation-early.service
● 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 dmeventd or progress polling
lvm2-pvscan@8:17.service loaded active exited LVM2 PV scan on device 8:17
lvm2-pvscan@8:18.service loaded active exited LVM2 PV scan on device 8:18
lvm2-pvscan@8:19.service loaded active exited LVM2 PV scan on device 8:19
lvm2-pvscan@8:21.service loaded active exited LVM2 PV scan on device 8:21
mdmonitor.service loaded inactive dead Software RAID monitoring and management
microcode.service loaded inactive dead Load CPU microcode update
network.service loaded active exited LSB: Bring up/down networking
● NetworkManager-wait-online.service loaded failed failed Network Manager Wait Online
NetworkManager.service loaded active running Network Manager
● ntpd.service not-found inactive dead ntpd.service
ntpdate.service loaded inactive dead Set time via NTP
plymouth-quit-wait.service loaded inactive dead Wait for Plymouth Boot Screen to Quit
plymouth-quit.service loaded inactive dead Terminate Plymouth Boot Screen
plymouth-read-write.service loaded inactive dead Tell Plymouth To Write Out Runtime Data
plymouth-start.service loaded inactive dead Show Plymouth Boot Screen
polkit.service loaded active running Authorization Manager
postfix.service loaded active running Postfix Mail Transport Agent
rc-local.service loaded inactive dead /etc/rc.d/rc.local Compatibility
rescue.service loaded inactive dead Rescue Shell
rhel-autorelabel-mark.service loaded inactive dead Mark the need to relabel after reboot
rhel-autorelabel.service loaded inactive dead Relabel all filesystems, if necessary
rhel-configure.service loaded inactive dead Reconfigure the system on administrator request
rhel-dmesg.service loaded active exited Dump dmesg to /var/log/dmesg
rhel-import-state.service loaded active exited Import network configuration from initramfs
rhel-loadmodules.service loaded inactive dead Load legacy module configuration
rhel-readonly.service loaded active exited Configure read-only root support
rngd.service loaded active running Hardware RNG Entropy Gatherer Daemon
rpcbind.service loaded active running RPC bind service
rsyslog.service loaded active running System Logging Service
[email protected] loaded inactive dead Migrate local SELinux policy changes from the old store structure to the new structur
● sendmail.service not-found inactive dead sendmail.service
smartd.service loaded active running Self Monitoring and Reporting Technology (SMART) Daemon
● sntp.service not-found inactive dead sntp.service
sshd-keygen.service loaded inactive dead OpenSSH Server Key Generation
sshd.service loaded active running OpenSSH server daemon
● syslog.service not-found inactive dead syslog.service
sysstat.service loaded active exited Resets System Activity Logs
systemd-ask-password-console.service loaded inactive dead Dispatch Password Requests to Console
systemd-ask-password-plymouth.service loaded inactive dead Forward Password Requests to Plymouth
systemd-ask-password-wall.service loaded inactive dead Forward Password Requests to Wall
systemd-binfmt.service loaded inactive dead Set Up Additional Binary Formats
systemd-firstboot.service loaded inactive dead First Boot Wizard
systemd-fsck-root.service loaded inactive dead File System Check on Root Device
systemd-hwdb-update.service loaded inactive dead Rebuild Hardware Database
systemd-initctl.service loaded inactive dead /dev/initctl Compatibility Daemon
systemd-journal-catalog-update.service loaded inactive dead Rebuild Journal Catalog
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-machine-id-commit.service loaded inactive dead Commit a transient machine-id on disk
systemd-modules-load.service loaded inactive dead Load Kernel Modules
● systemd-random-seed-load.service not-found inactive dead systemd-random-seed-load.service
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-readahead-collect.service loaded inactive dead Collect Read-Ahead Data
systemd-readahead-done.service loaded inactive dead Stop Read-Ahead Data Collection
systemd-readahead-replay.service loaded inactive dead Replay Read-Ahead Data
systemd-reboot.service loaded inactive dead Reboot
systemd-remount-fs.service loaded active exited Remount Root and Kernel File Systems
systemd-shutdownd.service loaded inactive dead Delayed Shutdown Service
systemd-sysctl.service loaded active exited Apply Kernel Variables
● systemd-sysusers.service not-found inactive dead systemd-sysusers.service
● systemd-timesyncd.service not-found inactive dead systemd-timesyncd.service
systemd-tmpfiles-clean.service loaded inactive dead Cleanup of Temporary Directories
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-settle.service loaded active exited udev Wait for Complete Device Initialization
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-done.service loaded inactive dead Update is Completed
systemd-update-utmp-runlevel.service loaded inactive dead Update UTMP about System Runlevel Changes
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
tuned.service loaded active running Dynamic System Tuning Daemon
vdo.service loaded active exited VDO volume services
vgauthd.service loaded active running VGAuth Service for open-vm-tools
vmtoolsd.service loaded active running Service for virtual machines hosted on VMware
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.
112 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.
lines 80-120/120 (END)
如果不加--all选项,就不会列出inactive状态的服务。
systemctl enable crond.service //开机启动
systemctl disable crond.service //开机不启动
systemctl status crond.service //服务状态
systemctl stop crond.service //停止服务
systemctl start crond.service //启动服务
systemctl restart crond.service //重启服务
systemctl is-enabled crond.service //是否开机启动
其实服务是否开机启动查看的是一个文件/etc/systemd/system/multi-user.target.wants/crond.service
[root@localhost: init.d]# systemctl disable crond.service
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@localhost: init.d]# systemctl enable crond.service
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
开机启动的服务如下
[root@localhost: init.d]# ls /etc/systemd/system/multi-user.target.wants/
abrt-ccpp.service abrt-xorg.service crond.service libstoragemgmt.service remote-fs.target smartd.service vdo.service
abrtd.service atd.service firewalld.service mdmonitor.service rngd.service sshd.service vmtoolsd.service
abrt-oops.service auditd.service irqbalance.service NetworkManager.service rpcbind.service sysstat.service
abrt-vmcore.service chronyd.service kdump.service postfix.service rsyslog.service tuned.service
10.26 unit介绍
[root@localhost: ~]# ll /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 Jul 17 21:07 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
可以看到服务的源文件存在于目录/usr/lib/systemd/system中,这个目录下有很多service、path、target文件以及wants目录。
这些都叫做unit,用上一节的查看所有unit的命令即可查到。
/usr/lib/systemd/system下所有unit:
- service 系统服务
- target 多个unit组成的组
- device 硬件设备
- mount 文件系统挂载点
- automount 自动挂载点
- path 文件或路径
- scope 不是由systemd启动的外部进程
- slice 进程组
- snapshot systemd快照
- socket 进程间通信套接字
- swap swap文件
- timer定时器
比如centos7里有这么几个target对应centos6的七个启动级别
[root@localhost: system]# ls -l runlevel*.target
lrwxrwxrwx. 1 root root 15 Jun 27 03:19 runlevel0.target -> poweroff.target
lrwxrwxrwx. 1 root root 13 Jun 27 03:19 runlevel1.target -> rescue.target
lrwxrwxrwx. 1 root root 17 Jun 27 03:19 runlevel2.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jun 27 03:19 runlevel3.target -> multi-user.target
lrwxrwxrwx. 1 root root 17 Jun 27 03:19 runlevel4.target -> multi-user.target
lrwxrwxrwx. 1 root root 16 Jun 27 03:19 runlevel5.target -> graphical.target
lrwxrwxrwx. 1 root root 13 Jun 27 03:19 runlevel6.target -> reboot.target
unit相关命令:
- systemctl list-units #列出正在运行的unit
- systemctl list-units --all #列出所有的unit,包括状态为inactive
- systemctl list-units --all --state=inactive #列出inactive的unit
- systemctl list-units --type=service #列出状态为active和failed的service
- systemctl is-active crond.service #检查某个服务是否为active
10.27 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-pre.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
iprutils.target disabled
kexec.target disabled
local-fs-pre.target static
local-fs.target static
machines.target disabled
multi-user.target enabled
network-online.target static
network-pre.target static
network.target static
nss-lookup.target static
nss-user-lookup.target static
paths.target static
poweroff.target disabled
printer.target static
reboot.target disabled
remote-cryptsetup.target disabled
remote-fs-pre.target static
remote-fs.target enabled
rescue.target disabled
rpcbind.target static
runlevel0.target disabled
runlevel1.target disabled
查看某个target下有什么unit
[root@localhost: system]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─123.service
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─dbus.service
● ├─firewalld.service
● ├─irqbalance.service
● ├─kdump.service
● ├─libstoragemgmt.service
● ├─mdmonitor.service
● ├─network.service
● ├─NetworkManager.service
● ├─plymouth-quit-wait.service
● ├─plymouth-quit.service
● ├─postfix.service
● ├─rngd.service
● ├─rpcbind.service
● ├─rsyslog.service
● ├─smartd.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
● ├─vdo.service
● ├─vmtoolsd.service
● ├─basic.target
● │ ├─microcode.service
● │ ├─rhel-autorelabel-mark.service
左边的圆点是有颜色的。
[root@localhost: init.d]# systemctl get-default
multi-user.target
以上为默认的target。
centos7更改这个默认的target即可达到centos6更改配置文件/etc/inittab文件更改系统运行级的效果。
设置默认target:
[root@localhost: init.d]# 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。
[root@localhost: init.d]# cd -
/usr/lib/systemd/system
[root@localhost: system]# cat sshd.s
sshd.service sshd.socket
[root@localhost: system]# cat 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
只需要看Install一栏即可。