Linux设置日期时间,任务计划cron,chkconfig,systemd

醉酒当歌 提交于 2019-12-04 07:36:53

任务计划cron

当我们需要在凌晨执行一条命令或运行一个脚本的时候,我们可能不会守在电脑旁,等时间到了去操作,经常我们会使用任务计划cron来实现。

设置系统时间

而使用任务计划往往会用到日期和时间等数值,但是我们在安装centos的时候时间选的是对的,但是在安装完了之后我们会发现系统的时间跟实际时间是对不上的,这是因为安装系统是采用了UTC,简单来说UTC就是0时区的时间,是国际标准,而我们中国是处于东八区+8时区。这个时候就需要修改系统时间了。

  • 机器上面都有两个时间,一个是系统时间,一个是硬件时间.
  • 使用date命令可以查看系统时间
[root@localhost ~]# date
2018年 07月 27日 星期五 21:56:16 CST
  • 使用clock或hwclock查看硬件时间
[root@localhost ~]# clock
2018年07月27日 星期五 21时57分01秒  -0.810611 秒
[root@localhost ~]# hwclock
2018年07月27日 星期五 21时57分09秒  -0.219701 秒
  • 设置系统时间可以使用 date -s (月月日日时时分分年年年年.秒秒) 括号里每个字代表一个字符,比如2018年2月3日10点55分44秒 date 020310552018.44
[root@llll ~]# date  020310552018.22
2018年 02月 03日 星期六 10:55:22 CST
  • 或者分开设置,先设置日期date -s 07/27/18这里是拿2018-7-27来举例。
  • 再设置时间 date -s 22:09:22
  • 将系统时间同步到硬件时间 hwclock --systohc 时间设置完成之后就可以做任务计划了。

设置任务计划

有两个文件可以控制crontab能否被其他用户使用,/etc/cron.deny 和/etc/cron.allow 系统默认保留的是/etc/cron.deny,最好选择一个使用,避免逻辑混乱。如果不想用户lic使用crontab功能,可以将lic添加到/etc/corn.deny文件中。

  • crontab命令
  • crontab -u 只有root用户能够使用该参数,也就是帮其他用户添加删除crontab任务计划
  • crontab -e 编辑crontab的工作内容
  • crontab -l 查阅crontab的工作内容
  • crontab -r 删除所有的crontab的内容,如果要删除一项,可以使用crontab -e去删除一项。

** 使用crontab来新建了任务计划之后,该项计划就会被记录到/var/spool/cron里面,且是以账号来判别的。比如,root用户建了任务计划就会被写到/var/spool/cron/root中;另外cron执行的每一项工作都会被写到/var/log/cron这个日志文件中,所以如果不知道系统是否被恶意运行过cron ,可以查看该日志文件。**

任务计划的配置文件是/etc/crontab可以cat看一下里面的说明

[root@llll ~]# 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

可以清楚的看到,编辑一个任务计划时,第一段分钟第二段小时第三段日期第四段月份第五段礼拜几 第六段 命令 。如果每隔五分钟操作一次,就在分钟哪里*/5就可以了。

  • 可以使用crontab -l 命令来查看任务计划
[root@llll ~]# crontab -l
00 23 * * * mail li.chao11@21vianet.com < /tmp/1.txt
  • 如果要删除所有的任务计划,可以使用crontab -r 命令来实现,如果仅仅删除一条计划可以使用crontab -e进入文件后删除相对应的那一行。
[root@llll ~]# crontab -r
[root@llll ~]# crontab -l
no crontab for root

chkconfig工具

chkconfig工具是centos7之前的版本使用的系统服务管理工具,在7之后该工具下面只有少数几个服务在里面,其他的系统服务都在systemd中

[root@llll ~]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

iprdump        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprinit        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprupdate      	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

上面显示的这些服务,文件是在/etc/init.d下的

[root@llll init.d]# ls
functions  iprdump  iprinit  iprupdate  netconsole  network  README
  • 可以使用chkconfig命令来关闭或开启一个服务在某个运行级别上。举例:chkconfig --level 34 network off 运行在3和4级别上关掉network服务。
[root@llll init.d]# chkconfig --level 34 network off
[root@llll init.d]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

iprdump        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprinit        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprupdate      	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:关	4:关	5:开	6:关

还可以使用chkconfig --add 来将某个服务添加到chkconfig工具下管理或使用chkconfig --del 将某个服务从chkconfig管理工具下删除。不管删除还是添加有一个前提是该服务有一个有效的配置文件在/etc/init.d/中

[root@llll init.d]# cp network 222
[root@llll init.d]# ls
222  functions  iprdump  iprinit  iprupdate  netconsole  network  README
[root@llll init.d]# cd ..
[root@llll etc]# cd
[root@llll ~]# chkconfig --add 222
[root@llll ~]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

222            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprdump        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprinit        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
iprupdate      	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:关	4:关	5:开	6:关

systemd服务管理工具

在centOS7 及以后的版本,systemd这个管理工具是很重要的。

  • systemctl list-unit-files命令来查看所有的units-files显示的内容很多,不方便查看
  • systemctl list-units --all --type=service 命令会列出所有的服务
  • systemctl list-units --type=service 命令会列出所有激活的(active)服务
  • 以crond服务为例,开机启动 systemctl -enable crond.service
  • 开机不启动 systemctl -disable crond.service
  • 查看状态 systemctl status crond
  • 停止服务systemctl stop crond
  • 启动服务systemctl start crond
  • 重启服务systemctl restart crond\
  • 检查服务是否开机启动 systemctl is-enabled crond
[root@llll ~]# systemctl is-enabled crond
enabled

unit→/usr/lib/systemd/system下的所有的都是unit

  • service 系统服务
  • target 多个unit组成的组
  • device 硬件设备
  • mount 挂在点
  • automount 自动挂载点
  • path 文件或路径
  • scope 不是由systemd启动的进程
  • slice 进程组
  • snapshot systemd快照
  • socket 进程间通信套接字
  • swap swap文件
  • timer 定时器 总之,所有的unit组成了系统,是一个非常复杂的组合。我们常用到的可能就是service 和target。

Linux 与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的service
  • systemctl is-active crond 查看某个服务是否是active

target

系统为了方便管理,用target管理unit

  • systemctl list-unit-files --type=target查看所有target
  • systemctl list -dependencies multi-user.target查看指定target下有哪些unit
  • systemctl get-default 查看系统默认target
  • 查看某个服务的target cat/usr/lib/systemd/system/sshd.service 中的INSTALL部分。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!