进程前后台与SIGHUP

进程前后台与SIGHUP

前台任务是独占命令行窗口,只有运行完了或者手动终止才能执行其他命令。

后台任务的特点:

  1. 继承当前 session (对话)的标准输出(stdout)和标准错误(stderr)。因此,后台任务的所有输出依然会同步地在命令行下显示。
  2. 不再继承当前 session 的标准输入(stdin)。你无法向这个任务输入指令了。如果> 它试图读取标准输入,就会暂停执行(halt)
  • 也就是说:

    • 只有前台任务能接受你的输入,一个后台任务遇到输入时会挂起
    • 后台任务也可以输出到屏幕上,除非你重定向
  • 前台后台和会话

nohup vs disown vs &

这里有非常详细的说明
下面是摘录自stackoverflow

  • & puts the job in the background, that is, makes it block on attempting to read input, and makes the shell not wait for its completion.
  • disown removes the process from the shell’s job control, but it still leaves it connected to the terminal. One of the results is that the shell won’t send it a SIGHUP. Obviously, it can only be applied to background jobs, because you cannot enter it when a foreground job is running.
  • nohup disconnects the process from the terminal, redirects its output to nohup.out and shields it from SIGHUP. One of the effects (the naming one) is that the process won’t receive any sent SIGHUP. It is completely independent from job control and could in principle be used also for foreground jobs (although that’s not very useful).
  1. nohup是GNU-Coreutils,仅在创建进程时使用
  2. disown是shell-built-in, disown只能操作后台程序,因为在前台你都不能输入disown这个命令。。。
  • disown原本是用于作业管理,同时也可以指定某个作业忽略SIGHUP信号
  • nohup是用于信号处理,用于在创建进程的时候声明本进程不接受SIGHUP信号
  • disown的进程仍然可以接受SIGHUP(当然,如果指定了-h指令,进程就不能接受SIGHUP信号了)
  • 使用nohup开启的进程仍然可以出现在jobs队列里面(当然,如果shell突然被关掉,队列也就消失了)
  • 总而言之两者没有必然联系
Functionstdout/stderrAfter Closing the terminal (SIGHUP)
&Run a command as a background jobInherited from the shellThe job will be stopped
disownRemove jobs in the job list– Protect jobs from the SIGHUP– Inherited from the shell– Discarded if with the -h option and the controlling terminal is closedThe job process will keep running only if with the -h option
nohupProtect a command from the SIGHUPRedirected to a nohup.out fileThe command process will keep running

把正在运行的进程放到后台

  1. 使用ctrl+Z暂停当前进程
  2. 使用bg将任务放到后台继续执行(bg本身的用途就是把暂停的作业放到后台继续执行)
  • 但是仅仅移到后台,进程仍然会继承stdout,stderr,更致命的是,仍然可以接受SIGHUP,如果想要让进程忽略SIGHUP信号,即退出登陆后进程不会被杀掉,可以使用disown -h
[1]+  Stopped                 myprogram
$ bg 1
[1]+ myprogram &
$ disown -h %1
$ logout
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值