zoukankan      html  css  js  c++  java
  • linux进程管理之作业控制

    作业控制 jobs


    ====================================================================================

    作业控制是一个命令行功能,允许一个shell 实例来运行和管理多个命令。
    如果没有作业控制,父进程fork()一个子进程后,将sleeping,直到子进程退出。
    使用作业控制,可以选择性暂停,恢复,以及异步运行命令,让 shell 可以在子进程运行期间返回接受其他命令。


    foreground, background, and controlling terminal

    foreground: 前台进程是在终端中运行的命令,该终端为进程的控制终端。前台进程接收键盘产生的输入和信号,并允许从终端读取或写入到终端。
    background: 后台进程没有控制终端,它不需要终端的交互。


    示例1:
    [root@localhost ~]# sleep 3000 & //运行程序(时),让其在后台执行
    [root@localhost ~]# sleep 4000 //^Z,将前台的程序挂起(暂停)到后台
    [2]+ Stopped sleep 4000

    [root@localhost ~]# ps aux |grep sleep
    root 8895 0.0 0.0 100900 556 pts/0 S 12:13 0:00 sleep 3000
    root 8896 0.0 0.0 100900 556 pts/0 T 12:13 0:00 sleep 4000

    查看被挂起的进程( jobs )
    [root@localhost ~]# jobs //查看后台作业
    [1]- Running sleep 3000 &
    [2]+ Stopped sleep 4000

    恢复到前台继续运行( fg )
    恢复到后台继续运行( bg)
    [root@localhost ~]# bg %2 //让作业2在后台运行
    [root@localhost ~]# fg %1 //将作业1调回到前台

    [root@localhost ~]# kill %1 //kill 1,终止PID为1的进程

    [root@localhost ~]# (while :; do date; sleep 2; done) & //进程在后台运行,但输出依然在当前终端

    [root@localhost ~]# (while :; do date; sleep 2; done) &>/dev/null &


    示例2:如何管理远程主机
    [root@localhost ~]# ssh 172.16.50.240
    [root@www ~]# yum -y install screen
    [root@www ~]# screen -S install_apache

    ==断网后,重新连接==
    [root@www ~]# screen -list
    There are screens on:
    28958.install_nginx (Detached)
    29013.install_apache (Detached)
    2 Sockets in /var/run/screen/S-root.

    [root@www ~]# screen -r 29013

  • 相关阅读:
    poj 1013 Counterfeit Dollar
    poj百练2973:Skew数 进制问题
    poj百练2972 进制问题
    poj2080 Calendar
    POJ 1928 The Peanuts
    EXCEL打开CSV文件乱码的解决方法
    希望博客园做个软件职业生存状态调查问卷
    Linq使用Group By [转]
    ADO.NET 从DataTable中获取某列含有的不同值的几种方式
    本该遭拒的十大科技专利:苹果滑动解锁上榜[转]
  • 原文地址:https://www.cnblogs.com/anttech/p/10597638.html
Copyright © 2011-2022 走看看