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

可莉
• 阅读 637

10.23 linux任务计划cron

在Linux中,任务计划是必不可少的,让某条命令,脚本,在某个时间可以执行。进而减少人力值班

任务计划的配置文件

/etc/crontab
[root@aminglinux-02 ~]# 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)         //分(0到59)
# |  .------------- hour (0 - 23)               //时 (0到23)
# |  |  .---------- day of month (1 - 31)   //天   (1到31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...       //月  (1到12) 也支持英文
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat     //星期 (0到6) 0或7都是周日  也可支持英文
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed    //用户  ,不写用户默认是root+执行的命令

定义计划

crontab -e  //进入任务计划配置文件当中   ; *表示不设置

例:

0 3 * * *
表示每天的3点0分   
执行范围
例:
0 3 1-10 */2 2,5
每双数的月份的1号到10号的3点0分执行,周二和周五
*/2 表示 月份数字能被2整除的,将执行
指定某天,用 “,”逗号分隔

执行脚本 例:

/bin/bahs  /usr/local/sbin/123.sh

执行脚本 脚本所在 为了便于维护,可以做个记录日志

>> /tmp/123.log  2>>/tmp/123error.log

正确输出,将记录到123.log 日志里;错误输出,将记录到123error.log 日志里

查看任务计划

crontab -l

对应的用户的任务计划存放路径

/var/spool/cron/
root用户的就存放在 /var/spool/cron/root
user1用户的就存放在/var/spool/cron/user1

备份任务计划,只需把任务计划所在的目录拷贝一下即可

清除任务计划

crontab -r 

指定用户操作

crontab -u 

例:

crontab -u root -l

要想启动任务计划服务

systemctl start crond

确定任务计划服务启动

systemctl status crond
[root@aminglinux-02 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2017-08-18 13:29:12 CST; 18min ago    //表示已经启动
Main PID: 500 (crond)
   CGroup: /system.slice/crond.service
           └─500 /usr/sbin/crond -n
8月 18 13:29:12 aminglinux-02 systemd[1]: Started Command Scheduler.
8月 18 13:29:12 aminglinux-02 systemd[1]: Starting Command Scheduler...
8月 18 13:29:12 aminglinux-02 crond[500]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 36% if used.)
8月 18 13:29:12 aminglinux-02 crond[500]: (CRON) INFO (running with inotify support)

任务计划没有执行,可能导致的原因是脚本的里面没有使用绝对路径

10.24 chkconfig工具

Linux系统服务管理-chkconfig
在centos 6 包含6之前的版本使用的管理服务的机制教 SysV,7以后使用的机制是systemd
chkconfig 下的服务,需要有服务的脚本
所在的路径为

/etc/init.d

查看目前运行的SysV的服务

chfconfig --list

是否开机启动

chkconfig 服务名 off/on

例:

chkconfig nginx on 

默认是在2、3、4、5级别设置

运行级别
0   关机状态
1   单用户
2   多用户模式 (没有nfs服务,没有无图形服务)
3   多用户模式(无图形)
4   保留
5   多用户 (带有图形服务)
6   重启状态

修改默认开机级别(7版本,不适用)

/etc/inittab/

指定级别是否开机启动

chkconfig --level 3 nginx off

3级别开机不启动

将服务加入到开机启动

chkconfig --add 123
  1. 启动脚本,必须在“/etc/init.d”路径下
  2. 必须是一个启动脚本

删除开启启动

chkconfig --del 123

10.25 systemd管理服务

systemd 是centos 7 的系统服务管理的机制

列出所有的service 服务

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


systemctl enable crond.service //让服务开机启动
[root@aminglinux-02 ~]# systemctl enable crond
[root@aminglinux-02 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service
lrwxrwxrwx 1 root root 37 8月  18 14:29 /etc/systemd/system/multi-user.target.wants/crond.service -> /usr/lib/systemd/system/crond.service

开启启动,是将服务的文件“/usr/lib/systemd/system/crond.service”创建一个软链接到“/etc/systemd/system/multi-user.target.wants/crond.service”;

systemctl disable crond //不让开机启动


[root@aminglinux-02 ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@aminglinux-02 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service

ls: 无法访问/etc/systemd/system/multi-user.target.wants/crond.service: 没有那个文件或目录

关闭启动,是将之前创建的软链接移除

systemctl status crond //查看状态
systemctl stop crond //停止服务
systemctl start crond //启动服务
systemctl restart crond //重启服务
systemctl is-enabled crond //检查服务是否开机启动

10.26 unit介绍

ls /usr/lib/systemd/system //系统所有unit,分为以下类型 类型|描述 ---|--- service | 系统服务 target | 多个unit组成的组 device | 硬件设备 mount | 文件系统挂载点 automount | 自动挂载点 path | 文件或路径 scope | 不是由systemd启动的外部进程 slice | 进程组 snapshot | systemd 快照 socket | 进程间通信套接字 swap | swap文件 timer | 定时器

unit相关命令

列出正在运行的unit

systemctl list-units 

列出所有,包括失败的或者inactive的

systemctl list-units --all 

列出inactive的unit

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

列出状态为active的service

systemctl list-units --type=service

查看某个服务是否为active

systemct is-active crond.service

查看某个服务是否为enadled

systemct is-enadled crond.service 

10.27 target介绍

系统为了方便管理用target来管理unit

systemctl list-unit-files --type=target

查看指定target下面有哪些unit

systemctl list-dependencies multi-user.target 

查看系统默认的target

systemctl get-default 

设置系统默认的target

systemctl set-default multi-user.target  

一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

查看service属于哪个target

cat /usr/lib/systemd/system/sshd.service //看[install]部分

总结: 系统systemd这个管理机制,由多种unit组成,为了方便管理,就把它归类若干个类,每个类别称为一个target;target是由多个unit组成的,service 属于一种类型的unit,一个target里面包含了若干个service。

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
systemd创建定时任务
SystemdorCronCron是类Unix系统里最常见的任务计划程序,而Systemd也开始提供定时器作为Cron的替代品。尽管争议不断,Systemd还是被越来越多的Linux发行版使用,Ubuntu也是如此。因此在需要创建定时任务时我决定向"邪恶势力"低头,基于Systemd来实现。Systemd配置根据功
Stella981 Stella981
2年前
Linux计划任务 定时任务 Crond 配置详解 crond计划任务调试 sh
一、Crond是什么?(概述)crontab是一款linux系统中的定时任务软件用于实现无人值守或后台定期执行及循环执行任务的脚本程序,在企业中使用的非常广泛.  现在开始学习linux计划任务程序吧。crontab的优势:可以实现24小时或定期执行任务,非常高效实用,几乎是每个企业
Stella981 Stella981
2年前
Linux服务器时间同步命令
时间同步1.首先需了解linux内一任务计划工具crontabcrontab可以定时去执行你要做的动作直接用crontab命令编辑crontabu//设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数crontabl//列出某个用户cron服务的详细内容crontabr//删除某个用户的cron服务cr
Stella981 Stella981
2年前
Linux计划任务执行结果和手动执行不一致
Linux计划任务执行结果和手动执行不一致,发生原因有三种:以下是计划任务和脚本情况介绍,写出具体代码是为了说明第三种情况root@saltmaster09crontabl/1/bin/sh/root/test03/09/01
可莉 可莉
2年前
12.4 linux任务计划cron chkconfig工具 systemd管理服务 unit介绍 target介绍
10.23linux任务计划cronroot@www~cat/etc/crontabSHELL/bin/bashPATH/sbin:/bin:/usr/sbin:/usr/binMAILTOrootFordetailsseeman4crontab
Wesley13 Wesley13
2年前
MySQL定时执行脚本(计划任务)命令实例
在mysql中我们可以直接进行一些参数设置让它成定时为我们执行一些任务了,这个虽然可以使用windows或者linux中的计划任务实现,但是mysql本身也能完成查看event是否开启复制
Stella981 Stella981
2年前
Linux的定时任务
任务计划的条件:1.在未来的某个时间点执行一次某个任务(atbatch)2.周期性的执行某个任务(cron)at在指定时间执行任务_用法_at\选项参数\\时间\_选项参数_\l      查看作业\c      显示即将执行任务的细节\d      使用任务id号
Stella981 Stella981
2年前
Quartz.NET总结(一)入门
转载自:http://www.cnblogs.com/zhangweizhong/p/4874396.html前段时间,花了大量的时间,将原先的计划任务,切换到Quartz.NET来进行管理。原先的后台定时服务都是通过计划任务来实现的,但是随着业务增长,计划任务也越来越多,每个后台服务,都得创建一个计划任务。日常的维护和管理非常麻烦。  于是
Wesley13 Wesley13
2年前
MySql中的事件
一、前言自MySQL5.1.0起,增加了一个非常有特色的功能–事件调度器(EventScheduler),可以用做定时执行某些特定任务(例如:删除记录、对数据进行汇总等等),来取代原先只能由操作系统的计划任务来执行的工作。更值得一提的是MySQL的事件调度器可以精确到每秒钟执行一个任务,而操作系统的计划任务(如:Linux下的CRON或Wind
Stella981 Stella981
2年前
Quartz.NET总结(四)Quartz 远程调度
转载自:http://www.cnblogs.com/zhangweizhong/p/5552558.html 前面篇已经介绍了Quartz.NET的配置,使用和Cron表达式表达式的写法。基本上后台的定时任务的定时执行已经完成,并能正确的按照执行计划,执行相关的job。  然后,如果任务需要更新,停止某个任务呢?总不能上服务器去改相关