栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux基础(三)——进程管理与IO重定向、管道

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux基础(三)——进程管理与IO重定向、管道

进程的生命周期

 

第一个系统进程:

CentOS 5/6 init

CentOS 7 systemd

进程的状态

 

查看进程

静态查看进程 ps

#查看进程
ps aux

[root@VM-4-9-centos ~]# ps aux |less
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  43592  3576 ?        Ss   May14   0:12 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2  0.0  0.0      0     0 ?        S    May14   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        S<   May14   0:00 [kworker/0:0H]
root         5  0.0  0.0      0     0 ?        S    May14   0:03 [kworker/u4:0]
root         6  0.0  0.0      0     0 ?        S    May14   0:00 [ksoftirqd/0]
root         7  0.0  0.0      0     0 ?        S    May14   0:01 [migration/0]
root         8  0.0  0.0      0     0 ?        S    May14   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        S    May14   0:44 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S<   May14   0:00 [lru-add-drain]
root        11  0.0  0.0      0     0 ?        S    May14   0:00 [watchdog/0]
root        12  0.0  0.0      0     0 ?        S    May14   0:00 [watchdog/1]
root        13  0.0  0.0      0     0 ?        S    May14   0:01 [migration/1]
root        14  0.0  0.0      0     0 ?        S    May14   0:00 [ksoftirqd/1]
root        16  0.0  0.0      0     0 ?        S<   May14   0:00 [kworker/1:0H]
root        18  0.0  0.0      0     0 ?        S    May14   0:00 [kdevtmpfs]
root        19  0.0  0.0      0     0 ?        S<   May14   0:00 [netns]
root        20  0.0  0.0      0     0 ?        S    May14   0:00 [khungtaskd]
root        21  0.0  0.0      0     0 ?        S<   May14   0:00 [writeback]

#排序
ps aux --sort %cpu|less
ps aux --sort -%cpu |less
ps aux --sort rss|less

#查看进程的PID
ps aux|grep sshd
cat /run/sshd.pid
pgrep -l sshd
pgrep sshd
pidof sshd

#查看进程树
pstree

 

动态查看进程 top

top
top -d 1 #每隔1秒刷新一次
top -d 1 -p 10126
top -d 1 -p 10126,1

IO重定向

文件描述符

 

ps aux|grep passwd

$$ 当前进程的PID

ll /proc/PID/fd 查看进程打开了哪些文件

[root@tyler ~]# ulimit -a
 core file size          (blocks, -c) 0
 data seg size           (kbytes, -d) unlimited
 scheduling priority             (-e) 0
 file size               (blocks, -f) unlimited
 pending signals                 (-i) 3755
 max locked memory       (kbytes, -l) 64
 max memory size         (kbytes, -m) unlimited
 open files                      (-n) 1024
 pipe size            (512 bytes, -p) 8
 POSIX message queues     (bytes, -q) 819200
 real-time priority              (-r) 0
 stack size              (kbytes, -s) 8192
 cpu time               (seconds, -t) unlimited
 max user processes              (-u) 3755
 virtual memory          (kbytes, -v) unlimited
 file locks                      (-x) unlimited

输出重定向

 [root@tyler ~]# ls /weee
 ls: 无法访问/weee: 没有那个文件或目录
 [root@tyler ~]# ls /weee 2>weee.txt
 [root@tyler ~]# date 1>date.txt

输入重定向

[root@tyler ~]# cat 0 

date 1> date.txt #输出重定向(对stdout重定向)
date > date.txt #1可以省略
date >> date.txt #2个箭头表示往文件中追加
cat < date.txt #0也可以省略
date 2> date.txt #2不能省略

ls /home /weeeee > 1.txt > error.txt #输出到不同位置
ls /home /weeeee &> 1.txt #输出到相同位置(混合输出)
ls /home /weeeee > 1.txt 2>&1

重定向案例

输出重定向

脚本中使用重定向

原脚本:

ping -c1 192.168.1.1
if [ $? -eq 0 ]; then
    echo "up"
else
    echo "down"
fi

不想要的内容重定向输出到null

ping -c1 192.168.1.1 &>/dev/null
if [ $? -eq 0 ]; then
    echo "up"
else
    echo "down"
fi

想要的追加到新文件中

ping -c1 192.168.1.1 &>/dev/null
if [ $? -eq 0 ]; then
    echo "up" >> up.txt
else
    echo "down" >> down.txt
fi

输入重定向

[root@VM-4-9-centos ~]# mail alice
Subject: hello
1
2
.
EOT

[root@VM-4-9-centos ~]# mail -s "test01" alice < /etc/hosts

grep 'root' < /etc/passwd

dd if=/dev/zero of=/file1.txt bs=1M count=20
dd /file1.txt bs=1M count=20

mysql -uroot -p123 < bbs.sql #MySQL表结构导入

[root@VM-4-9-centos ~]# at now +5 min #五分钟后执行任务
at> useradd tylersean
at> 
job 1 at Mon May 16 21:55:00 2022
You have new mail in /var/spool/mail/root

[root@VM-4-9-centos ~]# at now +2 min 

综合案例

date > date.txt
date > /dev/pts/0
date > /dev/pts/1
date > /dev/null #空设备
date > /dev/sda #毁坏硬盘

cp /etc/passwd /dev/null #空设备丢失

[root@VM-4-9-centos ~]# ll /dev/null /dev/vda1 /etc/hosts
crw-rw-rw- 1 root root   1, 3 May 14 11:23 /dev/null
brw-rw---- 1 root disk 253, 1 May 14 11:23 /dev/vda1
-rw-r--r-- 1 root root    234 May 14 10:10 /etc/hosts
# 1,3 是主设备号,从设备号[MAJOR MINOR]
# 主设备号相同表示同一种类型,也可以认为kernel使用的是相同的驱动(模块)
# 从设备号表示在同类型设备中的序号

脚本执行

手动添加文件内容的时候,要按Ctrl+D结束,用脚本的时候设置一个结束符

[root@VM-4-9-centos ~]# vim create_file.sh
cat >file1.txt < 

两条命令同时重定向

(ls; date) &> /dev/null

在subshell中执行

如果有些命令对当前shell造成影响,那么可以在两边加括号,在子shell中执行

[root@VM-4-9-centos ~]# cd /boot; ls
config-3.10.0-1160.45.1.el7.x86_64
config-3.10.0-1160.49.1.el7.x86_64
efi
grub
grub2

[root@VM-4-9-centos boot]#

[root@VM-4-9-centos]# (cd /boot; ls)
config-3.10.0-1160.45.1.el7.x86_64
config-3.10.0-1160.49.1.el7.x86_64
efi
grub
grub2
[root@VM-4-9-centos]#

进程管道Piping

IO重定向是控制输出到文件中

Piping是输出到其他程序中

 

ps aux|less
ps aux|grep 'sshd'
[root@VM-4-9-centos boot]# ps aux|grep sshd
root      1322  0.0  0.2 112984  4328 ?        Ss   May14   0:01 /usr/sbin/sshd -D
root     10857  0.0  0.0 112816   976 pts/0    S+   21:12   0:00 grep --color=auto sshd
root     19929  0.0  0.2 156860  5456 ?        Ss   16:29   0:00 sshd: root@pts/0

过滤方法:先过滤行,再切割列

管理源内的所有的软件包

[root@VM-4-9-centos boot]# yum list|grep tiger|grep vnc
Repository epel is listed more than once in the configuration
tigervnc.x86_64                          1.8.0-22.el7                  updates  
tigervnc-icons.noarch                    1.8.0-22.el7                  updates  
tigervnc-license.noarch                  1.8.0-22.el7                  updates  
tigervnc-server.x86_64                   1.8.0-22.el7                  updates  
tigervnc-server-applet.noarch            1.8.0-22.el7                  updates  
tigervnc-server-minimal.x86_64           1.8.0-22.el7                  updates  
tigervnc-server-module.x86_64            1.8.0-22.el7                  updates 

[root@VM-4-9-centos boot]# ll /dev/ |less

已安装的软件包

[root@VM-4-9-centos boot]# rpm -qa|grep ssh
libssh2-1.8.0-4.el7.x86_64
openssh-7.4p1-22.el7_9.x86_64
openssh-server-7.4p1-22.el7_9.x86_64
openssh-clients-7.4p1-22.el7_9.x86_64

排序

sort a.txt #排序
sort -n a.txt #按行桉数值排序
sort -nr a.txt #按行按数值逆序排序
sort -n -k2 a.txt #按行按第二列按数值排序

ps aux --sort=-%cpu |head -6 |grep -v '%CPU'

统计当前/etc/passwd中用户使用的shell类型

[root@VM-4-9-centos boot]# awk -F: '{print $7}' /etc/passwd|sort|uniq -c
      3 /bin/bash
      1 /bin/false
      1 /bin/sync
      1 /sbin/halt
     22 /sbin/nologin
      1 /sbin/shutdown

# 打印第七列,排序,降重

统计访问网站情况,TOP5 IP

ss -an |grep :80|awk -F: '{print $8}'|sort |uniq -c|sort -k1 -rn|head -5

打印当前所有IP

ip addr |grep 'inet '|awk '{print $2}'|awk -F"/" '{print $1}'

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/893582.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号