码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-12-04 11:36:31      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:chkconfig 定时任务

比如备份数据或者重启服务。

crontab -u-e-l-r(删除)

 格式:分 user command

 文件/var/spool/cron/username

 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7

 可用格式1-5表示一个范围15

 可用格式1,2,3表示1或者2或者3

 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时

 要保证服务是启动状态

 systemctl start crond.service


cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

# 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

crontab –e  //进入编辑模式

0 2 * * * /bin/bash /usr/local/sbin/123.sh >>/tmp/123.txt 2>>12.txt

#每天凌晨2点 执行脚本123.sh 把正确的输入到123.txt,把错误的输入到12.txt

 

0 2 1-4 */2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.txt 2>>12.txt

#每天凌晨2,14号,2,4,6,8,10,12月(能被2整除的)周2和周5一个区间用-逗号隔开  执行脚本 123.sh 把正确的输入到123.txt,把错误的输入到12.txt

任务计划没执行

1.可能脚本里可能只是一个命令,不是绝对路径 //(因为不在crondPATH里。cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin)。最好的办法就是写绝对路径

2.追加一个日志,正确的输出或者错误的输出。保证有据可查


chkconfig服务管理工具

chkconfig --list  //查看系统使用chkcongfig服务有哪些

chkconfig network on/off  //分别表示开机开机还是关闭

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

 chkconfig --level 345 network off  //关闭345级别

 chkconfig --del network //删除

 chkconfig --add network  //增加

chkconfig --he


自己添加一个服务

要把文件放到/etc/init.d目录下


systemd服务管理

systemctl list-units --all --type=service //所有的服务都列出(包括未激活的)几个常用的服务相关的命令

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

systemctl disable crond //不让开机启动

 systemctl status crond //查看状态

 systemctl stop crond //停止服务

 systemctl start crond //启动服务

 systemctl restart crond //重启服务

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


unit介绍

ls /usr/lib/systemd/system //系统所有unit,分为以下类型

 service 系统服务

 target 多个unit组成的组

ls -l runlevel*

device 硬件设备

 mount 文件系统挂载点

 automount 自动挂载点

 path 文件或路径

 scope 不是由systemd启动的外部进程

 slice 进程组

 snapshot systemd快照

 socket 进程间通信套接字

 swap  swap文件

 timer 定时器

unit相关的命令

 systemctl list-units //列出正在运行的unit

 systemctl list-units --all //列出所有,包括失败的或者inactive

 systemctl list-units --all --state=inactive //列出inactiveunit

 systemctl list-units --type=service//列出状态为activeservice

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


target介绍

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

 systemctl list-unit-files --type=target //列出所有的target

 systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit

 systemctl get-default //查看系统默认的target

 systemctl set-default multi-user.target

 一个service属于一种类型的unit

 多个unit组成了一个target

 一个target里面包含了多个service

 cat /usr/lib/systemd/system/sshd.service //看这个service属于哪个unit[install]部分










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

标签:chkconfig 定时任务

原文地址:http://blog.51cto.com/lsxme/2047099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!