Linux系统的任务计划
10.23 任务计划命令cron
1.cat /etc/crontab,查看到的数字左到右依次为:分、时、日、月、周和命令行
[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
2.用crontab -e来编写任务计划,示例每天凌晨三点执行命令
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
3.分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7;可用格式1-5表示一个范围1到5;可用格式1,2,3表示1或者2或者3; 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
4.要保证服务是启动状态-systemctl start crond
[root@localhost ~]# systemctl start crond
[root@localhost ~]# ps aux |grep cron
root 903 0.0 0.0 126264 1636 ? Ss 15:16 0:00 /usr/sbin/crond -n
root 6761 0.0 0.0 112676 976 pts/0 S+ 20:23 0:00 grep --color=auto cron
4.1 ps aux |grep cron查看是否启动
4.2 systemctl status crond 方法二
[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 一 2018-02-26 10:27:29 CST; 9h ago
Main PID: 903 (crond)
CGroup: /system.slice/crond.service
└─903 /usr/sbin/crond -n
4.3 如何停止计划 -systemctl stop crond
5.任务计划未执行的情况
查看crontab -e中是否为写明命令的绝对路径,而是直接写了command, 同时把正确和错误的执行过程记录在日志文件中,==方便后续查找??是否说的这个==
6.查看已经设定的任务计划crontab -l
[root@localhost ~]# crontab -l
0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
7.crontab-r删除任务计划
[root@localhost ~]# crontab -r
[root@localhost ~]# crontab -l
no crontab for root
8.crontab -u 指定用户
[root@localhost ~]# crontab -u root -l
no crontab for root
10.24 chkconfig系统服务工具(CentOS6)
1.chkconfig --list只剩下netconsole和network两个服务了
[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:关
早期的CentOS6用到的服务管理都是SysV,而7换成了systemd. 输入top,查看:
2.查看Linux所有的预设服务:ls /etc/init.d/
[root@localhost ~]# ls /etc/init.d/
functions netconsole network README
3.chkconfig network off/on
4.回顾这里7个系统启动级别的知识,现在CnetOS7已经不再严格区分,但6之前是有规定的:0即shutdown,1作为重启至单用户,2表示无NFS服务支持的多用户模式,3表示完全多用户模式,但不带图形,4保留用户自定义,5表示图形登入模式,6表示重启
5.更改某个级别下的状态:chkconfig --level 3 network off
[root@localhost ~]# chkconfig --level 3 network off
[root@localhost ~]# chkconfig --list |grep network
[root@localhost ~]# chkconfig --level 345 network off
6.chkconfig可以把某个服务(脚本)加入系统中
[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# ls
functions netconsole network README
增加名称为123的服务
[root@localhost init.d]# cp network 123
[root@localhost init.d]# ls -l
总用量 40
-rwxr-xr-x 1 root root 6643 2月 26 21:05 123
-rw-r--r--. 1 root root 15131 9月 12 2016 functions
-rwxr-xr-x. 1 root root 2989 9月 12 2016 netconsole
-rwxr-xr-x. 1 root root 6643 9月 12 2016 network
-rw-r--r--. 1 root root 1160 5月 26 2017 README
6.1这里我们查看下文件123,vim 123
7.删除某个服务chkconfig --del 123
[root@localhost init.d]# chkconfig --del 123
[root@localhost 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:关
10.25 systemd服务管理
1. 列出系统所有的服务
[root@localhost init.d]# systemctl list-units --all --type=service
UNIT LOAD ACTIVE SUB DESCRIPTION
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 active running ABRT Xorg log watcher
abrtd.service loaded active running ABRT Automated Bug Reporting Tool
accounts-daemon.service loaded inactive dead Accounts Service
alsa-restore.service loaded inactive dead Save/Restore Sound Card State
alsa-state.service loaded active running Manage Sound Card State (restore and store)
● apparmor.service not-found inactive dead apparmor.service
atd.service loaded active running Job spooling tools
auditd.service loaded active running Security Auditing Service
auth-rpcgss-module.service loaded inactive dead Kernel Module supporting RPCSEC_GSS
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
2.几个常用的服务相关的命令
systemctl enable crond.service //让服务开机启动
systemctl disable crond //不让开机启动
systemctl status crond //查看状态
systemctl stop crond //停止服务
systemctl start crond //启动服务
systemctl restart crond //重启服务
systemctl is-enabled crond //检查服务是否开机启动
[root@localhost ~]# systemctl enable crond.service
[root@localhost ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
10.26 unit介绍
1.ls /usr/lib/systemd/system //系统所有unit,分为以下类型
[root@localhost ~]# ls /usr/lib/systemd/system
abrt-ccpp.service irqbalance.service runlevel3.target
abrtd.service iscsid.service runlevel3.target.wants
abrt-oops.service iscsid.socket runlevel4.target
abrt-pstoreoops.service iscsi.service runlevel4.target.wants
abrt-vmcore.service iscsi-shutdown.service runlevel5.target
abrt-xorg.service iscsiuio.service runlevel5.target.wants
accounts-daemon.service iscsiuio.socket runlevel6.target
alsa-restore.service kdump.service saslauthd.service
alsa-state.service kexec.target selinux-policy-migrate-local-changes@.service
alsa-store.service kexec.target.wants serial-getty@.service
anaconda-direct.service kmod-static-nodes.service shutdown.target
anaconda-nm-config.service kpatch.service shutdown.target.wants
anaconda-noshell.service ksm.service sigpwr.target
anaconda-pre.service ksmtuned.service sleep.target
anaconda.service libstoragemgmt.service -.slice
anaconda-shell@.service libvirtd.service slices.target
anaconda-sshd.service libvirt-guests.service smartcard.target
anaconda.target lldpad.service smartd.service
anaconda-tmux@.service lldpad.socket sockets.target
arp-ethers.service local-fs-pre.target sockets.target.wants
2. 上述的文件可以归为以下几类:
- [ ] service 系统服务
- [ ] target 多个unit组成的组
- [ ] device 硬件设备
- [ ] mount 文件系统挂载点
- [ ] automount 自动挂载点
- [ ] path 文件或路径
- [ ] scope 不是由systemd启动的外部进程
- [ ] slice 进程组
- [ ] snapshot systemd快照
- [ ] socket 进程间通信套接字
- [ ] swap swap文件
- [ ] timer 定时器
3.unit相关的命令
systemctl list-units //列出正在运行的unit
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
systemctl is-enable crond.service //查看某个服务是否为enable
[root@localhost system]# systemctl is-active crond.service
active
10.27 target介绍:系统为了方便管理用target来管理unit
1.查看当前系统的所有target:systemctl list-unit-files --type=target
[root@localhost ~]# systemctl list-unit-files --type=target
UNIT FILE STATE
anaconda.target static
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
iprutils.target disabled
kexec.target disabled
local-fs-pre.target static
local-fs.target static
machines.target disabled
multi-user.target enabled
2.systemctl list-dependencies multi-user.target //查看一个target包含的所有unit
[root@localhost ~]# systemctl list-dependencies multi-user.target
multi-user.target
● ├─abrt-ccpp.service
● ├─abrt-oops.service
● ├─abrt-vmcore.service
● ├─abrt-xorg.service
● ├─abrtd.service
● ├─atd.service
● ├─auditd.service
● ├─avahi-daemon.service
● ├─brandbot.path
● ├─chronyd.service
● ├─crond.service
● ├─cups.path
● ├─cups.service
● ├─dbus.service
● ├─irqbalance.service
● ├─kdump.service
● ├─ksm.service
● ├─ksmtuned.service
● ├─libstoragemgmt.service
● ├─libvirtd.service
● ├─mdmonitor.service
● ├─ModemManager.service
● ├─netcf-transaction.service
● ├─network.service
3.查看指定target下面有哪些unit:systemctl list-dependencies basic.target
[root@localhost ~]# systemctl list-dependencies basic.target
basic.target
● ├─alsa-restore.service
● ├─alsa-state.service
● ├─iptables.service
● ├─microcode.service
● ├─rhel-autorelabel-mark.service
● ├─rhel-autorelabel.service
● ├─rhel-configure.service
● ├─rhel-dmesg.service
● ├─rhel-loadmodules.service
● ├─selinux-policy-migrate-local-changes@targeted.service
● ├─paths.target
● ├─slices.target
● │ ├─-.slice
● │ └─system.slice
● ├─sockets.target
● │ ├─avahi-daemon.socket
● │ ├─cups.socket
● │ ├─dbus.socket
● │ ├─dm-event.socket
● │ ├─iscsid.socket
● │ ├─iscsiuio.socket
● │ ├─rpcbind.socket
● │ ├─systemd-initctl.socket
● │ ├─systemd-journald.socket
4. systemctl get-default //查看系统默认的target
[root@localhost ~]# systemctl get-default
multi-user.target
5.systemctl set-default multi-user.target //设置默认的target
[root@localhost ~]# 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.
6.多个unit组成了一个target;一个target里面包含了多个service
7.查看sshd.service属于哪个target
cat /usr/lib/systemd/system/sshd.service //看[install]部分