10.23 linux任务计划cron chkconfig工具 system

简介:

10.23 linux任务计划cron

crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

语法: crontab [options]
Options:
-e:=edit 编辑用户的计时器设置
-l:=list 列出用户的计时器设置
-r:=remove 删除用户的计时器设置
-u:=user 指定设定计时器的用户

配置计划任务

crontab的配置文件: /etc/crontab

[root@adai003 grep]# 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

设定计划任务

eg:

[root@adai003 grep]# crontab -e
0 3 * /bin/bash /user/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

0 3 1-10 */2 2,5 /bin/bash /user/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
解析:

第一条命令:
每天凌晨3点(*位置不指定数字就代表每天、月、周),当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

第二条命令:
每个偶数月(*/2:表示能被2整除)1号到10号的周二和周五的凌晨3点,当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

启动crond服务/查看服务状态

配置完成后需要启动crond服务:

启动服务:
[root@adai003 grep]# systemctl start crond

查看crond服务状态:
方法1:
[root@adai003 grep]# systemctl status crond.service 
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2017-07-17 10:05:11 CST; 4h 48min ago
Main PID: 501 (crond)
……
方法2:
[root@adai003 grep]# ps aux |grep crond
root 501 0.0 0.1 126224 1624 ? Ss 10:05 0:00 /usr/sbin/crond -n

停止crond服务: 
[root@adai003 grep]# systemctl stop crond.service
注意: 在编写配置文件或者shell脚本时,所有的命令都要使用绝对路径;每个计划任务追加一个日志。

查看现有的计划任务

[root@adai003 grep]# crontab -l
no crontab for root
计划任务存放位置: /var/spool/cron/,所有的计划任务存放在该目录下以用户名命名的文件中,备份时可以使用该文件。

删除计划任务

[root@adai003 grep]# crontab -r
注: 以上所有操作都可以附加-u选项来指定用户。

10.24 chkconfig工具

chkconfig命令检查、设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接(该命令多用于centos6及以前版本)。

语法: chkconfig [options]
Options:
--list:查看在使用chkconfig命令的服务的状态
--add:增加指定服务
--del:删除指定服务
--level:指定某系统服务要在系统某运行级别中开启或关毕。

应用:

chkconfig --list 查看当前系统服务状态
[root@adai003 grep]# 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:关
服务所在位置:/etc/init.d/

[root@adai003 grep]# ls /etc/init.d/
functions netconsole network README
更改服务状态

更改服务所有状态:
[root@adai003 grep]# chkconfig network off/on
功能服务在某一运行级别的状态:
[root@adai003 grep]# chkconfig --level 345 network off/on
运行级别配置文件:“/etc/inittab”,centos7已不再使用该文件。

添加/删除服务
首先,在添加服务之前必须把该服务的脚本放到“/etc/init.d/”目录下并添加执行权限。然后执行命令:

[root@adai003 ~]# ls /etc/init.d/
123 functions netconsole network README

添加/删除:
[root@adai003 ~]# chkconfig --add /etc/init.d/123
[root@adai003 ~]# chkconfig --list

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

[root@adai003 init.d]# chkconfig --del /etc/init.d/123
注: 关于该服务脚本
mark

10.25 systemd管理服务

systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一起。

systemctl命令

mark

[root@adai003 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@adai003 ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
说明: 伴随某服务的开/关会建立/删除一个指向该服务的软链接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”

扩展:systemd添加自定义系统服务

节选于:http://www.jb51.net/article/100457.htm

设置自定义开机启动的方法:

1、服务权限

systemd有系统和用户区分:系统(/user/lib/systemd/system/),用户(/etc/lib/systemd/user/)。
一般系统管理员手工创建的单元文件建议存放在/etc/systemd/system/目录下面。

2、创建服务文件

[Unit] 
Description=nginx - high performance web server 
Documentation=http://nginx.org/en/docs/ 
After=network.target remote-fs.target nss-lookup.target

[Service] 
Type=forking 
PIDFile=/run/nginx.pid 
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf 
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf 
ExecReload=/bin/kill -s HUP $MAINPID 
ExecStop=/bin/kill -s QUIT $MAINPID 
PrivateTmp=true

[Install] 
WantedBy=multi-user.target 
[Unit]

Description : 服务的简单描述

Documentation : 服务文档

Before、After:定义启动顺序。Before=xxx.service,代表本服务在xxx.service启动之前启动。After=xxx.service,代表本服务在xxx.service之后启动。

Requires:这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,这个单元也停止了。

Wants:推荐使用。这个单元启动了,它需要的单元也会被启动;它需要的单元被停止了,对本单元没有影响。

[Service]

Type=simple(默认值):systemd认为该服务将立即启动。服务进程不会fork。如果该服务要启动其他服务,不要使用此类型启动,除非该服务是socket激活型。

Type=forking:systemd认为当该服务进程fork,且父进程退出后服务启动成功。对于常规的守护进程(daemon),除非你确定此启动方式无法满足需求,使用此类型启动即可。使用此启动类型应同时指定 PIDFile=,以便systemd能够跟踪服务的主进程。

Type=oneshot:这一选项适用于只执行一项任务、随后立即退出的服务。可能需要同时设置 RemainAfterExit=yes 使得 systemd 在服务进程退出之后仍然认为服务处于激活状态。

Type=notify:与 Type=simple 相同,但约定服务会在就绪后向 systemd 发送一个信号。这一通知的实现由 libsystemd-daemon.so 提供。

Type=dbus:若以此方式启动,当指定的 BusName 出现在DBus系统总线上时,systemd认为服务就绪。

Type=idle: systemd会等待所有任务(Jobs)处理完成后,才开始执行idle类型的单元。除此之外,其他行为和Type=simple 类似。

PIDFile:pid文件路径

ExecStart:指定启动单元的命令或者脚本,ExecStartPre和ExecStartPost节指定在ExecStart之前或者之后用户自定义执行的脚本。Type=oneshot允许指定多个希望顺序执行的用户自定义命令。

ExecReload:指定单元停止时执行的命令或者脚本。

ExecStop:指定单元停止时执行的命令或者脚本。

PrivateTmp:True表示给服务分配独立的临时空间

Restart:这个选项如果被允许,服务重启的时候进程会退出,会通过systemctl命令执行清除并重启的操作。

RemainAfterExit:如果设置这个选择为真,服务会被认为是在激活状态,即使所以的进程已经退出,默认的值为假,这个选项只有在Type=oneshot时需要被配置。

[Install]

Alias:为单元提供一个空间分离的附加名字。

RequiredBy:单元被允许运行需要的一系列依赖单元,RequiredBy列表从Require获得依赖信息。

WantBy:单元被允许运行需要的弱依赖性单元,Wantby从Want列表获得依赖信息。

Also:指出和单元一起安装或者被协助的单元。

DefaultInstance:实例单元的限制,这个选项指定如果单元被允许运行默认的实例。

3、重载服务

systemctl enable nginx.service

就会在/etc/systemd/system/multi-user.target.wants/目录下新建一个/usr/lib/systemd/system/nginx.service 文件的链接。

4、操作服务

#启动服务 
$ sudo systemctl start nginx.service

#查看日志 
$ sudo journalctl -f -u nginx.service 
— Logs begin at 四 2015-06-25 17:32:20 CST. — 
6月 25 10:28:24 Leco.lan systemd[1]: Starting nginx – high performance web server… 
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok 
6月 25 10:28:24 Leco.lan nginx[7976]: nginx: configuration file /etc/nginx/nginx.conf test is successful 
6月 25 10:28:24 Leco.lan systemd[1]: Started nginx – high performance web server. 
#重启 
$ sudo systemctl restart nginx.service 
#重载 
$ sudo systemctl reload nginx.service 
#停止 
$ sudo systemctl stop nginx.service
10.26 unit介绍

unit所在目录: /usr/lib/systemd/system/

unit文件类型

mark

unit相关命令

mark

10.27 target介绍

系统为了方便管理,所以使用target来管理unit。

相关操作:

mark

target、service、unit关系

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

查看一个service属于哪个target:

[root@adai003 system]# cat /usr/lib/systemd/system/sshd.service 
看Install部分!
………










本文转自 芬野 51CTO博客,原文链接:http://blog.51cto.com/yuanhaohao/2047188,如需转载请自行联系原作者
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
目录
相关文章
|
10天前
|
监控 Unix Linux
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
Linux操作系统调优相关工具(四)查看Network运行状态 和系统整体运行状态
26 0
|
17天前
|
存储 前端开发 Linux
Linux系统之部署ToDoList任务管理工具
【4月更文挑战第1天】Linux系统之部署ToDoList任务管理工具
61 1
|
18天前
|
存储 传感器 运维
linux系统资源统计工具
【4月更文挑战第1天】Linux系统监控工具如dstat、htop、glances、vmstat、top、iostat、mpstat、sar和atop,用于跟踪CPU、内存、磁盘I/O、网络和进程性能。这些工具提供实时、交互式和历史数据分析,助力管理员优化系统性能和故障排查。例如,dstat是vmstat等工具的增强版,htop提供彩色界面的进程管理,而atop则结合了多种功能并记录历史数据。
27 5
linux系统资源统计工具
|
28天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 软件包管理工具 rpm命令 使用指南
【Shell 命令集合 系统设置 】Linux 软件包管理工具 rpm命令 使用指南
46 0
|
10天前
|
Linux
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
Linux操作系统调优相关工具(三)查看IO运行状态相关工具 查看哪个磁盘或分区最繁忙?
21 0
|
15天前
|
资源调度 JavaScript 安全
Linux系统之部署web-check网站分析工具
【4月更文挑战第3天】Linux系统之部署web-check网站分析工具
63 9
|
16天前
|
运维 监控 Linux
不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
【4月更文挑战第3天】不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
29 0
不是所有的Linux工具都会让人惊叹,但这个绝对让你叫绝
|
25天前
|
缓存 Linux iOS开发
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
【C/C++ 集成内存调试、内存泄漏检测和性能分析的工具 Valgrind 】Linux 下 Valgrind 工具的全面使用指南
62 1
|
28天前
|
安全 Shell Linux
【Shell 命令集合 系统管理 】Linux 终端复用工具 screen命令 使用指南
【Shell 命令集合 系统管理 】Linux 终端复用工具 screen命令 使用指南
33 0
|
28天前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
【Shell 命令集合 系统设置 】Linux 管理系统服务 chkconfig命令 使用指南
27 0