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

任务计划cron

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

设置系统时间

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

  • 机器上面都有两个时间,一个是系统时间,一个是硬件时间.
  • 使用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文件中。ssh

  • 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 ,能够查看该日志文件。**socket

任务计划的配置文件是/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就能够了。centos7

  • 可使用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中rest

[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/中code

[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部分。
相关文章
相关标签/搜索