1、任务计划:crond [iyunv@litongyao ~]# cat /etc/crontab (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 (分钟)(小时)(日期)(月份)(星期)【星期天=0】 用户(不写的话默认是root) 命令(这里的命令必须是绝对路径)
[iyunv@litongyao ~]# crontab -e (修改crontab的配置文件) 0 3 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log2 2>/tmp/1234.log (每天3点钟执行/usr/local/sbin/123.sh这个脚本,正确的日志追加到/tmp/123.log下,错误的日志追加到/tmp/1234.log下。)
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log2 2>/tmp/1234.log (双数月1-10号周二和周五的凌晨三点运行这个脚本,并追加日志)
要想让服务正常启动,则需要启动服务: [iyunv@litongyao ~]# systemctl start crond [iyunv@litongyao ~]# ps aux | grep cron root 537 0.0 0.1 126236 1612 ? Ss 07:23 0:00 /usr/sbin/crond -n root 6506 0.0 0.1 125336 1116 ? Ss 15:01 0:00 /usr/sbin/anacron -s root 6625 0.0 0.0 112680 972 pts/0 S+ 15:12 0:00 grep --color=auto cron 服务启动,还可以用 [iyunv@litongyao ~]# systemctl status crond (查看服务启动情况) [iyunv@litongyao ~]# crontab -l (查看任务计划) 0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log2 2>/tmp/1234.log 其实-l所查看的目录就是/var/spool/cron/(这里会有不同用户的文件名,主要是写任务计划是用户是谁的就在谁的目录下)
[iyunv@litongyao ~]# crontab -r (删除任务计划) [iyunv@litongyao ~]# crontab -l no crontab for root
二、linux系统管理chkconfing
centos6和之前的版本会用倒chkconfig,centos7时已经不用,为了兼容,我们还是要掌握 [iyunv@litongyao ~]# 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级别 关机状态
1级别 单用户状态 2级别 比3少一个nfs服务 3级别 多用户模式,但是不带图像 4级别 保留级别 5级别 多用户级别。带图像 6级别 重启
[iyunv@litongyao ~]# chkconfig network off (设置开机不启动) [iyunv@litongyao ~]# chkconfig --level 3 network off (设置3级别network为关闭状态) [iyunv@litongyao ~]# chkconfig --level 345 network off (设置3.4.5级别,network为关闭状态)
添加系统服务启动:(服务启动脚本放到必须在/etc/init.d文件下) 举例:(复制一个network的启动脚本改名为123,添加服务123.用List查看。) 删除系统服务启动:
三、systemd管理服务
centos7后,使用systemd服务,在之前使用sysv服务。chkconfig在7中也能使用,这一小节则教我们使用systemctl [iyunv@litongyao ~]# systemctl --all --type=service (查看所有的服务,如果去掉all,则未激活的服务不显示)
[iyunv@litongyao ~]# systemctl is-enabled crond (检查服务是否开机启动) enabled [iyunv@litongyao ~]# systemctl disable crond (不让开机启动) Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[iyunv@litongyao ~]# systemctl enable crond (设置开机启动) Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
我们不难发现,其实设置开机启动是给/usr/lib/systemd/system/crond.service.做了一个软连接/etc/systemd/system/multi-user.target.wants/crond.service。 [iyunv@litongyao ~]# ll /etc/systemd/system/multi-user.target.wants/crond.service lrwxrwxrwx 1 root root 37 12月 4 16:16 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[iyunv@litongyao ~]# systemctl status crond (查看服务的运行状况) ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 四 2017-11-30 15:50:14 CST; 4 days ago Main PID: 537 (crond) CGroup: /system.slice/crond.service └─537 /usr/sbin/crond -n
11月 30 15:50:14 litongyao systemd[1]: Started Command Scheduler. 11月 30 15:50:14 litongyao systemd[1]: Starting Command Scheduler... 11月 30 15:50:14 litongyao crond[537]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% if used.) 11月 30 15:50:14 litongyao crond[537]: (CRON) INFO (running with inotify support) [iyunv@litongyao ~]# systemctl stop crond (停止服务) [iyunv@litongyao ~]# systemctl start crond (开启服务)
四、unit介绍
在系统/usr/lib/systemd/system下存放着所有的unit unit分为以下几个类型: service 系统服务 target 多个unit组成的组 device 硬件设备 mount 文件系统挂载点 automount 自动挂载点 path 文件或路径 scope 不是由systemd启动的外部进程 slice 进程组 snapshot systemd快照 socket 进程间通信套接字 swap swap文件 timer 定时器
[iyunv@litongyao ~]# cd /usr/lib/systemd/system [iyunv@litongyao system]# ls -l runlevel* (会显示出来centos7的6个等级) lrwxrwxrwx. 1 root root 15 10月 20 08:10 runlevel0.target -> poweroff.target lrwxrwxrwx. 1 root root 13 10月 20 08:10 runlevel1.target -> rescue.target lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel2.target -> multi-user.target lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel3.target -> multi-user.target lrwxrwxrwx. 1 root root 17 10月 20 08:10 runlevel4.target -> multi-user.target lrwxrwxrwx. 1 root root 16 10月 20 08:10 runlevel5.target -> graphical.target lrwxrwxrwx. 1 root root 13 10月 20 08:10 runlevel6.target -> reboot.target
[iyunv@litongyao system]# systemctl list-units (列出正在运行的unit) [iyunv@litongyao system]# systemctl list-units (列出所有,包括失败的或者inactive的) [iyunv@litongyao system]# systemctl list-units --all --state=inactive (列出inactive的unit) [iyunv@litongyao system]# systemctl list-units --type=service (列出状态为active的service) [iyunv@litongyao system]# systemctl is-active crond (查看某个服务是否为active)
五、target介绍
系统为了方便管理用target来管理unit systemctl list-unit-files --type=target systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit systemctl get-default //查看系统默认的target systemctl set-default multi-user.target 一个service属于一种类型的unit 多个unit组成了一个target 一个target里面包含了多个service cat /usr/lib/systemd/system/sshd.service //看[install]部分
|