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

10.23 linux任务计划cron

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

任务计划的配置文件

1/etc/crontab 2[root@aminglinux-02 ~]# cat /etc/crontab 3SHELL=/bin/bash 4PATH=/sbin:/bin:/usr/sbin:/usr/bin //环境变量,命令所在 5MAILTO=root //发送邮件给谁 6# For details see man 4 crontabs 7# Example of job definition: // 格式 8# .---------------- minute (0 - 59) //分(0到59) 9# | .------------- hour (0 - 23) //时 (0到23) 10# | | .---------- day of month (1 - 31) //天 (1到31) 11# | | | .------- month (1 - 12) OR jan,feb,mar,apr ... //月 (1到12) 也支持英文 12# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat //星期 (0到6) 0或7都是周日 也可支持英文 13# | | | | | 14# * * * * * user-name command to be executed //用户 ,不写用户默认是root+执行的命令

定义计划

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

例:

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

执行脚本 例:

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

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

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

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

查看任务计划

crontab -l

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

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

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

清除任务计划

crontab -r 

指定用户操作

crontab -u 

例:

crontab -u root -l

要想启动任务计划服务

systemctl start crond

确定任务计划服务启动

1systemctl status crond 2[root@aminglinux-02 ~]# systemctl status crond 3● crond.service - Command Scheduler 4 Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) 5 Active: active (running) since 五 2017-08-18 13:29:12 CST; 18min ago //表示已经启动 6Main PID: 500 (crond) 7 CGroup: /system.slice/crond.service 8 └─500 /usr/sbin/crond -n 9818 13:29:12 aminglinux-02 systemd[1]: Started Command Scheduler. 10818 13:29:12 aminglinux-02 systemd[1]: Starting Command Scheduler... 11818 13:29:12 aminglinux-02 crond[500]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 36% if used.) 12818 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级别设置

1运行级别 20 关机状态 31 单用户 42 多用户模式 (没有nfs服务,没有无图形服务) 53 多用户模式(无图形) 64 保留 75 多用户 (带有图形服务) 86 重启状态

修改默认开机级别(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 服务

1systemctl list-units --all --type=service 2 3 4systemctl enable crond.service //让服务开机启动 5[root@aminglinux-02 ~]# systemctl enable crond 6[root@aminglinux-02 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service 7lrwxrwxrwx 1 root root 37 818 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”;

1systemctl disable crond //不让开机启动 2 3 4[root@aminglinux-02 ~]# systemctl disable crond 5Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service. 6[root@aminglinux-02 ~]# ls -l /etc/systemd/system/multi-user.target.wants/crond.service

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

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

1systemctl status crond //查看状态 2systemctl stop crond //停止服务 3systemctl start crond //启动服务 4systemctl restart crond //重启服务 5systemctl 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

1systemct is-active crond.service 2

查看某个服务是否为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。

点赞
收藏

评论区

加载中...

相关推荐

systemd创建定时任务

SystemdorCronCron是类Unix系统里最常见的任务计划程序,而Systemd也开始提供定时器作为Cron的替代品。尽管争议不断,Systemd还是被越来越多的Linux发行版使用,Ubuntu也是如此。因此在需要创建定时任务时我决定向"邪恶势力"低头,基于Systemd来实现。Systemd配置根据功

Linux计划任务 定时任务 Crond 配置详解 crond计划任务调试 sh

一、Crond是什么?(概述)crontab是一款linux系统中的定时任务软件用于实现无人值守或后台定期执行及循环执行任务的脚本程序,在企业中使用的非常广泛.  现在开始学习linux计划任务程序吧。crontab的优势:可以实现24小时或定期执行任务,非常高效实用,几乎是每个企业

Linux服务器时间同步命令

时间同步1.首先需了解linux内一任务计划工具crontabcrontab可以定时去执行你要做的动作直接用crontab命令编辑crontabu//设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数crontabl//列出某个用户cron服务的详细内容crontabr//删除某个用户的cron服务cr

Linux计划任务执行结果和手动执行不一致

Linux计划任务执行结果和手动执行不一致,发生原因有三种:以下是计划任务和脚本情况介绍,写出具体代码是为了说明第三种情况root@saltmaster09crontabl/1/bin/sh/root/test03/09/01

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

10.23linux任务计划cronroot@www~cat/etc/crontabSHELL/bin/bashPATH/sbin:/bin:/usr/sbin:/usr/binMAILTOrootFordetailsseeman4crontab

Linux的定时任务

任务计划的条件:1.在未来的某个时间点执行一次某个任务(atbatch)2.周期性的执行某个任务(cron)at在指定时间执行任务_用法_at\选项参数\\时间\_选项参数_\l      查看作业\c      显示即将执行任务的细节\d      使用任务id号