53

将正在运行的任务脱离终端运行

 5 years ago
source link: https://www.tlanyan.me/make-running-task-detach-terminal/?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

转载请注明文章出处: https://tlanyan.me/make-running-task-detach-terminal/

让任务脱离终端运行,是非常容易做到的,常用的方法有两种: tmux / screen ,或者 nohup 。如果一个正在的运行的任务,因为网络或者时间关系,需要退出当前会话,如何让其在后台继续运行呢?答案是使用 disown 命令。具体操作如下:

  1. ctrl + z 暂停任务;
  2. 执行 bg 让其转入后台继续运行;
  3. 输入 disown -h %jobid 让其脱离终端运行,其中jobid是用 jobs 命令查看的后台任务编号。如果有多个任务,可以使用 disown -a 命令.

disownbash 内置命令,作用是删除当前shell的任务。如果你的shell不是 bash ,可能没有 disown ,则可以尝试 kill 命令组合达到相同效果:

kill -s SIGTSTP pid
kill -s SIGCONT pid

参考

  1. https://stackoverflow.com/questions/625409/how-do-i-put-an-already-running-process-under-nohup

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK