zoukankan      html  css  js  c++  java
  • Linux 任务控制

    Linux/Unix 提供了不同与 windows 的多任务处理,也提供一套用来切换前后台任务的命令

    bg fg & Ctrl+z nohup sitsid

    1. Ctrl-Z  挂起程序
      user@ae01:~$ ./test.sh
      ^Z
      [1]+  Stopped                 ./test.sh
      user@ae01:~$

      当作业在运行期间,执行 Ctrl-Z 暂挂起该作业并返回该作业的 JobID 等信息,类似于使用 jobs 命令返回的信息

    2. bg  后台执行
      user@ae01:~$ jobs
      [1]+  Stopped                 ./test.sh、
      user@ae01:~$ bg 1
      [1]+ ./test.sh &
      user@ae01:~$

      bg 将暂挂的作业作为后台作业运行来在当前环境中重新执行这些作业,如果指定的作业已经在后台运行,bg 命令不起作用并成功退出。如果未提供 JobID 参数,bg 命令会使用最近暂挂的作业

    3. fg  前台运行作业
      user@ae01:~$ jobs
      [1]+  Stopped                 ./test.sh、
      user@ae01:~$ fg 1
      ./test.sh
      user@ae01:~$

      fg 使用 JobID 参数来指明在前台下要运行的特定作业。如果提供 JobID,fg 命令使用最近在后台被暂挂的作业

    4. &  把作业放到后台执行
      user@ae01:~$ ./test.sh &
      [1] 33511
      user@ae01:~$

      当在前台运行某个作业时,终端被该作业占据;可以使用&命令把作业放到后台执行,而在后台运行作业时,它不会占据终端。实际上,这样是将命令放入到一个作业队列中了。作业在后台运行一样会将结果输出到屏幕上,干扰你的工作。如果放在后台运行的作业会产生大量的输出,最好使用下面的方法把它的输出重定向到某个文件中:command >out.file 2>&1 &

    5. nohup  忽略挂断信号
      user@ae01:~$ nohup ./test.sh > test.log 2>&1 &
      [1] 33537
      user@ae01:~$ jobs

      执行作业时,可以使用小括号() 执行后台作业也能做到类似 nuhup 的效果,使得 当前 Shell 关闭后,作业仍能正常运行。原理同 setsid 和 disown

      user@ae01:~$ (./test.sh &)
      
      user@ae01:~$ ps -ef | grep test
      515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh
      515      12483 21734 0 11:59 pts/12   00:00:00 grep test

      使用 & | bg 运行后台命令时,其父进程还是当前终端 shell 的进程,而一旦父进程退出,则会发送hangup信号给所有子进程,子进程收到hangup以后也会退出。如果我们要在退出shell的时候继续运行进程,则需要使用nohup忽略hangup信号,或者使用setsid将父进程设为init进程(进程号为1),也可以使用 disown 命令

      1. setsid
        user@ae01:~$ ps -ef | grep test
        515      29710 21734 0 11:47 pts/12   00:00:00 /bin/sh ./test.sh
        515      29713 21734 0 11:47 pts/12   00:00:00 grep test
        user@ae01:~$ setsid ./test.sh &
        [1] 409
        user@ae01:~$ ps -ef | grep test
        515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh
        515        413 21734 0 11:49 pts/12   00:00:00 grep test

        对于已经执行的前台作业,setsid 可以将当前作业的父进程设置为1,但对于已经是后台运行的命令我们需要使用 disown 命令

      2. disown
        user@ae01:~$ ./test.sh &
        [1] 2539
        user@ae01:~$ jobs -l
        [1]+ 2539 Running                 ./test.sh &
        user@ae01:~$ disown -h %1
        user@ae01:~$ ps -ef | grep test
        515        410     1 0 11:49 ?        00:00:00 /bin/sh ./test.sh
        515       2542 21734 0 11:52 pts/12   00:00:00 grep test

        对于已经执行的后台作业,disown 可以将当前作业的父进程设置为1

  • 相关阅读:
    Atitit.ati  str  字符串增强api
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.加密算法 des  aes 各个语言不同的原理与解决方案java php c#
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.html解析器的选型 jsoup nsoup ,java c# .net 版本
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    Atitit.软件开发的最终的设计 dsl化,ast化(建立ast, 解析执行ast)
    swfupload 上传控件的配置
    SWFUpload下载地址
    js 转化类似这样的时间( /Date(1389060261000)/)问题
  • 原文地址:https://www.cnblogs.com/tannerBG/p/4064331.html
Copyright © 2011-2022 走看看