欢迎光临
我们一直在努力

别只会yum安装!源码编译|自建YUM仓库|计划任务|进程调度全套实战(CentOS7)

Linux软件包管理、计划任务与进程调度实战(CentOS7)

前言

在CentOS7运维工作中,除rpm/yum包管理器之外,源码编译可以自定义编译参数部署软件;自建YUM仓库可以解决内网环境软件安装依赖问题;at、crontab实现任务自动化;进程调度nice、chrt可以管控CPU资源分配,本篇把以上高频实操整理为完整笔记。

一、源码安装软件

1.1 源码编译 Nginx

Nginx是高性能HTTP以及反向代理Web服务器,官方地址:https://nginx.org

# 安装编译依赖
[root@server~ 18:39:54]# yum install gcc pcre-devel zlib-devel

# 下载源码包
[root@server~ 18:40:20]# wget https://nginx.org/download/nginx-1.24.0.tar.gz

# 解压源码
[root@server~ 18:42:42]# tar -xf nginx-1.24.0.tar.gz

# configure配置编译参数,指定安装目录
[root@server~ 18:42:59]# cd nginx-1.24.0/
[root@servernginx-1.24.0 18:46:10]# ./configure –prefix=/usr/local/nginx

# make编译 && make install安装
[root@servernginx-1.24.0 18:47:31]# make install

# 查看安装目录
[root@server~ 18:47:58]# ls /usr/local/nginx/
conf html logs sbin

配置环境变量,全局直接执行nginx命令

# 当前会话临时生效
[root@server~ 18:48:07]# export PATH=$PATH:/usr/local/nginx/sbin/
# 写入bashrc永久生效
[root@server~ 18:48:35]# echo 'export PATH=$PATH:/usr/local/nginx/sbin/' >> ~/.bashrc

基础使用

# 启动nginx
[root@server~ 18:48:52]# nginx

# 访问验证
[root@server~ 18:52:08]# curl -s http://localhost | grep Thank
<p><em>Thank you for using nginx.</em></p>

# 优雅停止,等待现有请求处理完成
[root@server~ 18:55:56]# ps -C nginx
PID TTY TIME CMD
8252 ? 00:00:00 nginx
8253 ? 00:00:00 nginx
[root@server~ 18:57:20]# nginx -s quit
[root@server~ 18:57:53]# ps -C nginx
PID TTY TIME CMD

# 强制停止
nginx -s stop

# 重载配置文件
nginx -s reload

把nginx注册为systemd系统服务

# 复制sshd.service模板
[root@server~ 19:13:48]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/nginx.service

[root@server~ 19:14:07]# vim /etc/systemd/system/nginx.service

写入下面内容

[Unit]
Description=Nginx server daemon

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target

重载systemd,设置开机自启同时立刻启动服务

[root@server~ 19:15:08]# systemctl daemon-reload
[root@server~ 19:15:27]# systemctl enable nginx.service –now

二、自建YUM仓库

适用场景:内网服务器无法访问互联网,本地搭建yum源。整体流程:准备rpm软件包 → 创建仓库索引 → web发布 → 客户端配置repo文件测试。

2.1 服务端操作

# 创建目录存放rpm包
[root@server~ 19:17:00]# mkdir rpms
[root@server~ 19:19:22]# cd rpms

#下载软件包以及全部依赖到当前目录
[root@serverrpms 19:19:28]# yum install –downloadonly –downloaddir . vim open-vm-tools vsftpd lrzsz bash-completion nginx httpd mariadb-server

#把rpm包移动nginx网页目录
[root@server~ 19:21:37]# mkdir /usr/local/nginx/html/tools
[root@server~ 19:21:44]# mv rpms /usr/local/nginx/html/tools/Packages

#安装createrepo工具生成仓库元数据索引
[root@server~ 19:22:48]# yum install -y createrepo
[root@server~ 19:24:47]# createrepo /usr/local/nginx/html/tools/

修改nginx配置开启目录浏览autoindex on

[root@server~ 19:25:24]# vim /usr/local/nginx/conf/nginx.conf

在http{}块内添加

http {
autoindex on;
……
}

重载nginx

[root@server~ 19:26:40]# systemctl reload nginx

2.2 客户端配置验证

#备份原有repo仓库文件
[root@server~ 19:27:01]# mkdir /etc/yum.repos.d/old
[root@server~ 19:27:58]# mv /etc/yum.repos.d/*repo /etc/yum.repos.d/old

#新建本地仓库repo配置
[root@server~ 19:28:33]# cat << 'EOF' > /etc/yum.repos.d/tools.repo
[tools]
name=centos7 tools from server.laoma.cloud
baseurl=http://server.laoma.cloud/tools/
enabled=1
gpgcheck=0
EOF

[root@server~ 19:32:01]# ls /etc/yum.repos.d/
old tools.repo

#测试安装软件
[root@server~ 19:32:13]# yum install -y httpd

2.3 仓库同步

把远端yum仓库完整同步到本地

#同步软件包
[root@server~ 19:32:24]# reposync –repoid=tools
#重新生成索引
[root@server~ 19:32:47]# createrepo tools
[root@server~ 19:33:03]# ls tools/
Packages repodata

三、Linux计划任务管理

3.1 一次性计划任务 atd

at用来执行未来某时间一次性任务,依赖后台守护进程atd。

#安装at工具
[root@server~ 19:35:19]# yum install at

#查看服务状态,确认开机自启
[root@server~ 19:36:24]# systemctl status atd
● atd.service – Job spooling tools
Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since 一 2026-07-27 10:10:35 CST; 2 days ago
Main PID: 1259 (atd)
CGroup: /system.slice/atd.service
└─1259 /usr/sbin/atd -f

727 10:10:35 server.liuyifei.cloud systemd[1]: Started Job spooling tools.

基础示例

#1分钟之后执行任务,输入完成按Ctrl+D提交任务
[root@server~ 19:36:45]# at now +1 minutes
at> echo hello world > /tmp/hello.log
at> <EOT>
job 1 at Wed Jul 29 19:39:00 2026
[root@server~ 19:39:26]# cat /tmp/hello.log
hello world

读取脚本执行任务

[root@server~ 19:39:46]# vim myscript.sh
date >> /tmp/myscript.log
echo hello myscript >> /tmp/myscript.log

#方式1 重定向
[root@server~ 19:40:56]# at now +1 minutes < myscript.sh
job 2 at Wed Jul 29 19:42:00 2026
[root@server~ 19:41:52]# cat /tmp/myscript.log
2026年 07月 29日 星期三 19:42:00 CST
hello myscript

#方式2 使用-f参数
[root@server~ 19:42:09]# at now +100 minutes -f myscript.sh

时间语法示例

  • now +5 hours 5小时之后
  • teatime tomorrow 明天16点
  • noon +4 days 4天后中午12点
  • 5 pm august 3 2016 指定年月日下午5点

任务管理命令

#查看任务队列 atq等价 at -l
[root@server~ 19:43:35]# atq

#指定队列提交任务 a-z,字母越靠后优先级越低
[root@server~ 19:43:36]# at -q b now +5 minutes < myscript.sh

#查看任务详情
[root@server~ 19:43:43]# at -c 3

#删除任务 atrm等价 at -d
[root@server~ 19:43:53]# atrm 3

用户权限控制文件

  • /etc/at.allow:白名单,只有写在此文件内用户允许使用at
  • /etc/at.deny:黑名单,写在此文件用户禁止使用at

规则:

  • 如果存在at.allow,只允许白名单用户
  • 如果没有at.allow,读取at.deny,黑名单内禁止,其余全部允许
  • 3.2 周期性计划任务 crond

    crond守护进程实现循环重复执行任务,分为用户级crontab、系统级crontab。

    用户级 crontab
    命令说明
    crontab -l 查看当前用户定时任务
    crontab -e 编辑定时任务
    crontab -r 清空当前用户所有定时任务
    crontab -u 用户名 root管理其他用户任务

    crontab时间格式:分钟 小时 日期 月份 星期 命令
    时间字段符号说明

    • *:任意时间
    • x-y:连续范围
    • x,y:多个离散时间
    • */n:每隔n单位执行

    示例

    [root@server~ 19:45:16]# export EDITOR=vim
    [root@server~ 19:46:07]# crontab -e
    1 # For details see man 4 crontabs
    2
    3 # Example of job definition:
    4 # .—————- minute (0 – 59)
    5 # | .————- hour (0 – 23)
    6 # | | .———- day of month (1 – 31)
    7 # | | | .——- month (1 – 12) OR jan,feb,mar,apr …
    8 # | | | | .—- day of week (0 – 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri, sat
    9 # | | | | |
    10 # * * * * * command to be executed
    11 * 14,15 10-31 */2 1-5 /bin/bash /root/myscript.sh

    ```bash
    [root@centos7 ~ 22:06:15]# crontab -l
    */2 2,6-23 * * * date >> /tmp/date.log

    注意:crontab任务标准输出、错误输出默认发送邮件;命令建议做重定向。

    系统级计划任务
  • /etc/crontab:系统全局任务,格式多一列执行用户字段
  • /etc/cron.d/*.conf:推荐自定义系统定时任务,防止包更新覆盖/etc/crontab
  • /etc/anacrontab:弥补关机错过执行的任务,定义daily、weekly、monthly任务
    • /etc/cron.daily:每日脚本
    • /etc/cron.weekly:每周脚本
    • /etc/cron.monthly:每月脚本
  • [root@centos7 ~ 22:12:10]# cat /etc/crontab
    [root@centos7 ~ 22:12:35]# cat /etc/anacrontab

    run-parts:批量执行目录下面所有可执行脚本

    四、Linux进程调度管理

    CPU资源少于进程数量时,内核调度器分配CPU时间,分为实时调度器、非实时调度器。

    4.1 调度策略

    实时调度器(优先级更高)

    • SCHED_FIFO:先入先出,抢到CPU一直运行,直到阻塞或者更高优先级抢占
    • SCHED_RR:轮询,同等优先级轮流分配时间片

    非实时调度器(绝大多数普通进程)

    • SCHED_OTHER(SCHED_NORMAL) 默认时间片轮转
    • SCHED_BATCH 批量后台任务
    • SCHED_IDLE 极低优先级

    最小优先级实时进程,优先级也高于最高优先级普通进程。

    内核可调参数(防止实时进程占满CPU)

    • kernel.sched_rt_period_us CPU周期,默认1000000us=1s
    • kernel.sched_rt_runtime_us周期内实时进程最大占用时间,默认950000us

    4.2 nice值(普通进程优先级)

    nice范围 -20 ~ 19

    • -20:优先级最高
    • 19:优先级最低
    • 默认0

    CPU空闲的时候nice高低不会体现差异,只有CPU竞争才生效。

    #启动进程时设置nice
    [root@centos7 ~ 22:20:12]# nice md5sum /dev/zero &

    #指定调整值
    [root@centos7 ~ 22:20:40]# nice -n 2 md5sum /dev/zero &

    #查看进程nice
    [root@centos7 ~ 22:21:15]# ps -o pid,nice,command

    #修改已经运行进程nice renice
    [root@centos7 ~ 22:22:02]# renice -n 2 55782

    #普通用户只能增大nice,不能调小;root可以任意修改
    [root@centos7 ~ 22:23:10]# renice -n -2 55782

    top交互界面按r,输入PID可以修改nice值。

    4.3 chrt 修改实时调度策略(root)

    chrt用来查看、修改进程调度策略与实时优先级。

    #查看允许优先级范围
    [root@centos7 ~ 22:30:10]# chrt -m

    #以RR策略优先级5运行程序
    [root@centos7 ~ 22:30:42]# chrt -r 5 md5sum /dev/zero &

    #修改正在运行PID为56225进程为FIFO优先级10
    [root@centos7 ~ 22:31:20]# chrt -f –pid 10 56225

    #改回普通调度SCHED_OTHER
    [root@centos7 ~ 22:31:45]# chrt -o –pid 0 56225

    #查看进程调度信息
    [root@centos7 ~ 22:32:10]# ps -o pid,cls,rtprio,command 56225

    总结

    crontab 计划任务
    用户级任务使用crontab -l / -e / -r管理,格式为分 时 日 月 周 命令;日期和星期是逻辑或关系,这点极易踩坑。任务输出建议重定向日志,避免系统邮件堆积。
    系统级定时任务优先使用/etc/cron.d/*.conf;anacron用来弥补关机错过的定时作业,run‑parts批量运行目录内脚本。
    进程调度
    Linux 分为实时调度策略(SCHED_FIFO、SCHED_RR)和普通非实时调度;所有实时进程优先级高于普通进程。
    nice控制普通进程优先级,范围‑20 ~ 19,仅 CPU 竞争时优先级才生效。
    chrt用来修改进程实时调度策略,需要 root 权限;top 命令r快捷键可快速修改 nice。
    实操提醒:定时任务尽量全部使用绝对路径;不要随意修改实时调度,防止系统卡死。

    赞(0)
    未经允许不得转载:171主机测评 » 别只会yum安装!源码编译|自建YUM仓库|计划任务|进程调度全套实战(CentOS7)
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址