三十四、Linux系统任务计划cron、chkconfig工具、systemd管理服务、unit介绍

三十四、Linux系统任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍

一、Linux系统任务计划cron

crontab命令:对任务计划功能的操作用此命令。选项:

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

-e:制定任务计划。

-l:列出任务计划。

-r:删除任务计划。

任务计划的配置文件:/etc/crontab

文件内共有五个字段。

从左往右依次为:分、时、日、月、周、用户、命令。

可以不指定用户就是root。

# crontab -e   //编写任务计划,实际是用vim打开了crontab配置文件。

此时打开的配置文件的/var/spool/cron/username。(若用户是root,则cron后是root)。不能vi编辑这个文件,只能用crontab -e编辑,否则出错。

*表示所有。如每周,每天,每月。

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

1-10日,被2整除的月,周二和周五

# systemctl start crond  启动crond服务,要使用任务计划,就要启动。

# ps aux |grep cron

root        589  0.0  0.0 126236  1612 ?        Ss   01:51   0:00 /usr/sbin/crond -n

root       1460  0.0  0.0 112676   980 pts/0    S+   04:26   0:00 grep --color=auto cron

有这个进程就说明服务已经启动了。

# systemctl status crond   查看它的状态,若启动,则显示绿色的active (running)。若未启动,则无字体有颜色显示,inactive (dead)。

配置文件内写任务计划时,命令写绝对路径,否则可能不执行,因为没在环境变量里。

每次写任务计划时,将正确日志和错误日志都追加保存起来。出问题时就有据可查。

二、Linux系统服务管理-chkconfig

centos6上的服务管理工具是chkconfig,系统内所有预设服务都可以通过/etc/init.d查看到。centos7已经不再延续centos6的服务管理方案了。因此只有几个文件。

# ls /etc/init.d    所有预设服务的目录

functions  netconsole  network  README

# 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:关

centos6及以前是sysv服务管理,centos7是systemd服务管理。

级别:系统启动级别(7之前的用法,7不严格区分级别)

0:关机状态。

1:单用户模式

2:比3少一个NFS服务

3:多用户模式,不带图形

4:保留状态,给用户自定义

5:多用户模式,带图形

6:重启

# chkconfig --level 3 network off

--level指定级别,再服务名,再off或者on。

还可以指定多个级别,--level 345。

也可省略级别,默认对2、3、4、5操作。

服务必须在/etc/init.d/目录内才能增加服务。

增加服务,chkconfig --add network(服务名)

删除服务,chkconfig --del network

可以在chkconfig --list查到。

文件内格式这两点必须有,否则无法识别

# chkconfig: 2345 10 90

# description: Activates/Deactivates all network interfaces configured to \

#              start at boot time.

三、systemd管理服务

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

常用的命令:

# systemctl enable crond            让某个服务开机启动(.service可省略)

# systemctl disable crond            不让服务开机启动

# systemctl status crond              查看服务状态

# systemctl start crond                 启动某服务

# systemctl stop crond                  停止某服务

# systemctl restart crond               重启某服务

# 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介绍

类似于centos6里的启动级别,target支持多个同时启动。它是多个unit的组合,系统启动就是启动了多个unit,target就是用来管理这些unit。

# systemctl list-unit-files --type=target   //查看当前系统所有的target

# systemctl list-dependencies multi-user.target   //查看一个target包含的所有unit,以树形列出来的。

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

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

service、unit和target之间的联系:

1)一个service属于一种unit

2)多个unit一起组成一个target

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

原文地址:http://blog.51cto.com/13576245/2091413

时间: 2024-12-18 12:39:03

三十四、Linux系统任务计划cron、chkconfig工具、systemd管理服务、unit介绍的相关文章

linux任务计划cron chkconfig工具 systemd管理服务 unit介绍 target介绍

[[email protected] ~]# crontab -e  编写任务计划no crontab for root - using an empty one 20 11 29 01 1 echo "OK" > /root/cron.log      表示在1月29日(星期一)的11点20分执行:echo  "OK" > /root/cron.log 命令crontab -e 实际上打开/var/spool/cron/username文件(如果用户是

任务计划cron / chkconfig工具/ system管理服务/unit及target介绍

一.linux任务计划cron 1.cat /etc/crontab 任务计划的配制文件: 2.crontab -e 编辑配制文件. 图片说明,每个月的1-10号3点执行脚本123.sh ,以追加形式生成日志文件123.log,生成错误文件日志321.log crontab -e 实际上是打开了 "/var/spool/cron/username" (如果是root则打开的是/var/spool/cron/root)这个文件,所以不要直接去编辑那个文件,因为可能会出错,所以一定要使用

linux任务计划cron、chkconfig工具、systemd管理、unit介绍、targe介绍

linux任务计划cron linux任务计划:在某个时间自动执行命令或者脚本. 任务计划的配置文件cat /etc/crontab 前面两行是定义变量,第三行是指发送邮件给谁,然后最后一行有五个*(星号)分别对应着五个位,也就是上面的五行,下面来介绍一下分别表示什么意思: 1.表示分钟(0-59) 2.表示小时(0-23) 3.表示日期(1-31) 4.表示月份(1-12可以写数字或者英文的简写) 5.表示星期(0-6,0或者7表示周日,也可以写成英文的简写) 最后一行开头部分是用户(在roo

linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和targe

一. linux任务计划cron 关于cron任务计划功能的操作都是通过crontab这个命令来完成的. 其中常用的选项有: -u :指定某个用户,不加-u选项则为当前用户: /etc/crontab 任务计划的配置文件 前面两行是定义变量,第三行是指发送邮件给谁,然后最后一行有五个点分别对应着五个位,也就是上面的五行,分别表示:1.表示分钟(0-59)2.表示小时(0-23)3.表示日期(1-31)4.表示月份(1-12可以写数字或者英文的简写)5.表示星期(0-6,0或者7表示周日,也可以写

Linux centos7 linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、 target介绍

一.linux任务计划cron crontab -u  -e -l -r 格式;分 时 日 月 周 user command 文件/var/spool/corn/username 分范围0-59,时范围0-23,日范围0-31,月范围0-12,周1-6 可用格式1-5表示一个范围1到5 可用格式1,2,3表示1或2或3 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时 启动 systemctl etop crond停止 systemctl start crond.service  二

linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍

linux任务计划cron [[email protected] ~]# crontab -e no crontab for root - using an empty one 每天的凌晨3点 执行这个 0 3 * * * /usr/bin/touch /root/123.txt &>/dev/null 启动服务 [[email protected] ~]# systemctl start crond 基本格式 : * * * * * command 分 时 日 月 周 命令 第1列表示分钟

10.23 linux任务计划cron 10.24 chkconfig工具 10.25 system

八周一次课 10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍 10.23 linux任务计划cron 10.24 chkconfig工具 显示chkconfig服务 系统启动服务在inittab 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍 原文地址:http://blog.51cto.com/wbyyy/2066113

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target

                  Linux 任务计划 cron 任务计划的配置文件: /etc/crontab (图中:MAILTO 表示发送任务计划给谁) crontab: -u:表示指定某个用户,不加-u则为当前用户 -e :表示制定计划任务(进入到编辑模式) -l :列出计划任务 -r: 删除计划任务 进入编辑模式 : crontab -e (编写任务计划crond服务是如果执行一个bash/命令,写它的绝对路径) 指定执行范围:   ( 分范围0-59,时范围0-23,日范围1-31

cron计划任务、chkconfig工具、systemd管理服务、unit、target介绍

1. linux任务计划cron linux的大部分系统管理工作都是通过定期自动执行某个脚本来完成的,那么如何定期执行某个脚本呢?这就要借助linux的cron功能了,这部分的内容很重要,请牢记! 命令crontab linux的任务计划功能的操作都是通过crontab命令来完成的,其常用的选项有以下几个: -u:表示指定某个用户,不加-u选项则为当前用户. -e:表示制定计划任务 -l:表示列出计划任务 -r:表示删除计划任务 任务计划的配置文件: # cat /etc/crontab SHE