Linux相关知识的第十一回合

Linux相关知识的第十一回合

显示统计占用系统内存最多的进程,并排序

[hooper@magedu-demo ~] $ ps aux --sort=-%mem |head -6
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      3450  0.0  0.4 574028 17272 ?        Ssl  5月11   1:24 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      1698  0.0  0.3  47272 14544 ?        Ss   5月11   0:03 /usr/lib/systemd/systemd-journald
polkitd   3171  0.0  0.3 612240 13160 ?        Ssl  5月11   0:03 /usr/lib/polkit-1/polkitd --no-debug
root      3453  0.0  0.2 296484 11132 ?        Ssl  5月11   0:38 /usr/sbin/rsyslogd -n
root      3174  0.0  0.2 473756 10468 ?        Ssl  5月11   0:15 /usr/sbin/NetworkManager --no-daemon
[hooper@magedu-demo ~] $ ps aux --sort=-rss |head -6
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      3450  0.0  0.4 574028 17272 ?        Ssl  5月11   1:24 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      1698  0.0  0.3  47272 14544 ?        Ss   5月11   0:03 /usr/lib/systemd/systemd-journald
polkitd   3171  0.0  0.3 612240 13160 ?        Ssl  5月11   0:03 /usr/lib/polkit-1/polkitd --no-debug
root      3453  0.0  0.2 296484 11132 ?        Ssl  5月11   0:38 /usr/sbin/rsyslogd -n
root      3174  0.0  0.2 473756 10468 ?        Ssl  5月11   0:15 /usr/sbin/NetworkManager --no-daemon
[hooper@magedu-demo ~] $ ps aux | sort -k4nr|head -5
root      3450  0.0  0.4 574028 17272 ?        Ssl  5月11   1:24 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
polkitd   3171  0.0  0.3 612240 13160 ?        Ssl  5月11   0:03 /usr/lib/polkit-1/polkitd --no-debug
root      1698  0.0  0.3  47272 14544 ?        Ss   5月11   0:03 /usr/lib/systemd/systemd-journald
root      3174  0.0  0.2 473756 10468 ?        Ssl  5月11   0:15 /usr/sbin/NetworkManager --no-daemon
root      3453  0.0  0.2 296484 11132 ?        Ssl  5月11   0:38 /usr/sbin/rsyslogd -n

# ps aux 表头的含义
# USER 进程属于哪个使用者
# PID 进程的PID
# %CPU 进程使用CPU资源的百分比
# %MEM  进程使用物理内存的百分比
# VSZ 进程使用虚拟内存的情况(Kbytes)
# RSS 进程使用物理内存的情况(Kbytes)
# TTY 进程在哪个终端上运行的
# STAT 进程目前的状态
# START 开始运行改进程的时间
# TIME 进程实际使用CPU运行的时间
# COMMAND 进程实际运行的命令

编写脚本,使用forwhile分别实现192.168.0.0/24网段内,地址是否能够ping通,若ping通则输出"success!",若ping不通则输出"fail!"

for循环的脚本

###################################################################
[hooper@magedu-demo sh] $vim ping_for_ip.sh
###################################################
# File Name: ping_for_ip.sh
# Author: hooper
# Mail: hujing0228@gmail.com
# Created Time: 2021-05-24 16:21
#==================================================
#!/bin/bash

### 设置变量
net_id=192.168.168.

### 脚本开始
for ip_id in {1..254};do
{
  if /bin/ping -c1 -W1 ${net_id}${ip_id} >/dev/null ;then
    echo -e "\033[32m ${net_id}${ip_id} is success! \033[0m"
  else
    echo -e "\033[31m ${net_id}${ip_id} is fail! \033[0m"
  fi
} &   ###并发执行
done
wait
###################################################################
[hooper@magedu-demo sh] $ chmod +x ping_for_ip.sh
[hooper@magedu-demo sh] $ /bin/sh ping_for_ip.sh
 192.168.168.2 is success!
 192.168.168.5 is success!
 192.168.168.13 is success!
 192.168.168.20 is success!
 192.168.168.15 is success!
 192.168.168.18 is success!
 192.168.168.10 is success!
 192.168.168.23 is success!
 ..............
 192.168.168.1 is fail!
 192.168.168.6 is fail!
 192.168.168.9 is fail!
 192.168.168.35 is fail!
 192.168.168.38 is fail!
 192.168.168.43 is fail!
 192.168.168.49 is fail!
 192.168.168.31 is fail!
 192.168.168.50 is fail!
 192.168.168.25 is fail!
 192.168.168.53 is fail!
 192.168.168.4 is fail!
 192.168.168.7 is fail!
 192.168.168.8 is fail!

while循环的脚本

[hooper@magedu-demo sh] $ vim ping_while_ip.sh
###################################################################
###################################################
# File Name: ping_while_ip.sh
# Author: hooper
# Mail: hujing0228@gmail.com
# Created Time: 2021-05-24 16:37
#==================================================
#!/bin/bash

### 设置变量
net_id=192.168.168.

### 脚本开始

ip_id=1

while [[ ${ip_id} -le 254 ]]; do
  /bin/ping -c1 -W1 ${net_id}${ip_id} >/dev/null
  if [[ $? -eq 0 ]]; then
    echo -e "\033[32m ${net_id}${ip_id} is success! \033[0m"
  else
    echo -e "\033[31m ${net_id}${ip_id} is fail! \033[0m"
  fi
  let ip_id++
done
###################################################################
[hooper@magedu-demo sh] $ chmod +x ping_while_ip.sh
[hooper@magedu-demo sh] $ /bin/sh ping_while_ip.sh
 192.168.168.1 is fail!
 192.168.168.2 is success!
 192.168.168.3 is success!
 192.168.168.4 is fail!
 192.168.168.5 is success!
 192.168.168.6 is fail!
 192.168.168.7 is fail!
 192.168.168.8 is fail!
 192.168.168.9 is fail!
 192.168.168.10 is success!
 192.168.168.11 is success!
 192.168.168.12 is success!

每周的工作日1:30,将/etc备份至/backup目录中,保存的文件名称格式 为“etcbak-yyyy-mm-dd-HH.tar.xz”,其中日期是前一天的时间

[root@magedu-demo ~] # crontab -l
0 */5 * * *  /usr/sbin/ntpdate ch.pool.ntp.org >/dev/null 2>&1
30 1 * * 1-5 /usr/bin/tar -cPf /backup/etcbak-$(date -d yesterday +\%F-\%H).tar /etc > /dev/null && /usr/bin/xz -z /backup/etcbak-$(date -d yesterday +\%F-\%H).tar >/dev/null 2>&1
# ⚠️注意
# crontab命令中的%会被当成回车来看待,而且%后的内容会作为该命令的标准输入
# 所以如果在crontab命令中如果有%需要使用反斜杠(\)进行转移

工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就发送邮件报警

[hooper@magedu-demo sh] $ vim check_disk.sh
###################################################################
###################################################
# File Name: check_disk.sh
# Author: hooper
# Mail: hujing0228@gmail.com
# Created Time: 2021-05-24 17:42
#==================================================
#!/bin/bash

used_rate_than80=$(df -h|awk -F '[ %]+' '{print $5,$6}'|awk '$1>80'|tail -n +2 >/tmp/disk.log)
counts=$(cat /tmp/disk.log|wc -l)

if [[ ${counts} -ne 0 ]]; then
  cat /tmp/disk.log| mutt -s "磁盘使用率大于80%" no-replay@kefuxing.com.cn
fi
###################################################################
[hooper@magedu-demo sh] $ chmod +r check_disk.sh
[hooper@magedu-demo sh] $ /bin/sh -x check_disk.sh
++ df -h
++ awk '$1>80'
++ tail -n +2
++ awk -F '[ %]+' '{print $5,$6}'
+ used_rate_than80=
++ cat /tmp/disk.log
++ wc -l
+ counts=0
+ [[ 0 -ne 0 ]]

[hooper@magedu-demo sh] $ crontab -l
*/10 * * * 1-5 /bin/sh /home/hooper/sh/check_disk.sh >/dev/null 2>&1
Linux下实现回制游戏的C代码需要考虑几个关键点。首先,需要定义游戏的角色及其属性。假设有两个角色A和B,可以定义结构体来表示它们的属性,如生命值、攻击力等。然后,需要定义游戏的回逻辑,比如角色A先攻击角色B,在每个回结束后,判断角色的生命值是否为零,若是,则游戏结束;否则,切换角色进行下一回。接着,可以编写主函数来实现具体的游戏逻辑。 以下是一个简单的回制游戏的C代码示例: ```c #include <stdio.h> // 定义角色结构体 typedef struct { int hp; // 生命值 int atk; // 攻击力 } Character; // 角色A攻击角色B void attack(Character* attacker, Character* defender) { defender->hp -= attacker->atk; printf("角色A攻击了角色B,角色B剩余生命值:%d\n", defender->hp); } int main() { Character A = {100, 10}; // 定义角色A Character B = {100, 5}; // 定义角色B int round = 1; // 初始化回数 while (A.hp > 0 && B.hp > 0) { printf("第%d回\n", round++); if (round % 2 == 1) { attack(&A, &B); // A先攻击B } else { attack(&B, &A); // B攻击A } } if (A.hp <= 0) { printf("游戏结束,角色B获胜!\n"); } else { printf("游戏结束,角色A获胜!\n"); } return 0; } ``` 以上代码通过结构体定义了两个角色A和B的属性,并在每个回中实现了角色之间的攻击。每个回结束后,会判断角色的生命值,如果有一个角色的生命值降至零以下,则游戏结束。最后,根据生命值判断输出获胜的角色。 这只是一个简单的示例,实际的回制游戏可能还涉及更复杂的逻辑和功能。可以根据需求进一步扩展和修改这段代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值