linux 进程管理

linux 进程管理

1. 进程信息查看

1. ps aux (BSD UNIX风格显示),aux会截断command列

    a     所有用户进程
    x     所有非用户进程
    u     BSD UNIX风格显示

[root@localhost ~]# ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.3  44516  7132 ?        Ss   11:04   0:01 /usr/lib/systemd/systemd
root          2  0.0  0.0      0     0 ?        S    11:04   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    11:04   0:00 [ksoftirqd/0]
root          7  0.0  0.0      0     0 ?        S    11:04   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    11:04   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        S    11:04   0:00 [rcuob/0]
root         10  0.0  0.0      0     0 ?        S    11:04   0:00 [rcuob/1]
root         11  0.0  0.0      0     0 ?        S    11:04   0:00 [rcuob/2]
root         12  0.0  0.0      0     0 ?        S    11:04   0:00 [rcuob/3]


内存使用及其VSZ(虚拟内存大小)和RSS(常驻集大小):
    VSZ     表示如果一个程序完全驻留在内存的话需要占用多少内存空间(KB)
    RSS     指明了当前实际占用了多少内存(KB)
STAT    显示了进程当前的状态:
    D 不可中断 Uninterruptible(usually IO)
    R 正在运行,或在队列中的进程
    S 处于休眠状态
    T 停止或被追踪
    Z 僵尸进程
    W 进入内存交换(从内核2.6开始无效)
    X   死掉的进程

2. ps -ef (system V风格显示),不会截断command列

    -e    所有进程
    -f    完整格式

[root@localhost ~]# ps -ef
UID         PID   PPID  C STIME TTY          TIME CMD
root          1      0  0 11:04 ?        00:00:01 /usr/lib/systemd/systemd --switched-root
root          2      0  0 11:04 ?        00:00:00 [kthreadd]
root          3      2  0 11:04 ?        00:00:00 [ksoftirqd/0]
root          7      2  0 11:04 ?        00:00:00 [migration/0]
root          8      2  0 11:04 ?        00:00:00 [rcu_bh]
root          9      2  0 11:04 ?        00:00:00 [rcuob/0]
root         10      2  0 11:04 ?        00:00:00 [rcuob/1]

3. ps -ef -ouid,pid,time(指定显示的列)

    -o    指定显示的列

4. pidof通过进程名查找进程id

    [root@localhost ~]# jobs
    [1]   Running                 ping www.ibm.com > /tmp/ping2.out 2>&1 &
    [2]-  Running                 nohup ping www.baidu.com > /tmp/ping1.out 2>&1 &
    [3]+  Running                 nohup ping www.baidu.com > /tmp/ping2.out 2>&1 &
    [root@localhost ~]# pidof ping
    10655 10654 10618
    [root@localhost ~]# pidof ping | xargs kill
    [1]   Terminated              ping www.ibm.com > /tmp/ping2.out 2>&1
    [2]-  Terminated              nohup ping www.baidu.com > /tmp/ping1.out 2>&1
    [3]+  Terminated              nohup ping www.baidu.com > /tmp/ping2.out 2>&1

5. pkill 通过进程名杀死进程

    [root@localhost ~]# nohup ping www.baidu.com > /tmp/ping2.out 2>&1 &
    [1] 10679
    [root@localhost ~]# nohup ping www.baidu.com > /tmp/ping1.out 2>&1 &
    [2] 10680
    [root@localhost ~]# pkill ping
    [1]-  Terminated              nohup ping www.baidu.com > /tmp/ping2.out 2>&1
    [2]+  Terminated              nohup ping www.baidu.com > /tmp/ping1.out 2>&1

2. 进程前台与后台切换

1. ctrl + z 停止当前进程,并将进程切换到后台

2. jobs可以查看后台进程

3. fg + number(jobs命令结果第一列)可切换后台进程到前台

[root@localhost ~]# vi /etc/hosts
[1]+  Stopped                 vi /etc/hosts
[root@localhost ~]# jobs
[1]+  Stopped                 vi /etc/hosts
[root@localhost ~]# fg 1
vi /etc/hosts

3. 后台启动进程

当用户注销(logout)或者网络断开时,终端会收到 HUP(hangup)信号从而关闭其所有子进程,因此,我们的解决办法就有两种途径:要么让进程忽略HUP信号,要么让进程运行在新的会话里从而成为不属于此终端的子进程

1. nohup(忽略 HUP 信号)

    1. 在要处理的命令前加上nohup,标准输出和标准错误缺省会被重定向到当前目录nohup.out文件中

    2. nohup会占用终端,可在结尾加上"&"将命令放入后台运行

    3. nohup运行的进程,在当前会话结束时,会将父进程托管给init进程(进程id为1)

    [root@localhost ~]# nohup ping www.ibm.com &
    [1] 10479
    [root@localhost ~]# nohup: ignoring input and appending output to 'nohup.out'
    [root@localhost ~]# jobs
    [1]+  Running                 nohup ping www.ibm.com &

2. setsid (新建会话)

    1. 在要处理的命令前加上 setsid 即可

    2. setid开启的进程父进程是init进程(进程id为1)

    3. 进程会占用终端,可在结尾加上">filename 2>&1"来更改缺省的重定向文件名

    [root@localhost ~]# setsid ping www.ibm.com > /tmp/ping.out
    [root@localhost ~]# ps -ef | grep ping
    root      10543      1  0 14:49 ?        00:00:00 ping www.ibm.com
    root      10545  10486  0 14:49 pts/0    00:00:00 grep --color=auto ping

3. & 

    1. 在要处理的命令后加上 & 即可, 标准输出和标准错误依然会打印到当前终端,可在&前重定向输出
        [root@localhost ~]# ping www.ibm.com > /tmp/ping2.out 2>&1 &
        [1] 10618

    2. nohup运行的进程,在当前会话结束时,会将父进程托管给init进程(进程id为1)

4. disown

如果事先在命令前加上nohup或者setsid就可以避免HUP信号的影响。但是如果我们未加任何处理就已经提交了命令,这时可以用disown命令

    1. 用disown -h jobspec来使某个作业忽略HUP信号
    2. 用disown -ah 来使所有的作业都忽略HUP信号
    3. 用disown -rh 来使正在运行的作业忽略HUP信号


    disown 示例1(如果提交命令时已经用“&”将命令放入后台运行,则可以直接使用“disown”)
    [root@pvcent107 build]# cp -r testLargeFile largeFile &
    [1] 4825
    [root@pvcent107 build]# jobs
    [1]+  Running                 cp -i -r testLargeFile largeFile &
    [root@pvcent107 build]# disown -h %1
    [root@pvcent107 build]# ps -ef |grep largeFile
    root      4825   968  1 09:46 pts/4    00:00:00 cp -i -r testLargeFile largeFile
    root      4853   968  0 09:46 pts/4    00:00:00 grep largeFile
    [root@pvcent107 build]# logout

    如果提交命令时未使用“&”将命令放入后台运行,可使用CTRL-z和“bg”将其放入后台,再使用“disown”
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值