美文网首页
5、Linux服务管理、定时任务、配置文件检查、后台运行、ps、

5、Linux服务管理、定时任务、配置文件检查、后台运行、ps、

作者: 一个反派人物 | 来源:发表于2020-11-05 11:50 被阅读0次

1 systemctl

服务管理命令,从CentOS7以后可以使用

systemctl start COMMAND
systemctl stop COMMAND
systemctl restart COMMAND
systemctl reload COMMAND  #重新加载配置,不是所有命令都支持
systemctl status COMMAND
systemctl enbale COMMAND #使服务开机自启动,CentOS6为chkconfig COMMAND on
systemctl disable COMMAND #使服务不开机自启动,CentOS6为chkconfig COMMAND off

systemctl daemon-reload   #修改了/usr/lib/systemd/system/下的服务脚本时,需要重新加载

2 crontab

守护进程是crond,需要开启此服务

2.1 系统定时任务

/etc/cron.daily、/etc/cron.hourly、/etc/cron.monthly、/etc/weekly目录存放了定时执行的脚本

2.2 用户定时任务

/var/spool/cron/存放了不同用户的定时任务配置文件
crontab -e进行定时任务编辑,语法如下:

# 注释,好习惯要添加注释
*  *  *  *  *   CMD
分 时 日 月 周
---------------------------------------------------------------
              field          allowed values
              -----          --------------
              minute         0-59
              hour           0-23
              day of month   1-31
              month          1-12 (or names, see below)
              day of week    0-7 (0 or 7 is Sunday, or use names)

语法

  • 如果时间位置为*,则是每分/每小时/每日/每月/每周的意思
    * 12 * * * CMD表示每天的12点每分钟都执行命令
    0 12 * * * CMD表示每天的12点都执行命令
    01 12 * * * CMD表示每天的12点01分执行命令
  • 如果要表示每隔n分/n小时/n日/n月/n周,用*/n来表示
    */5 * * * * CMD表示每隔5分钟执行命令
  • 还可以用- ,来连接不同的时间
    */5 3-5,7,10 * * * CMD表示每天的3-5、7、10点每隔5分钟执行命令

注意事项

  • 定时任务要加注释
  • 直接写date命令的%要转义,date +\%F\%T,或者将date命令部分放到脚本中
  • 命令要用绝对路径来写,因为定时任务的PATH变量只有/usr/bin:/bin
  • 执行脚本要这样写/bin/bash 文件 &>/dev/null
  • 命令的如果产生输出信息,都会以邮件的形式发送到/var/spool/mail/下的用户邮件文件中。为了避免文件无限增大,推荐配置&>/dev/null
  • 星期不要和日期同时设置

crontab命令的参数

crontab -e  #创建和修改计划任务
crontab -l  #查看当前用户的计划任务
crontab -l -u USERNAME #查看其他用户的计划任务
crontab -r  #删除当前用户所有计划任务

2.3 日志文件

/var/log/cron

2.4 黑名单文件

/etc/cron.deny 防止哪些用户编写定时任务,在其中写入用户名即可

3 aide

配置文件一致性检查工具,可以检查文件的元数据、内容、哈希值、大小的任何修改
安装aide

yum -y install aide

配置文件是/etc/aide.conf,文件中的FIPSRCONTENT_EXCONTENT等变量代表校验哪些信息,如下的这一部分表示了要校验哪些文件夹,除默认的一些文件夹外还可自行添加。例如:/boot/CONTENT_EX的方式来校验。

# Next decide what directories/files you want in the database. Aide
# uses a first match system. Put file specific instructions before generic
# matches. e.g. Put file matches before directories.

/boot/   CONTENT_EX
/bin/    CONTENT_EX
/sbin/   CONTENT_EX
/lib/    CONTENT_EX
/lib64/  CONTENT_EX
/opt/    CONTENT

进行现有文件信息备份,生成的备份文件为/var/lib/aide/aide.db.new.gz

aide --init

当系统运行一段时间需要进行文件改动校验时,将/var/lib/aide/下的aide.db.new.gz更名为aide.db.gz进行检查

aide --check

4 nohup和&

  • &:后台运行,免疫ctrl+C退出,但是用户退出shell,进程会关闭
  • nohup:永久执行,用户退出shell,进程依旧执行,但是ctrl+C会令进程关闭

要让进程真正不受Ctrl+C和shell关闭的影响

nohup conmmand &

5 ps

进程管理,一般用法是ps aux,如需查看指定的进程可以ps aux | grep来过滤出指定进程


显示信息共分11个部分,含义如下:

  1. 启用进程的用户
  2. 进程的PID
  3. 进程占用的CPU
  4. 进程占用的内存
  5. 进程使用的虚拟内存
  6. 进程使用的物理内存
  7. 启动进程的终端
  8. 进程状态,S表示Sleep,R表示Running。
 PROCESS STATE CODES
       Here are the different values that the s, stat and state output specifiers 
       (header "STAT" or "S") will display to describe the state of a process:

               D    uninterruptible sleep (usually IO)
               R    running or runnable (on run queue)
               S    interruptible sleep (waiting for an event to complete)
               T    stopped by job control signal
               t    stopped by debugger during the tracing
               W    paging (not valid since the 2.6.xx kernel)
               X    dead (should never be seen)
               Z    defunct ("zombie") process, terminated but not reaped by its parent

       For BSD formats and when the stat keyword is used, additional characters may be displayed:

               <    high-priority (not nice to other users)
               N    low-priority (nice to other users)
               L    has pages locked into memory (for real-time and custom IO)
               s    is a session leader
               l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
               +    is in the foreground process group
  1. 进程启动时间
  2. 进程持续的时间
  3. 启动进程的命令

5 kill 结束进程

kill -9 PID 强制结束

6 Linux启动流程

(1)加电自检:bios开机检查服务器硬件是否正常
(2)选择启动设备:根据bios设置的优先启动项(网卡、硬盘、u盘、光驱)boot
(3)MBR(GPT)引导:读取磁盘分区信息,内核加载路径
(4)grup菜单:选择启动的内核/进行单用户模式重置密码
(5)加载系统内核信息:可以更好的使用内核控制硬件
(6)systemd:系统的第一个进程运行起来systemd,systemd控制后续服务启动时,是并行启动。(Centos6的init是串行)
(7)读取启动文件,/usr/lib/systemd/system/default.target,得知运行级别
(8)读取系统初始化文件,/usr/lib/systemd/system/sysinit.target
(9)加载/etc/systemd/system/目录中的信息,根据target级别实现服务开机自动启动
(10)运行mingetty进程,显示开机登录信息界面

7 Linux开机自动启动的服务

/etc/systemd/system/文件夹下的文件记录了不同target级别开机自动启动的服务

[root@nfs01 ~]$ ll /etc/systemd/system/
total 4
drwxr-xr-x. 2 root root   57 Dec 11 21:07 basic.target.wants
lrwxrwxrwx. 1 root root   57 Dec 11 21:07 dbus-org.freedesktop.nm-dispatcher.service -> /usr/lib/systemd/system/NetworkManager-dispatcher.service
lrwxrwxrwx. 1 root root   37 Dec 11 21:10 default.target -> /lib/systemd/system/multi-user.target
drwxr-xr-x. 2 root root   87 Dec 11 21:07 default.target.wants
drwxr-xr-x. 2 root root   32 Dec 11 21:07 getty.target.wants
drwxr-xr-x. 2 root root   35 Dec 11 21:07 local-fs.target.wants
drwxr-xr-x. 2 root root 4096 Dec 14 19:51 multi-user.target.wants
drwxr-xr-x. 2 root root   48 Dec 11 21:07 network-online.target.wants
drwxr-xr-x  2 root root   31 Dec 14 19:31 remote-fs.target.wants
drwxr-xr-x. 2 root root   51 Dec 14 19:31 sockets.target.wants
drwxr-xr-x. 2 root root  254 Dec 11 21:07 sysinit.target.wants
drwxr-xr-x. 2 root root   44 Dec 11 21:07 system-update.target.wants
drwxr-xr-x. 2 root root   58 Dec 11 21:07 vmtoolsd.service.requires

正常的启动级别是multi-usermulti-user.target.wants文件夹下是正常启动Linux会加载的服务

[root@nfs01 ~]$ ll /etc/systemd/system/multi-user.target.wants/
total 0
lrwxrwxrwx. 1 root root 38 Dec 11 21:07 auditd.service -> /usr/lib/systemd/system/auditd.service
lrwxrwxrwx. 1 root root 37 Dec 11 21:07 crond.service -> /usr/lib/systemd/system/crond.service
lrwxrwxrwx. 1 root root 42 Dec 11 21:07 irqbalance.service -> /usr/lib/systemd/system/irqbalance.service
lrwxrwxrwx. 1 root root 46 Dec 11 21:07 NetworkManager.service -> /usr/lib/systemd/system/NetworkManager.service
lrwxrwxrwx  1 root root 41 Dec 14 19:31 nfs-client.target -> /usr/lib/systemd/system/nfs-client.target
lrwxrwxrwx  1 root root 42 Dec 14 19:51 nfs-server.service -> /usr/lib/systemd/system/nfs-server.service
lrwxrwxrwx. 1 root root 39 Dec 11 21:07 postfix.service -> /usr/lib/systemd/system/postfix.service
lrwxrwxrwx. 1 root root 40 Dec 11 21:07 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target
lrwxrwxrwx. 1 root root 46 Dec 11 21:07 rhel-configure.service -> /usr/lib/systemd/system/rhel-configure.service
lrwxrwxrwx  1 root root 39 Dec 14 19:31 rpcbind.service -> /usr/lib/systemd/system/rpcbind.service
lrwxrwxrwx. 1 root root 39 Dec 11 21:07 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service
lrwxrwxrwx. 1 root root 36 Dec 11 21:07 sshd.service -> /usr/lib/systemd/system/sshd.service
lrwxrwxrwx. 1 root root 37 Dec 11 21:07 tuned.service -> /usr/lib/systemd/system/tuned.service
lrwxrwxrwx. 1 root root 40 Dec 11 21:07 vmtoolsd.service -> /usr/lib/systemd/system/vmtoolsd.service

相关文章

  • day16预习笔记

    Linux系统定时任务Cron(d)服务应用实践 什么是Cron(d): Cron是Linux系统中以后台进程模式...

  • day 17

    第13章 Linux系统定时任务Cron(d)服务应用实践 1.1、Linux定时任务 1.1.1、什么是定时任务...

  • Linux后台运行任务

    nohup (no hang up)命令 用途:不挂断地运行命令。 语法:nohup Command [ Arg ...

  • linux基础学习笔记三:任务、进程、服务管理

    4.1 定时任务 linux定时任务由crond这个服务管理,Crond每分钟执行三个位置的脚本: /etc/cr...

  • linux command

    linux服务器默认端口22 显示xxxx关键字进程ps -ef|grep xxxx 程序后台运行,并将打印信息保...

  • 为什么我的定时器不跑了

    最近要搞个小服务运行在多家客户的windows服务器上,里面有两个定时任务,一个是定时检查版本号,一个是定时向服务...

  • 13.Linux程序与资源管理

    Linux后台工作管理&:后台执行某个任务 command & bg 将当前任务放到后台++++++ fi...

  • Gearman任务管理服务器

    随着系统的膨胀,越来越多的以来后台各种服务。前期用个队列或者后台定时任务啥的足以搞定。但是随着服务增多,管理和升级...

  • crontab的基本使用

    作用 crontab是linux的一项系统服务。用来在linux上面定时执行任务。crontab服务又分为系统任务...

  • 学习Linux2

    linux 学习2 定时任务管理 crond 任务调度 crontab 进行 定时任务的设置 概述任务调度:是指系...

网友评论

      本文标题:5、Linux服务管理、定时任务、配置文件检查、后台运行、ps、

      本文链接:https://www.haomeiwen.com/subject/vsxnvktx.html