20190929计划任务

23、任务计划

#任务计划的配置文件

[root@linux-01 ~]# 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
               # 不写用户名就默认为root

使用方法 

# 编辑crontab 配置文件
 [root@linux-01 ~]# crontab -e

与vim一样按“i” 进入编辑
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log

/usr/local/abin/123.sh  #执行脚本
>/tmp/123.log           #正确的输出重定向
2>>/tmp/123.log         #错误的也追加到这来



0 3 1-10 */2 2,5 
0 3 
1-10     #1-10号
*/2      #能被2整除的双数月份 
2,5      #周二或者周五

启动停止服务

#启动
[root@linux-01 ~]# systemctl start crond 

#停止
[root@linux-01 ~]# systemctl stop crond

查看进程


[root@linux-01 ~]# ps aux |grep crond
root        606  0.0  0.1 126316  1672 ?        Ss   17:52   0:00 /usr/sbin/crond -n
root       3264  0.0  0.0 112724   996 pts/1    R+   21:19   0:00 grep --color=auto crond

 查看状态

[root@linux-01 ~]# systemctl status crond

 [root@linux-01 ~]# systemctl stop crond

 

服务没有启动,有能是编写配置文件的时候没有使用绝对路径 ,或者命令没有在配置文件中命令路径的目录中。

*建议没写一个任务计划都写一个日志,无论成功失败都有路径可查

备份任务配置文件

#查看任务计划列表
[root@linux-01 ~]# crontab -l
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log

[root@linux-01 ~]# cat /var/spool/cron/root 
0 3 * * * /bin/bash  /usr/local/abin/123.sh >/tmp/123.log 2>>/tmp/123.log
#配置文件存放路径 每个用户都有一个对应得cron 
#备份时复制这个文件

#删除任务文件
[root@linux-01 ~]# crontab -r


#查看指定用户任务计划
[root@linux-01 ~]# crontab -u root -l
no crontab for root

服务管理工具chkconfig(centos7不在使用但仍然兼容)

#列出所有服务
[root@linux-01 ~]# 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:关   #6个级别

 # 0级别 关机状态  
 # 1级别 单用户
 # 2级别 多用户模式(不带图形、NFS服务)
 # 3级别 多用户模式(不带图形)
 # 4级别 保留
 # 5级别 多用户模式(带图形)
 # 6级别 重启

 

服务的服务脚本

[root@linux-01 ~]# ls /etc/init.d
functions  netconsole  network  README
#关闭服务
[root@linux-01 ~]# chkconfig network off
[root@linux-01 ~]# 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:关

linux 有7个运行级别,分别从init 0到init 6。

init 0:关机状态,默认级别不能设为0,否则一开机就会自动关机

init 1:单用户模式,root权限,一般系统维护时才会使用,不能远程登录,只能本地

init 2:多用户模式,没有NFS和网络支持,说白了就是不能上网的init 3

init 3:完整的多用户文本模式,有NFS和网络,登录后可以进入控制台命令行模式(最常用的)

init 4:没啥用的模式,不用管它......

init 5:图形化界面,比较美观,但耗内存

init 6:重启,默认级别不能设为6,否则一开机就会自动重启

比较重要的级别:0,6(关机和重启的时候用)和3,5,所以先记住这四个级别就够用了

 

更改文件定义运行级别,系统一启动就定义在某个级别

[root@linux-01 ~]# vi /etc/inittab 

修改以下配置文件(新系统已全部注析)
# inittab is no longer used when using systemd.
#
# ADDING CONFIGURATION HERE WILL HAVE NO EFFECT ON YOUR SYSTEM.
#
# Ctrl-Alt-Delete is handled by /usr/lib/systemd/system/ctrl-alt-del.target
#
# systemd uses 'targets' instead of runlevels. By default, there are two main targets:
#
# multi-user.target: analogous to runlevel 3
# graphical.target: analogous to runlevel 5
#
# To view current default target, run:
# systemctl get-default
#
# To set a default target, run:
# systemctl set-default TARGET.target

设置某个级别开启或关闭

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
[root@linux-01 ~]# chkconfig --level 3 network off
[root@linux-01 ~]# 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@linux-01 ~]# chkconfig --level 345 network off
[root@linux-01 ~]# 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@linux-01 ~]# cd /etc/init.d
[root@linux-01 init.d]# ls
functions  netconsole  network  README
[root@linux-01 init.d]# cp network 123
[root@linux-01 init.d]# ls -l
总用量 48
-rwxr-xr-x  1 root root  7293 9月  11 22:57 123
-rw-r--r--. 1 root root 18104 1月   3 2018 functions
-rwxr-xr-x. 1 root root  4334 1月   3 2018 netconsole
-rwxr-xr-x. 1 root root  7293 1月   3 2018 network
-rw-r--r--. 1 root root  1160 4月  11 2018 README
[root@linux-01 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:关
[root@linux-01 init.d]# chkconfig --add  123
[root@linux-01 init.d]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

123            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:关	4:关	5:关	6:关    #已添加到服务里
[root@linux-01 init.d]# 



#删除
[root@linux-01 init.d]# chkconfig --del  123
[root@linux-01 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:关

 

配置文件有格式要求

 

[root@linux-01 ~]# systemctl list-units --all --type=service  

按空格键向下翻

 

如果想列出所有service 请使用   systemctl list-unit-files'

几个常用的服务相关的命令

•systemctl enable crond.service //让服务开机启动

[root@linux-01 ~]# systemctl enable crond.service
[root@linux-01 ~]# 

• systemctl disable crond //不让开机启动

[root@linux-01 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@linux-01 ~]# 

• systemctl status crond //查看状态

[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

9月 06 22:10:24 linux-01 systemd[1]: Started Command Scheduler.
9月 06 22:10:24 linux-01 systemd[1]: Starting Command Scheduler...
9月 06 22:10:24 linux-01 crond[606]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 86% if used.)
9月 06 22:10:25 linux-01 crond[606]: (CRON) INFO (running with inotify support)
9月 11 21:23:12 linux-01 systemd[1]: Stopping Command Scheduler...
9月 11 21:23:12 linux-01 systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@linux-01 ~]# 

 

• systemctl start crond //启动服务

[root@linux-01 ~]# systemctl start crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2019-09-13 15:23:09 CST; 3s ago
 Main PID: 4066 (crond)
   CGroup: /system.slice/crond.service
           └─4066 /usr/sbin/crond -n

9月 13 15:23:09 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 98% i...ed.)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (running with inotify support)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Hint: Some lines were ellipsized, use -l to show in full.

 

• systemctl stop crond //停止服务

[root@linux-01 ~]# systemctl stop crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: inactive (dead)

9月 06 22:10:25 linux-01 crond[606]: (CRON) INFO (running with inotify support)
9月 11 21:23:12 linux-01 systemd[1]: Stopping Command Scheduler...
9月 11 21:23:12 linux-01 systemd[1]: Stopped Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:23:09 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 98% i...ed.)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (running with inotify support)
9月 13 15:23:09 linux-01 crond[4066]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
9月 13 15:24:03 linux-01 systemd[1]: Stopping Command Scheduler...
9月 13 15:24:03 linux-01 systemd[1]: Stopped Command Scheduler.
Hint: Some lines were ellipsized, use -l to show in full.
[root@linux-01 ~]# 

• systemctl restart crond //重启服务

[root@linux-01 ~]# systemctl restart crond.service
[root@linux-01 ~]# systemctl status crond.service
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
   Active: active (running) since 五 2019-09-13 15:26:04 CST; 6s ago
 Main PID: 4083 (crond)
   CGroup: /system.slice/crond.service
           └─4083 /usr/sbin/crond -n

9月 13 15:26:04 linux-01 systemd[1]: Started Command Scheduler.
9月 13 15:26:04 linux-01 systemd[1]: Starting Command Scheduler...
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 43% i...ed.)
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (running with inotify support)
9月 13 15:26:04 linux-01 crond[4083]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
Hint: Some lines were ellipsized, use -l to show in full.

• systemctl is-enabled crond //检查服务是否开机启动

[root@linux-01 ~]# systemctl is-enabled crond
disabled

查看service 配置文件内容

[root@linux-01 ~]# cat /etc/systemd/system/multi-user.target.wants/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@linux-01 ~]# 

这个文件实际上是个软连接 

[root@linux-01 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 9月  13 15:30 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service
[root@linux-01 ~]#  
[root@linux-01 ~]# 
[root@linux-01 ~]# ls -l /usr/lib/systemd/system/crond.service
-rw-r--r--. 1 root root 284 4月  11 2018 /usr/lib/systemd/system/crond.service
[root@linux-01 ~]# 

如果enable 就会创建一个软连接它实际的源路径是/usr/lib/systemd/system

[root@linux-01 ~]# ls  /usr/lib/systemd/system
arp-ethers.service                      rescue.target
auditd.service                          rescue.target.wants
autovt@.service                         rhel-autorelabel-mark.service
basic.target                            rhel-autorelabel.service
basic.target.wants                      rhel-configure.service
blk-availability.service                rhel-dmesg.service
bluetooth.target                        rhel-domainname.service
brandbot.path                           rhel-import-state.service
brandbot.service                        rhel-loadmodules.service
chrony-dnssrv@.service                  rhel-readonly.service

 

unit介绍

• ls /usr/lib/systemd/system //系统所有unit,分为以下类型

• service 系统服务

• target 多个unit组成的组

• device 硬件设备

• mount 文件系统挂载点

• automount 自动挂载点

• path 文件或路径

• scope 不是由systemd启动的外部进程

• slice 进程组

• snapshot systemd快照

• socket 进程间通信套接字

• swap  swap文件

• timer 定时器

 

centos有7个运行级别 、而centos7也有7个运行级别

unit里有种类型是target,它是由多个service或unit组成组形成target 

• 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

 

 target介绍 

•系统为了方便管理用target来管理unit

• systemctl list-unit-files --type=target

  systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit

 对比

 root@localhost ~]# systemctl list-dependencies basic.target

• systemctl get-default //查看系统默认的target

[root@localhost ~]# systemctl get-default
multi-user.target
[root@localhost ~]# 

 centos7 可以更改默认的target来决定类似centos6的运行级别

• systemctl set-default multi-user.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.
[root@localhost ~]# 

• unit有多种类型、一个service属于一种类型的unit

• 多个unit组成了一个target

• 一个target里面包含了多个service

• cat /usr/lib/systemd/system/sshd.service //看[install]部分

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值