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

简介:

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

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]部分



本文转自 虾米的春天 51CTO博客,原文链接:http://blog.51cto.com/lsxme/2047099,如需转载请自行联系原作者

相关文章
|
4月前
|
开发工具
【云手机】(systemctl)解决:System has not been booted with systemd as init system (PID 1). Can't ope...
【云手机】(systemctl)解决:System has not been booted with systemd as init system (PID 1). Can't ope...
989 0
|
7月前
|
网络协议 Linux 网络安全
【Linux环境】centos7 Failed to start iptables.service: Unit not found 防火墙
【Linux环境】centos7 Failed to start iptables.service: Unit not found 防火墙
304 0
|
12月前
|
监控 Java 数据库连接
服务--systemd的管理
服务--systemd的管理
122 0
|
设计模式 监控 网络协议
关于Linux中通过 Systemd Path Unit 监听配置更新自动重启服务的一些笔记
在 stackoverflow.com 的Unix & Linux 社区 看到有小伙伴提出相关的问题。 这里整理分享给小伙伴,博文内容涉及: Systemd 的 Path Units常用命令手册学习 Path Units 的应用 通过httpd服务演示监听配置文件自动重启服务 食用方式:需要了解一点 Systemd 理解不足小伙伴帮忙指正
205 0
|
关系型数据库 MySQL Linux
Linux:1.service:服务管理+2.服务运行类别:runlevel+3.给各种服务设置自启动:chkconfig指令+4.systemctl:服务管理
Linux:1.service:服务管理+2.服务运行类别:runlevel+3.给各种服务设置自启动:chkconfig指令+4.systemctl:服务管理
105 0
Linux:1.service:服务管理+2.服务运行类别:runlevel+3.给各种服务设置自启动:chkconfig指令+4.systemctl:服务管理
appmon:start().启动错误
appmon:start().启动错误
158 0