day 36 Linux日常运维

来源:互联网 发布:java抽象工厂 编辑:程序博客网 时间:2024/05/17 08:13

10.23 Linux任务计划cron

用法: 分 时 日 月 周 user command
  • 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-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
  • 使用的命令尽量使用绝对路径,避免该命令不在PATH里导致计划任务不能执行,使用命令cat /etc/crontab查看PATH变量包含的路径,可将不在默认路径中的命令的路径加到PATH中
  • 不指定用户,则默认是当前用户
  • systemctl start crond #启动服务
  • crontab -u #指定用户,不加“-u”选项则为当前用户
  • crontab -e #指定计划任务,实际上是使用vim工具打开了crontab的配置文件“/var/spool/cron/username”,但不要直接去编辑该文件
  • crontab -l #列出计划任务
  • crontab -r #删除计划任务
10.24 chkconfig工具

chkconfig工具在CnetOS7中较少使用
  • chkconfig --list #列出所有的服务,CnetOS7系统目前仅有netconsole和network两个服务,可通过命令ls /etc/init.d查看到,0~6为不同的级别,其中0作为关闭系统的动作,1作为重启至单用户模式,2表示无NFS支持的多用户模式,3表示完全多用户模式(最常用的级别),4保留给用户自定义,5表示图形登录方式,6为重启;
  • chkconfig --level 3 network off #指定network服务在3级别关
  • chkconfig --level 345 network off #指定network服务在3、4、5级别关
  • chkconfig --add 服务名 #将某个自定义服务增加到服务列表中,自定义服务必须在/etc/init.d目录下
  • chkconfig --del 服务名 #将某个自定义服务从服务列表中删除
10.25 systemd管理服务

  • systemctl list-units --all --type=service #列出所有的服务,不加“--all”选项,则只列出active状态的服务,不包含“inactive”状态的服务项
几个常用的服务相关的命令
  • systemctl enable crond.service #让服务开机启动
  • systemctl disable crond #不让开机启动
  • systemctl status crond #查看状态
  • systemctl stop crond #停止服务
  • systemctl start crond #启动服务
  • systemctl restart crond #重启服务
  • systemctl is-enabled crond #检查服务是否开机启动
10.26 unit介绍

1、 unit类型
使用命令:ls /usr/lib/systemd/system 查看到系统所有的unit,分为以下类型:
  • service --系统服务
  • target --多个unit组成的组
  • device --硬件设备
  • mount --文件系统挂载点
  • automount --自动挂载点
  • path --文件或路径
  • scope --不是由systemd启动的外部进程
  • slice --进程组
  • snapshot --systemd快照
  • socket --进程间通信套接字
  • swap  --swap文件
  • timer --定时器
2、unit相关的命令
  • systemctl list-units #列出正在运行的unit
  • systemctl list-units --all #列出所有,包括失败的或者inactive的
  • systemctl list-units --all --state=inactive #列出inactive的unit
  • systemctl list-units --type=service #列出状态为active的service
  • systemctl is-active crond.service #查看某个服务是否为active
10.27 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 #设置系统默认的target
  • 一个service属于一种类型的unit
  • 多个unit组成了一个target
  • 一个target里面包含了多个service
  • cat /usr/lib/systemd/system/sshd.service #查看某个service属于哪个target,看[install]部分

原创粉丝点击