2_进程相关命令用法

Listing Processes

Processes相关命令的用法

  1. ps aux静态查看进程。

    aux前不用加横号。

  2. ps -ef静态查看进程。-efaux只是在输出格式上有所区别。

    vboxuser 5377 4706 0 15:59 pts/0 00:00:00 ps -ef

  3. pgrep -l process's name作用是检索进程id,同时显示进程name。

    for example:

    in bash:

    pgrep -l sshd

    696 sshd

  4. top

    top命令动态监视进程,以及系统平均负载、cpu和内存的消耗。

  5. 在命令后加&开启后台进程,非交互式的命令会将输出打印到同一个终端。

    for example:

    in bash:

    ifconfig > output.tx 2> errors.txt &

    如果不想命令输出打印到终端,可以这样做:

    ping -c 1 google.com > /dev/null 2>&1 &

    /dev/null相当于一个空的设备,俗称文件黑洞blackhole,它会立即丢弃写入其中的任何内容。

  6. jobs命令会在方括号中[]返回进程的jobs id以及process status。

    for example:

    in bash:

    sleep 15&
    sleep 10&
    jobs

    [3]- Running sleep 20&
    [4]+ Running sleep 12&

    jobs -l

    [3]- 3664 Done sleep 20
    [4]+ 3665 Done sleep 12

    这里需要注意的是jobs id是相对于本地shell终端的,而process id是由系统内核维护并且具有全局范围。

  7. 如果你想使bring a background running process to the foreground,使用fg命令,fg即foreground。

    for example:

    in bash:

    sleep 20&

    [1] 3875

    jobs

    [1]+ Running sleep 20 &

    fg %1 #使后台进程[1]恢复到前台运行

    sleep 20

    fgbg也可以用来恢复被ctrl + z中断的进程。

    for example:

    in bash:

    ==sleep== 20

    ^Z
    [1]+ Stopped sleep 20

    pgrep -l sleep

    3925 sleep

    jobs

    [1]+ Stopped sleep 20

    bg %1 #在后台恢复本地进程[1]

    [1]+ sleep 20 &

  8. nohupwill make the process to ignore all the hang up signals。

    for example:

    in bash:

    nohup sleep 123 & #使sleep命令忽略所有hang up信号

    nohup: ignoring input and appending output to 'nohup.out'

    close the terminal,open it again.

    pgrep -l sleep

    4148 sleep

    the sleep process is still there!

    这里注意如果想查看命令输出的结果,可以到'nohup.out'中查看。

    for example:

    in bash:

    nohup ifconfig &
    cat nohup.out

    ...

    if a process started with nohup and its parent ,which is the bash shell,gets terminated,the process will be adopted by init or systermd which runs as the first process on boot and has PID 1.

    其他命令如screen、Tmux,也可以防止在终端被关闭或断开连接时,运行中的进程被终止。

posted @   lyyyuanfang  阅读(30)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
点击右上角即可分享
微信分享提示