博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍...
阅读量:7025 次
发布时间:2019-06-28

本文共 7865 字,大约阅读时间需要 26 分钟。

  hot3.png

10.23 linux任务计划cron

>crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

语法: crontab [options]

Options:
-e:=edit 编辑用户的计时器设置
-l:=list 列出用户的计时器设置
-r:=remove 删除用户的计时器设置
-u:=user 指定设定计时器的用户

配置计划任务

[root@cham002 ~]# cat /etc/crontabSHELL=/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

设定计划任务 

0 3 * * *  /bin/bash /usr/local/sbin/123.sh >> /tmp/123.log 2>> /tmp/123.log分时日月周0 3 1-10 */2 2,5  /bin/bash /usr/local/sbin/123.sh >> /tmp/123.log 2>> /tmp/123.log
  • 第一条命令:

    每天凌晨3点(*位置不指定数字就代表每天、月、周),当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

  • 第二条命令:

    每个偶数月(*/2:表示能被2整除)1号到10号的周二和周五的凌晨3点,当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

启动crond服务/查看服务状态

[root@cham002 ~]# systemctl start crond[root@cham002 ~]# ps aux |grep cronroot       545  0.0  0.1 126224  1608 ?        Ss   01:36   0:00 /usr/sbin/crond -nroot      4702  0.0  0.0 112664   972 pts/0    R+   13:30   0:00 grep --color=auto cron[root@cham002 ~]# systemctl status crond● crond.service - Command Scheduler   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)   Active: active (running) since 六 2017-12-02 18:00:18 CST; 1 day 19h ago Main PID: 545 (crond)   CGroup: /system.slice/crond.service           └─545 /usr/sbin/crond -n12月 02 18:00:18 cham002 systemd[1]: Started Command Scheduler.12月 02 18:00:18 cham002 systemd[1]: Starting Command Scheduler...12月 02 18:00:19 cham002 crond[545]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 30% if used.)12月 02 18:00:21 cham002 crond[545]: (CRON) INFO (running with inotify support)[root@cham002 ~]#

 注意: 在编写配置文件或者shell脚本时,所有的命令都要使用绝对路径;每个计划任务追加一个日志。

查看现有的计划任务

[root@cham002 ~]# crontab -lno crontab for root[root@cham002 ~]# crontab -l1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f[root@cham002 ~]# cat /var/spool/cron/root1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f

计划任务存放位置: /var/spool/cron/,所有的计划任务存放在该目录下以用户名命名的文件中,备份时可以使用该文件。

删除计划任务

[root@cham002 ~]# crontab -r[root@cham002 ~]# crontab -lno crontab for root

注: 以上所有操作都可以附加-u选项来指定用户

 

10.24 chkconfig工具

>chkconfig命令检查、设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接(该命令多用于centos6及以前版本)。

语法: chkconfig [options]

Options:
--list:查看在使用chkconfig命令的服务的状态
--add:增加指定服务
--del:删除指定服务
--level:指定某系统服务要在系统某运行级别中开启或关毕。

应用:

  • chkconfig --list 查看当前系统服务状态
[root@cham002 ~]# 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@cham002 ~]# ls /etc/init.d/functions  netconsole  network  README

更改服务状态

  • 更改服务所有状态:
[root@cham002 ~]# chkconfig network off[root@cham002 ~]# 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@cham002 ~]# chkconfig network on[root@cham002 ~]# 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@cham002 ~]# 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@cham002 ~]# chkconfig --level 3 network off[root@cham002 ~]# 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@cham002 ~]# chkconfig --level 35 network off[root@cham002 ~]# 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@cham002 ~]# chkconfig --level 345 network on[root@cham002 ~]# 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/inittab”,centos7已不再使用该文件。

 

添加/删除服务

首先,在添加服务之前必须把该服务的脚本放到“/etc/init.d/”目录下并添加执行权限。然后执行命令:

[root@cham002 ~]# 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@cham002 ~]# cd /etc/init.d/[root@cham002 init.d]# lsfunctions  netconsole  network  README[root@cham002 init.d]# cp network  123[root@cham002 init.d]# ls123  functions  netconsole  network  README[root@cham002 init.d]# chkconfig --add 123[root@cham002 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@cham002 init.d]# chkconfig --del 123[root@cham002 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管理服务

>systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

systemctl命令

•systemctl status crond //查看状态

• systemctl stop crond //停止服务

• systemctl start crond //启动服务

• systemctl restart crond //重启服务

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

[root@cham002 ~]# systemctl disable crondRemoved symlink /etc/systemd/system/multi-user.target.wants/crond.service.[root@cham002 ~]# systemctl enable crondCreated symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

说明: 伴随某服务的开/关会建立/删除一个指向该服务的软链接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”

 

10.26 unit介绍

unit所在目录: /usr/lib/systemd/system/

unit文件类型

unit相关命令

10.27 target介绍

>系统为了方便管理,所以使用target来管理unit。

相关操作:

target、service、unit关系

一个service属于一种类型的unit,多个unit组成一个target,一个target包含多个service。

查看一个service属于哪个target:

[root@cham002 ~]# cat /usr/lib/systemd/system/sshd.service  看Install部分!………[Install]WantedBy=multi-user.target

 

转载于:https://my.oschina.net/u/3708120/blog/1584678

你可能感兴趣的文章
django中orm的简单操作
查看>>
Mybatis知识(1)
查看>>
[CentOS] 7 不执行文件 /etc/rc.d/rc.local
查看>>
模态窗口的各个属性
查看>>
10.28 (上午) 开课一个月零二十四天 (数据访问)
查看>>
为什么你应该(从现在开始就)写博客
查看>>
小技巧积累
查看>>
Java JDBC链接Oracle数据库
查看>>
Moss2010 部署命令
查看>>
Git 操作分支
查看>>
Grid search in the tidyverse
查看>>
hdu 三部曲 Contestants Division
查看>>
day22——创建表、增加数据、查询数据
查看>>
c# 调用 c dll 例子
查看>>
【C#】string格式的日期转为DateTime类型及时间格式化处理方法
查看>>
实验十三:窗口设计
查看>>
python解析XML的三种方法
查看>>
(转)如何使用C#自定义属性
查看>>
hdu 1142 A Walk Through the Forest (最短路径)
查看>>
HLG 1475 国王的宴会【树形DP】
查看>>