八周1课 任务计划cron,chkconfig工具,systemd管理服务,unit,target

linux任务计划
在linux中,任务计划是必不可少的,在linux中怎样设置任务计划呢?
首先看一个文件
[[email protected] ~]# 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 (星期,0代表周日,也可以写成7,也可以写英文简写)

| | | | |

* user-name(用户) command to be executed (要执行的命令)

这就是任务计划的配置文件,在这个配置文件中他会定义几个变量SHELL=/bin/bash、PATH=/sbin:/bin:/usr/sbin:/usr/bin、MAILTO=root(发送邮件谁)

在他下面的是格式。我们可以看到他下面有五个星号,所以分为五个位。

用命令crontab -e,这样就进入了配置文件当中。它的用法和vim相同。

比如现在有一项任务,需要在凌晨3点去执行
0 3 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
这里面的“
”代表所有的意思,第一个代表“分”,第二个代表“时”,第三个代表“日”。所以这是每天都执行。
然后后面接一个shell脚本,这个脚本的意思是每天把正确内容追加到一个指定的文件,错误的也追加到指定的文件。
任务计划的格式很简单,分时日月周后接具体的命令。

同时我们也可以用一个范围去执行。
还是上面的那个脚本。
0 3 1-10 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
它的意思是每双月的1-10日凌晨三点执行这个脚本。
但是我们发现这里面没有年份,如果我们想那一年执行一个脚本,这里我们就用星期去确定它的唯一性。比如今年的6月18日和明年的6月18日所在的周肯定也不一样。

想让任务计划crontab正常使用,我们还要去启动服务,那么怎样去启动服务呢?
systemctl start crond输入这条命令,他就启动了。
然后我们检查一下他有没有启动
[[email protected] ~]# ps -aux |grep cron
root 560 0.0 0.1 126236 1656 ? Ss 22:37 0:00 /usr/sbin/crond -n
root 1253 0.0 0.0 112676 984 pts/0 S+ 23:37 0:00 grep --color=auto cron
如果出现了含有cron的字符串,就证明他已经启动了。

我们也可以用systenctl status crond来查看
[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running)(如果这里是绿色的,就证明他已经启动了,如果没有颜色,就证明它没有启动) since 日 2018-03-25 22:37:48 CST; 1h 2min ago
Main PID: 560 (crond)
CGroup: /system.slice/crond.service
└─560 /usr/sbin/crond -n

3月 25 22:37:48 localhost.localdomain systemd[1]: Started Command Scheduler.
3月 25 22:37:48 localhost.localdomain systemd[1]: Starting Command Scheduler...
3月 25 22:37:48 localhost.localdomain crond[560]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 18% if used.)
3月 25 22:37:49 localhost.localdomain crond[560]: (CRON) INFO (running with inotify support)

还有一种情况,写了一个计划,放到了配置文件中,但是没有执行,这可能是写的脚本中,用的是一个命令,而没有用绝对路径,这就容易造成命令不执行的现象。因为你用的这个命令并没有在他的PATHZ中,所以解决的方法是要么写绝对路径,要么就将命令的路径加入到PATH。

还有一个建议就是,我们每写一个任务计划,后面就追加一个任务日志,正确输出和错误输入都要写上,这样才能保证我们的任务有据可查,如果不执行,是不是有错误,我么只要查看任务日志就能知晓。

任务日志的备份
[[email protected] ~]# crontab -l
1 10 2 /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
这样在我们写完任务计划后就能查看了。
它的问价在/var/spool/cron/这里面会有对应用户的crond,他会以用户名字作为结尾,形成一个文件。所以我们如果要备份,直接备份这个文件就可以,或者将这个目录整个拷贝一下就行。

如果我们想删除一个任务计划,就输入crontab -r
我们也可以指定一个用户,比如我们指定root用户。crontab -u root -l

linux系统服务管理-chkconfig
linux中有很多服务,比如iptables、crontab、firewalld等等都是服务,这么多的服务就需要有一个工具去管理。管理他怎样启动,怎样开机启动,怎么样让他在指定的级别启动。

列出chkconfig所有服务
chkconfig --list
[[email protected] ~]# 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:关

chkconfig服务变更
chkconfig 服务名 off/on(关闭或者打开)
比如我们现在要关闭chkconfig中的network
chkconfig --list
[[email protected] ~]# chkconfig --list
然后我们查询一下
chkconfig --list
network 0:关 1:关 2:关 3:关 4:关 5:关 6:关
这样在6个级别就全部关闭network了。
其中这6个数字的的含义是:0是关机状态,1是单用户状态,2是多用户,但是没有NFS、3是多用户模式,4是暂时没用,5是多用户的图形模式,6是重启。

指定某一个级别的关闭或者开启。
例如我们把3级别关闭
[[email protected] ~]# chkconfig --level network 3 off
然后我们查询一下
[[email protected] ~]# chkconfig --list
network 0:关 1:关 2:开 3:关 4:开 5:开 6:关
这样network在3级别就关闭了。

如果我想在多个级别上关闭服务,就直接将级别的序号写上,必要用“,”隔开
[[email protected] ~]# chkconfig --level 35 network off
[[email protected] ~]# chkconfig --list

network 0:关 1:关 2:开 3:关 4:开 5:关 6:关
这样就在3,5级别将network关闭了。

将脚本加入到服务列表中
chkconfig --add 脚本名
比如我们要将123加入到chkconfig的服务列表中
[[email protected] init.d]# chkconfig --add 123
然后我们查看一下
123 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:关 4:开 5:关 6:关
这样就添加了一项服务。

这时怎样做到的呢?首先我们要将服务脚本放到init.d这个目录下,只有放在这个目录中,我们才能添加到服务中去。名字无所谓,但是文件内容有格式。

#! /bin/bash(必须是shell脚本)
#

network Bring up/down networking

#

chkconfig: 2345 10 90(第一组数字是运行级别,第二组是运行级别启动顺序,第三组是关闭顺序)

description: Activates/Deactivates all network interfaces configured to \

start at boot time.(描述)

#
以上的内容是脚本必须要有的

将脚本从chkconfig列表中删除。
chkconfig --del
我们还用123做例子
[[email protected] init.d]# chkconfig --del 123
然后查询
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:关 4:开 5:关 6:关
这样名为123的服务就没有了。

linux系统管理服务-systemd
它是centos7的管理机制。centos6主要使用chkconfig。

查看所有服务
systemctl list-unit-files
[[email protected] init.d]# systemctl list-unit-files|head -10
UNIT FILE STATE
proc-sys-fs-binfmt_misc.automount static
dev-hugepages.mount static
dev-mqueue.mount static
proc-sys-fs-binfmt_misc.mount static
sys-fs-fuse-connections.mount static
sys-kernel-config.mount static
sys-kernel-debug.mount static
tmp.mount disabled
brandbot.path disabled
这样就查看了所有服务

还有回个命令,也可以查看,而且更加的清晰。
[[email protected] init.d]# systemctl list-units --all --type=service|head -5
UNIT LOAD ACTIVE SUB DESCRIPTION
auditd.service loaded active running Security Auditing Service
brandbot.service loaded inactive dead Flexible Branding Service
chronyd.service loaded active running NTP client/server
cpupower.service loaded inactive dead Configure CPU power related settings

开机启动
systemctl enable 服务名

开机不启动
systemctl disable 服务名

查看状态
systemctl status crond

启动服务
systemctl status 文件名

停止服务
systemctl stop 文件名

检查服务是否开机启动
systemctl is-enabled 文件名

unit的概念
systemd开启和监督整个系统是基于unit的概念。unit是由一个与配置文件名同名的名字和类型组成的(例如:avahi.service unit有一个具有相同名字的配置文件,它是守护进程avahi的一个封装单元)。

主要分类:
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组 snapshot systemd快照
socket 进程间通信套接字
swap swap文件
timer 定时器

unit相关的命令

列出正在运行的unit
systemctl list-units

列出所有的unit,包括失败的或者inactive的
systemctl list-units -all

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

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

查看某个服务是否为active
systemctl is-active crond.service

target概念
target就是多个unit的组合,?系统为了方便管理用target来管理unit。

列出系统中所有的target
systemctl list-unit-files --type=target

UNIT FILE STATE
basic.target static
bluetooth.target static
cryptsetup-pre.target static
cryptsetup.target static

查看指定target下面有哪些unit
systemctl list-dependencies multi-user.target

查看系统默认的target
systemctl get-default
[[email protected] init.d]# systemctl get-default
multi-user.target
这既是系统默认的target

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

原文地址:http://blog.51cto.com/13067688/2091420

时间: 2024-08-25 04:52:36

八周1课 任务计划cron,chkconfig工具,systemd管理服务,unit,target的相关文章

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介绍、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列表示分钟

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

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

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表示周日,也可以写

十(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

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

三十四.Linux系统任务计划cron.chkconfig工具.systemd管理服务.unit介绍.target介绍 一.Linux系统任务计划cron crontab命令:对任务计划功能的操作用此命令.选项: -u:指定某个用户,不加-u则为当前用户. -e:制定任务计划. -l:列出任务计划. -r:删除任务计划. 任务计划的配置文件:/etc/crontab 文件内共有五个字段. 从左往右依次为:分.时.日.月.周.用户.命令. 可以不指定用户就是root. # crontab -e  

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  二

10.23 linux任务计划cron10.24chkconfig工具10.25 systemd管理服务10.26 unit介绍 10.27 target介绍

- 10.23 linux任务计划cron - 10.24 chkconfig工具 - 10.25 systemd管理服务 - 10.26 unit介绍 - 10.27 target介绍 - 扩展 1. anacron http://blog.csdn.net/strikers1982/article/details/4787226  2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_46