linux任务计划cron
cron工具用来制定任务计划,大部分系统管理工作都是通过定期自动执行某个脚本来完成,如何制定就靠cron工具
- -u:表示指定某个用户,不加-u选项则默认当前用户。
- -e:表示制定计划任务。
- -l:表示列出计划任务。
- -r:表示删除计划任务。
通过以下命令查看计划任务的配置文件,了解计划文件的内容编写格式
[root@ask-02 ~]# cat /etc/crontab
模拟编写一个任务计划使用crontab -e命令来操作
[root@ask-02 ~]# crontab -e no crontab for root - using an empty one
0 3 */2 * 6 /usr/bin/echo "hello world" > /root/cs.log //字符串分别意思是(从左往右):分、时、日、月、周和命令行
编写好后保存并退出,执行以下命令开启该任务计划
[root@ask-02 ~]# systemctl start crond
使用以下命令查看查看执行状态
[root@ask-02 ~]# systemctl status crond
使用以下命令停止任务计划
[root@ask-02 ~]# systemctl stop crond
chkconfig工具
在Centos 6上的服务管理工具为chkconfig工具,Linux系统所有预设的服务都已可以通过**/etc/init.d/**目录得到
[root@ask-02 ~]# ls /etc/init.d/ functions netconsole network README
只有屈指可数的几个文件是因为Centos 7已经不再延续Centos 6版本的服务管理方案,但是我们依然可以继续使用chkconfig这个命令。 使用命令**chkconfig --list **列出所有的服务及其每个级别的开启状态
[root@ask-02 ~]# chkconfig --list
图中我们可以看到执行命令后系统所提示的内容,说明内容并没有包含Centos 7原生的systemd服务,而仅仅列出了SysV服务,也就是说Centos 7之前的版本采用的服务都是SysV。
这里的级别(数字0-6)为系统启动级别(现在的Centos 7已经不再严格区分级别的概念),运行级别0、1和6倍系统保留。其中0作为shutdown动作,1作为重启至单用户模式,6为重启。一般都是用2、3、4、5级别,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表示图形登录方式
更改某级别下的开启状态
[root@ask-02 ~]# chkconfig --level 3 network off [root@ask-02 ~]# chkconfig --list |grep network network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
这里**--level指定级别,后面跟服务名,然后是设定开关(off关on**开),还可以指定多个级别
[root@ask-02 ~]# chkconfig --level 245 network off [root@ask-02 ~]# chkconfig --list |grep network network 0:关 1:关 2:关 3:关 4:关 5:关 6:关
还可以省略掉级别,但默认是针对2、3、4、5级别来操作的
[root@ask-02 ~]# chkconfig network on [root@ask-02 ~]# chkconfig --list |grep network network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
chkconfig还有以下功能
[root@ask-02 ~]# chkconfig --del network //移除指定的服务 [root@ask-02 ~]# chkconfig --add network //添加指定的服务
systemd管理服务
使用systemd以下命令列出系统所有的服务
[root@ask-02 ~]# systemctl list-units --all --type=service
关于systemd常用的相关命令
[root@ask-02 ~]# systemctl enable crond.service //让某个服务开启(.service可以省略) [root@ask-02 ~]# systemctl disable crond //不让开机启动 [root@ask-02 ~]# systemctl status crond //查看服务状态 [root@ask-02 ~]# systemctl start crond //启动某个服务 [root@ask-02 ~]# systemctl stop crond //停止某个服务 [root@ask-02 ~]# systemctl restart crond //重启某个服务 [root@ask-02 ~]# systemctl is-enable crond //查看某个服务是否开机启动
unit介绍
使用以下命令查看启动脚本文件
[root@ask-02 ~]# ls /usr/lib/systemd/system/ abrt-ccpp.service quotaon.service abrtd.service rc-local.service abrt-oops.service rdisc.service abrt-pstoreoops.service reboot.target abrt-vmcore.service reboot.target.wants
这个路径下有很多文件,这里简单列出一小部分,这些文件有多种类型,不同类型的文件都为一个unit,正式这些unit组成了系统的各个资源。unit可以归类为以下几种
- sercive:系统服务
- target:多个unit组成的组
- device:硬件设备
- mount:文件系统挂载点
- automount:自动挂载点
- path:文件或路径
unit相关的命令
[root@ask-02 ~]# systemctl list-units //列出正在运行(active)的unit [root@ask-02 ~]# systemctl list-units --all //列出所有的unit(包括失败的、inactive的) [root@ask-02 ~]# systemctl list-units --all --state=inactive //列出所有inactive的unit [root@ask-02 ~]# systemctl list-units --all --type=service //列出所有状态的service [root@ask-02 ~]# systemctl list-units --type=service //列出状态的active的service [root@ask-02 ~]# systemctl is-active crond.service //查看某个unit是否active
target介绍
target其实是多个unit的组合,为了管理方便,就是用target来管理这些unit。 查看当前系统所有的target;
[root@ask-02 ~]# systemctl list-unit-files --type=target
查看一个target包含的所有unit
[root@ask-02 ~]# systemctl list-dependencies multi-user.target
关于target相关的命令
[root@ask-02 ~]# systemctl get-default //查看系统默认的target multi-user.target [root@ask-02 ~]# systemctl set-default multi-user.target //设置默认的target