linux schedule 目录,linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍...

目录

一、linux任务计划cron

二、 chkconfig工具

三、 systemd管理服务

四、 unit介绍

五、 target介绍

一、linux任务计划cron

Linux任务计划功能的操作都是通过crontab命令来完成的。首先,查看一下任务计划的配置文件:

[root@minglinux-01 ~]# man crontab

[root@minglinux-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

crontab命令的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段。从左到右依次为:分、时、日、月、周和命令行。

在以上各个字段中,还可以使用以下特殊字符:

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

crontab常用的选项有以下:

-u:表示指定某个用户,不加-u选项则为当前用户。

-e:表示制定计划任务。

-l:表示列出计划任务。

-r:表示删除计划任务。

crontab -e 创建任务计划

命令crontab -e实际上是打开了/var/spool/cron/username文件(如果用户是root,则打开的是/var/spool/cron/root)。

[root@minglinux-01 ~]# crontab -e

no crontab for root - using an empty one

然后我们写入任务计划内容,表示在10月30日星期二的22点08分执行echo "ok" > /root/cron.log命令

08 22 30 10 2 echo "ok" > /root/cron.log

任务计划已完成:

[root@minglinux-01 ~]# date

2018年 10月 30日 星期二 22:08:55 CST

[root@minglinux-01 ~]# cat /root/cron.log

ok

crontab -l命令查看已经设定的任务计划

[root@minglinux-01 ~]# crontab -l

08 22 30 10 2 echo "ok" > /root/cron.log

crontab -r命令删除任务计划

[root@minglinux-01 ~]# crontab -l

08 22 30 10 2 echo "ok" > /root/cron.log

[root@minglinux-01 ~]# crontab -r

[root@minglinux-01 ~]# crontab -l

no crontab for root

任务计划备份

因为命令crontab -e实际上是打了/var/spool/cron/username文件,所以任务计划都在该文件中,备份该文件就备份了任务计划。

systemctl start crond.service

启动crond服务,保证服务是启动状态 。

systemctl status crond查看一下crond服务是否已经启动

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

● crond.service - Command Scheduler

Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)

Active: active (running) since 六 2018-10-27 01:51:17 CST; 3 days ago

Main PID: 579 (crond)

CGroup: /system.slice/crond.service

└─579 /usr/sbin/crond -n

10月 27 01:51:17 minglinux-01 systemd[1]: Started Command Scheduler.

10月 27 01:51:17 minglinux-01 systemd[1]: Starting Command Schedule...

10月 27 01:51:17 minglinux-01 crond[579]: (CRON) INFO (RANDOM_DELAY...

10月 27 01:51:17 minglinux-01 crond[579]: (CRON) INFO (running with...

Hint: Some lines were ellipsized, use -l to show in full.

看Active那行,如果是启动状态显示为active(running),未启动则显示为inactive (dead)。

二、 chkconfig工具

CentOS 6上的服务管理工具为chkconfig,Linux系统所有的预设服务都可以通过查看/etc/init.d/目

录得到,如下所示:

[root@minglinux-01 ~]# ls /etc/init.d/

functions netconsole network README

使用命令chkconfig --list列出所有的服务及其每个级别的开启状态:

[root@minglinux-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:关

等级代号列表:

等级0表示:表示关机

等级1表示:单用户模式

等级2表示:无网络连接的多用户命令行模式

等级3表示:有网络连接的多用户命令行模式

等级4表示:不可用

等级5表示:带图形界面的多用户模式

等级6表示:重新启动

使用--level指定级别,后面是服务名,然后是off或者on。

[root@minglinux-01 ~]# chkconfig --level 3 network off

[root@minglinux-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:关

-选项--level后面还可以指定多个级别,如下所示:

[root@minglinux-01 ~]# chkconfig --level 345 network off

[root@minglinux-01 ~]# chkconfig --list |grep network

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 'systemctl list-unit-files'。

查看在具体 target 启用的服务请执行

'systemctl list-dependencies [target]'。

network 0:关 1:关 2:开 3:关 4:关 5:关 6:关

另外还可以省略级别,默认是针对级别2、3、4和5操作的,如下所示:

[root@minglinux-01 ~]# chkconfig network on

[root@minglinux-01 ~]# chkconfig --list |grep network

注:该输出结果只显示 SysV 服务,并不包含

原生 systemd 服务。SysV 配置数据

可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 'systemctl list-unit-files'。

查看在具体 target 启用的服务请执行

'systemctl list-dependencies [target]'。

network 0:关 1:关 2:开 3:开 4:开 5:开 6:关

chkconfig还有一个功能,就是可以把某个服务加入系统服务或者删除,即可以使用“chkconfig --add 服务名“或者“chkconfig --del 服务名“这样的形式。

这个功能常用于把自定义的启动脚本加入到系统服务当中。

三、 systemd管理服务

systemctl list-units --all --type=service列出系统所有的服务

oot@minglinux-01 ~]# systemctl list-units --all --type=service

UNIT LOAD ACTIVE SUB DESCRIPTION

auditd.service loaded active running Security Auditin

cpupower.service loaded inactive dead Configure CPU po

crond.service loaded active running Command Schedule

dbus.service loaded active running D-Bus System Mes

● display-manager.service not-found inactive dead display-manager.

dm-event.service loaded inactive dead Device-mapper ev

dracut-shutdown.service loaded inactive dead Restore /run/ini

ebtables.service loaded inactive dead Ethernet Bridge

emergency.service loaded inactive dead Emergency Shell

● exim.service not-found inactive dead exim.service

一些常用systemd相关命令

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

systemctl disable crond.service // 不让开机启动

systemctl status crond.service // 查看服务状态

systemctl start crond.service // 启动某个服务

systemctl stop crond.service // 停止某个服务

systemctl restart crond.service // 重启某个服务

systemctl is-enabled crond// 查看某个服务是否开机启动

四、 unit介绍

执行命令ls /usr/lib/systemd/system的时候,下面有很多文件,其实可以把它们归类为下面这几大类。

service:系统服务。

target:多个unit组成的组。

device:硬件设备。

mount:文件系统挂载点。

automount:自动挂载点。

path:文件或路径

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

slice:进程组。

snapshot:systemd快照。

socket:进程间通信的套接字。

swap:swap文件。

timer:定时器。

以上每种类型的文件都为一个unit,正是这些unit才组成了系统的各个资源(各个服务、各个设备等)

列举几个和unit相关的命令:

systemctl list-units // 列出正在运行(active)的unit

systemctl list-units --all // 列出所有的unit(包括失败的、inactive的)

systemctl list-units --all --state=inactive// 列出所有inactive的unit

systemctl list-units --all --type=service// 列出所有状态的service

systemctl list-units --type=service// 列出状态为active的service

systemctl is-active crond.service// 查看某个unit是否active

五、 target介绍

target类似于CentOS 6里面的启动级别,但target支持多个target同时启动。target其实是多个unit的组合,系统启动说白了就是启动多个unit,为了管理方便,就使用target来管理这些unit。

systemctl list-unit-files --type=target查看当前

系统的所有 target

关于target的命令:

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

systemctl set-default multi-user.target // 设置默认的target

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

service、unit以及target之间的联系:

(1) 一个service属于一种unit;

(2) 多个unit一起组成了一个target;

(3) 一个target里面包含了多个service,你可以查看文件/usr/lib/systemd/system/sshd.service里面[install]部分的内容,它就定义了该service属于哪一个target。

扩展

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值