任务计划crontab、服务管理(chkconfig、systemd)

任务计划

crontab计划任务文件
任务计划文件路径/var/spool/cron/

cat /etc/crontab

[[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
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
[[email protected] ~]#

格式:分钟 小时 日 月 周 user command

范围:
分钟:0-59;
小时:0-23;
日:0-31;
月:0-12;
周:0-6,0为周日、或7为周日;

可用格式:
*:表示所有;
1-5:表示范围为1到5;
1,2,3:表示1 2 3;*
/2:表示被2整除的数字,比如小时,那就是没隔2小时;**

举例:


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

0分 3点 1到10日 偶数月份 星期2或星期5 使用/bin/bash的内核shell 执行脚本123.sh无论正确错误都将输入123.log日志;
当偶数月份的1到10日是星期2或星期5的时候,那么就在凌晨3:00执行后面这个脚本;

新建或编辑模式:

crontab -e

查看内容:

crontab -l

[[email protected] ~]# crontab -l
0 3 1-10 */2 2,5  /bin/bash  /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log
[[email protected] ~]#

cat /var/spool/cron/root

[[email protected] ~]# cat /var/spool/cron/root
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[[email protected] ~]#

启动服务:

systemctl start crond

查询服务是否启动:

ps aux |grep crond

[[email protected] ~]# ps aux |grep crond
root       543  0.0  0.1 126268  1664 ?        Ss   14:47   0:00 /usr/sbin/crond -n
root      1372  0.0  0.0 112676   980 pts/0    R+   16:34   0:00 grep --color=auto crond
[[email protected] ~]#

或者使用命令
systemctl status crond
显示结果: Active: active (running)

[[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-01-29 14:47:53 CST; 1h 47min ago
Main PID: 543 (crond)
   CGroup: /system.slice/crond.service
           └─543 /usr/sbin/crond -n
1月 29 14:47:53 shu-test systemd[1]: Started Command Scheduler.
1月 29 14:47:53 shu-test systemd[1]: Starting Command Scheduler...
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 29% if used.)
1月 29 14:47:53 shu-test crond[543]: (CRON) INFO (running with inotify support)
1月 29 16:33:01 shu-test crond[543]: (root) RELOAD (/var/spool/cron/root)
[[email protected] ~]#

服务管理-chkconfig工具

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

[[email protected] ~]# ls /etc/init.d
functions  netconsole  network  README
[[email protected] ~]#

查看服务

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:关
[[email protected] ~]#

级别(0~6):为系统启动级别,0、1、6被系统保留。可以参照init命令;
0:为shutdown动作;
1:作为重启到单用户模式;
6:为重启;
2:无NFS支持的多用户模式;
3:完全多用户模式;
4:自定义级别;
5:图形界面;

关闭级别:

chkconfig --level 3 network off
关闭network的3级别


[[email protected] ~]# chkconfig --level 3 network off
[[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:关
[[email protected] ~]#

开启级别:

chkconfig --level 3 network on
开启network服务的3级别

[[email protected] ~]# chkconfig --level 3 network on
[[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:关
[[email protected] ~]#

增加自定义服务

chkconfig --add 123
将定义好的服务123添加到启动服务中,使用vim就行编辑123文件;

[[email protected] ~]# cd /etc/init.d/
[[email protected] init.d]# ls
functions  netconsole  network  README
[[email protected] init.d]# cp network 123
[[email protected] init.d]# ls
123  functions  netconsole  network  README
[[email protected] init.d]#
[[email protected] init.d]# chkconfig --add 123
[[email protected] init.d]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
      要列出 systemd 服务,请执行 ‘systemctl list-unit-files‘。
      查看在具体 target 启用的服务请执行
      ‘systemctl list-dependencies [target]‘。
123                0:关    1:关    2:开    3:开    4:开    5:开    6:关
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:开    3:开    4:开    5:开    6:关
[[email protected] init.d]#

删除自定义服务

chkconfig --del 123
删除123服务

[[email protected] init.d]# chkconfig --del 123
[[email protected] init.d]# 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:关
[[email protected] init.d]#

systemd管理服务

用于CentOS 7系统下、CentOS 6系统常用chkconfig工具;

列出系统所有的服务


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

让服务开机启动


systemctl enable crond.service

不让服务开机启动


systemctl disable crond

查看状态

systemctl status crond

[[email protected] init.d]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2018-01-29 16:37:09 CST; 3h 54min ago
Main PID: 1427 (crond)
   CGroup: /system.slice/crond.service
           └─1427 /usr/sbin/crond -n
1月 29 16:37:09 shu-test systemd[1]: Started Command Scheduler.
1月 29 16:37:09 shu-test systemd[1]: Starting Command Scheduler...
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 8% if used.)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (running with inotify support)
1月 29 16:37:09 shu-test crond[1427]: (CRON) INFO (@reboot jobs will be run at computer‘s startup.)
1月 29 16:47:01 shu-test crond[1427]: (root) RELOAD (/var/spool/cron/root)
[[email protected] init.d]#

停止服务


systemctl stop crond

启动服务


systemctl start crond

重启服务


systemctl restart crond

检查服务是否设置开机启动

systemctl is-enabled crond

[[email protected] init.d]# systemctl is-enabled crond
enabled
[[email protected] init.d]#

ls /usr/lib/systemd/system

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

unit相关

列出正在运行的unit


systemctl list-units

列出所有的unit


systemctl list-units --all

列出所有inactive的unit


systemctl list-units --all --state=inactive

列出所有状态的service


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

列出状态为active的service


systemctl list-units --type=service

查看某个unit是否active


systemctl is-active crond.service

target

target类似于centos6里面启动级别,target支持多个target同时启动。target其实是多个unit的组合;

查看当前系统的所有target


systemctl list-unit-files --type=target

查看一个target包含的所有unit


systemctl list-dependencies multi-user.target

查看系统默认的target

systemctl get-default

[[email protected] init.d]# systemctl get-default
multi-user.target
[[email protected] init.d]#

设置默认的target


systemctl set-default multi-user.target

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

查看服务文件

[[email protected] init.d]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
[[email protected] init.d]#

原文地址:http://blog.51cto.com/shuzonglu/2066587

时间: 2024-08-28 11:33:55

任务计划crontab、服务管理(chkconfig、systemd)的相关文章

Linux服务管理之systemd简介

近年来,Linux系统的init进程经历了两次重大的演进,传统的sysinit已经逐渐淡出历史舞台,新的upstart和systemd各有特点,越来越多的Linux发行版采纳了systemd.RHEL 7.0也开始使用systemd. systemd是Linux系统中最新的初始化系统(init),它的主要设计目标是克服sysinit固有的缺点,提高系统的启动速度.systemd和Ubuntu的upstart是竞争对手,预计会取代upstart,已经有消息称ubuntu也将采用systemd作为其

crontab命令、chkconfig、systemd命令

一:crontab命令 -u:指定某个用户,不加-u则为当前用户.-e:指定计划任务-l:列出计划任务-r:删除计划任务使用crontab -e来进行编写任务计划,实际上是使用vim工具打开了crontab的配置文件/var/spool/cron/username,如果是root,打开的就是/var/spool/cron/root,但是千万不能直接去编辑这个文件,可能会出错.01 10 05 06 3 echo "ok" > /root/cron.log从左到右分别为分,时,日,

服务管理/磁盘引导区恢复 笔记

Linux运行级别: 单人模式:提供系统维护.找回root密码: 纯文字的多人网络模式: 图形界面模式: ? 系统服务:提供某些常驻性功能的运行程式.这些程式运行时常驻在内存.服务名称后都有一个d字母. 独立服务和非独立服务? 独立服务:服务是独立启动的,并常驻于内存,运行速度快. 非独立服务:xinted管理程序提供的socket或port对应的管理.它会根据用户要求启动相应的服务程序,请求结束就会关闭此程序. ? 早期的服务启动: SysV的init脚本程序管理特点. 所有服务的启动脚本都在

linux任务计划、 系统服务管理工具chkconfig、systemd

linux任务计划 系统服务管理工具chkconfig 系统服务管理工具systemd 原文地址:http://blog.51cto.com/13515599/2069079

计划任务及服务管理

Linux任务计划 任务计划的配置文件及详解 [[email protected] ~]# cat /etc/crontab SHELL=/bin/bash ##定义bash PATH=/sbin:/bin:/usr/sbin:/usr/bin ##定义环境变量,命令的路径 MAILTO=root ##定义接受邮件的用户 # For details see man 4 crontabs # Example of job definition: # .---------------- minute

了解systemctl和chkconfig的服务管理工具

crontab计划任务 crontab计划任务的计时方式:对大部分时间单位都清楚的划分,具体可以划分到分钟,时间单位有分钟,小时,日(一个月内的第几天),月,周(一周的第几天,计划任务中可以是指定为每隔几周来执行某些任务) [[email protected] ~]# cat /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs #

Linux系统任务计划和系统服务管理

一.Linux系统的任务计划 crontab命令被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务.关于cron任务计划功能的操作都是通过crontab这个命令来完成的.常用的选项有: -u :指定某个用户,不加-u选项则为当前用户:-e :制定计划任务:-l :列出计划任务:-r :删除计划任务. 实例:1.

Linux学习总结(二十七)任务计划,系统服务管理

1 任务计划 说白了就是运行命令或者脚本的一个定时器,他可以让我们在休息时间自动给我们执行任相关任务.来看下它的配置文件:cat /etc/crontab第一行定义了 shell环境第二行定义 环境变量第三行定义了发邮件给root用户,邮件保存路径为/var/spool/mail/root/最后定义了任务内容的格式,从左到右依次为:分 时 日 月 周 命令分钟:0-59小时:0-23日期:1-31月份:1-12周:0-7 0,7都表示星期天可以指定一个范围:比如1-5 周一到周五, 也可以单独指

10.23-10.27 corn, chkconfig, systemd, unit, target

八周一次课(3月26日)10.23 linux任务计划cron10.24 chkconfig工具10.25 systemd管理服务10.26 unit介绍10.27 target介绍扩展1. anacron  http://blog.csdn.net/strikers1982/article/details/47872262. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)   http://blog.sina.com.cn/s/blog_465bbe6