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

1. linux任务计划cron

linux的大部分系统管理工作都是通过定期自动执行某个脚本来完成的,那么如何定期执行某个脚本呢?这就要借助linux的cron功能了,这部分的内容很重要,请牢记!

命令crontab

linux的任务计划功能的操作都是通过crontab命令来完成的,其常用的选项有以下几个:

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

-e:表示制定计划任务

-l:表示列出计划任务

-r:表示删除计划任务

任务计划的配置文件:

# 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

解释:

格式:分 时 日 月 周 user command

分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7(0表示周日,也可以用7表示,也都可以用英文简写sun,mon,tue,wed,thu,fri,sat)

可用格式1-5表示一个范围1到5,如1-5月或者1-5点等。

可用格式1,2,3表示1或者2或者3.如1,2,3月份或者1点,2点,3点或者星期一,星期二,星期三。

可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时;月份,那就是偶数月2月,4月,6月等。

使用 crontab -e 来进行编写任务计划,这实际上是使用vim工具打开了crontab的配置文件。crontab -e 是打开了目录文件“/var/spool/cron/username” (如果是root则打开的是/var/spool/cron/root)这个文件,保存时:wq即可,但是请千万不要直接去编辑那个文件,否则会出错,一定要使用命令crontab -e来编辑

启动计划任务服务命令:

systemctl start crond

停止任务计划服务命令:

systemctl stop crond

检查服务是否启动命令:

systemctl status crond 或者查看进程启动命令ps aux |grep cron

# systemctl status crond

● crond.service - Command Scheduler

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

Active: active (running) since 四 2017-11-30 14:00:30 CST; 4 days ago

Main PID: 506 (crond)

CGroup: /system.slice/crond.service

└─506 /usr/sbin/crond -n

1月 29 14:00:30 ccj systemd[1]: Started Command Scheduler.

1月 29 14:00:30 ccj systemd[1]: Starting Command Scheduler...

1月 29 14:00:30 ccj crond[506]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 96% if used.)

1月 29 14:00:30 ccj crond[506]: (CRON) INFO (running with inotify support)

# ps aux |grep cron

root        506  0.0  0.1 126236  1612 ?        Ss   1月29   0:01 /usr/sbin/crond -n

root       8380  0.0  0.0 112676   972 pts/0    S+   20:22   0:00 grep --color=auto cron

2. chkconfig工具

windows系统有开机启动项,linux也同样有开机启动项。在centos6上的开机启动项管理工具为chkconfig,所有的开机启动服务都可以在/etc/init.d/目录看到。但是centos7已经不在用chkconfig管理开机启动项。

# ls /etc/init.d/

functions  netconsole  network  README

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

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

在这里我们会看到一个提示,它提示我们该命令输出的内容并没有包含Centos 7原生systemd服务,而这里仅仅列出来SysV服务,这也是/etc/init.d/目录下面只有一两个启动脚本的根本原因。也就是说,早期CentOS版本(7之前)采用的服务管理都是Sysv,而7换成了systemd。

运行级别为系统启动级别,具体含义如下:

0 shutdown关机

1 单用户模式

2 无NFS支持的多用户模式

3 完全多用户模式,常用的命令行模式

4 保留给用户自定义

5 图形界面登录,比3多了一个图形界面

6 重启

运行级别0,1,6作为保留模式。通常做设置时,如果不指定级别,则默认为更改2,3,4,5。

# chkconfig --level 3 network off    关闭3级别的network服务

# chkconfig --list                           查看结果,3级别已关闭

注意:该输出结果只显示 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 --level 345 network off     关闭3,4,5级别的network服务

# chkconfig --list                               查看结果,3、4、5级别已关闭

注意:该输出结果只显示 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 network on        省略级别,默认是针对2、3、4、5操作

# 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 --del network      删除network服务

# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。

如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。

欲查看对特定 target 启用的服务请执行

'systemctl list-dependencies [target]'。

netconsole      0:关 1:关 2:关 3:关 4:关 5:关 6:关

# chkconfig --add network    添加network服务

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

3. systemd管理服务

列出系统所有服务

# systemctl list-units --all --type=service

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

crond.service                          loaded    active   running Command Scheduler

dbus.service                           loaded    active   running D-Bus System Message Bus

● display-manager.service                not-found

# ls /usr/lib/systemd/system      启动服务对应的脚本文件目录

arp-ethers.service                      lvm2-lvmetad.service                swap.target

auditd.service                          lvm2-lvmetad.socket                 sys-fs-fuse-connections.mount

[email protected]                         lvm2-lvmpolld.service               sysinit.target

basic.target                            lvm2-lvmpolld.socket                sysinit.target.wants

basic.target.wants                      lvm2-monitor.service                sys-kernel-config.mount

blk-availability.service                [email protected]                sys-kernel-debug.mount

bluetooth.target                        machine.slice                       syslog.socket

brandbot.path                           machines.target                     syslog.target.wants

brandbot.service                        messagebus.service                  sysstat.service

几个常用的服务相关的命令

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

systemctl disable crond              不让开机启动

systemctl status crond               查看状态

systemctl stop crond                 停止服务

systemctl start crond                 启动服务

systemctl restart crond              重启服务

systemctl is-enabled crond        检查服务是否开机启动

4. unit介绍

/usr/lib/systemd/system 此目录下列出了很多文件,这些文件都是unit。

系统所有unit,分为类别如下:

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 --type=service         列出状态为active的service

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

5. target介绍

target类似于centos6的启动级别,但target内又支持多个target同时启动,target其实是多个unit的组合。启动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

ctrl-alt-del.target       disabled

default.target            enabled

emergency.target          static

final.target              static

getty.target              static

graphical.target          static

halt.target               disabled

hibernate.target          static

hybrid-sleep.target       static

initrd-fs.target          static

initrd-root-fs.target     static

initrd-switch-root.target static

initrd.target             static

iprutils.target           disabled

kexec.target              disabled

local-fs-pre.target       static

local-fs.target           static

machines.target           disabled

multi-user.target         enabled

network-online.target     static

network-pre.target        static

network.target            static

...省略

查看指定target下面有哪些unit

# systemctl list-dependencies multi-user.target

multi-user.target

● ├─auditd.service

● ├─brandbot.path

● ├─chronyd.service

● ├─crond.service

● ├─dbus.service

● ├─irqbalance.service

● ├─kdump.service

● ├─network.service

● ├─NetworkManager.service

● ├─plymouth-quit-wait.service

● ├─plymouth-quit.service

● ├─postfix.service

● ├─rsyslog.service

● ├─sshd.service

● ├─sysstat.service

● ├─systemd-ask-password-wall.path

● ├─systemd-logind.service

● ├─systemd-readahead-collect.service

● ├─systemd-readahead-replay.service

● ├─systemd-update-utmp-runlevel.service

● ├─systemd-user-sessions.service

● ├─tuned.service

● ├─vmtoolsd.service

● ├─basic.target

● │ ├─firewalld.service

● │ ├─microcode.service

...省略

查看系统默认的target

# systemctl get-default

multi-user.target

设置默认的target

# systemctl set-default multi-user.target

Removed symlink /etc/systemd/system/default.target.

Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

运行级别上面提到的multi-user.target等同于centos6的运行级别3。其实还有其他几个target对应0-6运行级别,如下所示:service、unit以及target之间的联系

一个service属于一种unit;

多个unit一起组成了一个target;

一个target里面包含了多个service,你可以查看/usr/lib/systemd/system/sshd.service里面【install】部分的内容,它就定义了该service属于哪一个target。
# cat /usr/lib/systemd/system/sshd.service[Unit]Description=OpenSSH server daemonDocumentation=man:sshd(8) man:sshd_config(5)After=network.target sshd-keygen.serviceWants=sshd-keygen.service[Service]Type=forkingPIDFile=/var/run/sshd.pidEnvironmentFile=/etc/sysconfig/sshdExecStart=/usr/sbin/sshd$OPTIONSExecReload=/bin/kill -HUP$MAINPIDKillMode=processRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target

原文地址:http://blog.51cto.com/ccj168/2066674

时间: 2024-08-05 01:59:18

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

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

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