AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 8653
Accepted
Olivier Lalonde
Olivier Lalonde
Asked: 2010-10-22 04:09:13 +0800 CST2010-10-22 04:09:13 +0800 CST 2010-10-22 04:09:13 +0800 CST

如何在结束 ssh 会话后保持进程运行?

  • 772

假设我从 ssh 会话中启动了一堆进程。是否可以在保持这些进程在远程计算机上运行的同时终止 ssh 会话?

ssh
  • 14 14 个回答
  • 1086414 Views

14 个回答

  • Voted
  1. Best Answer
    tongpu
    2012-11-24T01:26:10+08:002012-11-24T01:26:10+08:00

    You should look for modern alternatives like tmux.

    tmux is superior to screen for many reasons, here are just some examples:

    • Windows can be moved between session and even linked to multiple sessions
    • Windows can be split horizontally and vertically into panes
    • Support for UTF-8 and 256 colour terminals
    • Sessions can be controlled from the shell without the need to enter a session

    Basic Functionality

    To get the same functionality as explained in the answer recommending screen, you would need to do the following:

    • ssh into the remote machine
    • start tmux by typing tmux into the shell
    • start the process you want inside the started tmux session
    • leave/detach the tmux session by typing Ctrl+b and then d

    You can now safely log off from the remote machine, your process will keep running inside tmux. When you come back again and want to check the status of your process you can use tmux attach to attach to your tmux session.

    If you want to have multiple sessions running side-by-side, you should name each session using Ctrl+b and $. You can get a list of the currently running sessions using tmux list-sessions or simply tmux ls, now attach to a running session with command tmux attach-session -t <session-name>.

    tmux can do much more advanced things than handle a single window in a single session. For more information have a look in man tmux or the tmux GitHub page. In particular, here's an FAQ about the main differences between screen and tmux.

    • 1047
  2. coteyr
    2012-11-28T18:58:12+08:002012-11-28T18:58:12+08:00

    Option 1: nohup

    The best way is often the simplest.

    nohup long-running-command &
    

    It was made specifically for this, it even logs stdout to nohup.log.

    man nohup
    

    Option 2: bg + disown

    ctrl+z
    bg
    disown -h
    

    If you want to "background" already running tasks, then Ctrl+Z then run bg to put your most recent suspended task to background, allowing it to continue running. disown will keep the process running after you log out. The -h flag prevents hangup.


    screen and others can do it, but that's not what they're for. I recommend nohup for tasks you know you are going to leave behind and bg for tasks you're already running and don't want to re-start.

    Keep in mind, both are bash specific. If you're not using bash, then the commands could be different.

    • 519
  3. Lincoln
    2010-10-22T04:20:01+08:002010-10-22T04:20:01+08:00

    您可以使用screen.

    键入man screen以了解更多信息或阅读此屏幕手册页。

    简单场景:

    • ssh 进入你的远程盒子。键入screen然后开始您想要的过程。

    • 按Ctrl-A然后Ctrl- D。这将“分离”您的屏幕会话,但让您的进程继续运行。您现在可以退出远程框。

    • 如果您想稍后再回来,请再次登录并键入screen -r这将“恢复”您的屏幕会话,您可以看到您的进程的输出。

    • 263
  4. bassgey
    2010-10-22T05:19:23+08:002010-10-22T05:19:23+08:00

    screen 和 nohup 是更好的方法,但是如果您必须分离已经在没有 screen 或 nohup 的情况下运行的进程,您可以运行 disown 命令。

    disown [-ar] [-h] [jobspec… |pid… ]

    Without options, remove each jobspec from the table of active jobs. If the -h option is given, the job is not removed from the table, but is marked so that SIGHUP is not sent to the job if the shell receives a SIGHUP. If jobspec is not present, and neither the -a nor the -r option is supplied, the current job is used. If no jobspec is supplied, the -a option means to remove or mark all jobs; the -r option without a jobspec argument restricts operation to running jobs.

    With disown you can close the terminal and get the process running on the machine.

    • 98
  5. Chaos
    2013-11-25T19:30:49+08:002013-11-25T19:30:49+08:00

    I was stuck in a large mv so I wasn't in a position to stop the process, setup screen and then start it again. I managed to exit the SSH session with the process running by essentially doing the following steps:

    1. Establish SSH connection: ssh user@host
    2. Run the desired command to start the process
    3. Press Ctrl+Z to pause the process
    4. Run bg to put the paused process in the background and resume it.
    5. Run disown [pid] (process ID is optional, defaults to last process) to disown the process. To get a list of jobs simply type jobs before.
    6. Exit the SSH session by running logout.

    Usage of the disown command:

    disown [-ar] [-h] [jobspec ... | pid ... ]
                  Without  options,  remove  each jobspec from the table of active
                  jobs.  If jobspec is not present, and neither the -a nor the  -r
                  option  is  supplied, the current job is used.  If the -h option
                  is given, each jobspec is not removed from  the  table,  but  is
                  marked  so  that  SIGHUP  is  not  sent  to the job if the shell
                  receives a SIGHUP.  If no jobspec is  supplied,  the  -a  option
                  means  to  remove or mark all jobs; the -r option without a job‐
                  spec argument restricts operation to running jobs.   The  return
                  value is 0 unless a jobspec does not specify a valid job.
    
    • 85
  6. SiddharthaRT
    2012-11-25T14:09:39+08:002012-11-25T14:09:39+08:00

    There are two major programs you can use to maintain programs and terminal state over multiple ssh connections. They are screen (the incumbent, but unfortunately unmaintained. Apparently being actively developed now) and tmux (newer, actively maintained). Byobu is a front end that can run on top of their of these systems and offer additional ubuntu status information. On new installations it will use tmux as a backend, if you have an older installation of byobu and an existing config it will maintain the previous backend, be it screen or tmux.

    Byobu

    Byobu can be installed on the computer by doing so in a Debian-based machine:

    sudo aptitude install byobu
    

    Using yum, you do

    su -c 'yum install byobu'
    

    It's also possible to install byobu on other distributions.

    Using byobu

    You can start byobu by running byobu on the host machine after connecting using ssh. This will give you a shell that looks like this:

    image-byobu

    You can also use Byobu Terminal on a Ubuntu machine with -X option and easily have a perfectly working byobu.

    Usage:

    Start byobu by typing byobu.

    You can press F2 to create a new window within the current session, F3-F4 to switch between the various windows.

    The best part about byobu is, you dont have to actually kill the processes running in the terminal to leave the terminal. You can simply send screen/tmux (the skeleton of byobu) to background and resume the next time you come:

    • To leave byobu and keeep it running (detach) press F6.

    • The next time you come, just do byobu and you sholud be back right where you were.

      byobu-detach-attach

    You can also create various byobu sessions by byobu -S session1 and so on. And you can connect to either of them when you come back.

    You can do much more using Byobu. Use it! Some definitive guides are here, or here.

    • 28
  7. Richard Holloway
    2010-10-22T04:16:58+08:002010-10-22T04:16:58+08:00

    一旦过程开始,您就无法执行此操作,您需要在运行长时间运行的作业之前进行设置。

    您可以使用nohup,但现代智慧建议您使用 screen 或 byobu 作为登录名,这样您就可以分离并保持运行。

    Screen 的优点是您可以从一台机器上分离并从另一台机器上重新连接,如果您想检查在工作日结束后运行的长时间运行的进程,这很方便。

    这里有一个合理的筛选入门指南。

    byobu在带有菜单等的屏幕顶部放置了一个易于使用的界面。它也是较新的 ubuntu 上当前的屏幕实现。F2 启动一个新的端子 F3/F4 来回切换,F6 断开。键入 exit 以实际永久结束终端。

    • 20
  8. user108430
    2014-07-30T06:58:11+08:002014-07-30T06:58:11+08:00

    For a single shell script that I have running over a long period of time, I will login, and run the process in the background using '&'.

    Example:

    /path/to/my/script &
    

    I've logged out and disconnected my SSH session. When I log in some time later, the script is still executing as proven by continuous data collection from the script.

    • 10
  9. Jorge Gutierrez
    2011-06-07T13:29:04+08:002011-06-07T13:29:04+08:00

    Hey, while I agreed that screen is the most efective option. You can use vncserver and then start the process on it.

    此外,如果您唯一的兴趣是让进程运行并且不需要控制它,并且最重要的是您不知道您将需要关闭会话并且您已经运行了进程,那么如果您不走运的话你使用 bash 作为 shell

    首先,您需要通过键入 Ctrl+Z 后跟 bg %1 将进程发送到后台(数字取决于作业编号,通常为 1,但您可以使用命令作业轻松拉出列表)

    最后调用命令 disown (后跟 jobid ...与 bg 命令相同)

    这将删除你的 shell 和后台进程之间的父子关系,防止它在你的 shell 终止时死掉。

    • 8
  10. Zibri
    2017-01-11T19:52:30+08:002017-01-11T19:52:30+08:00

    The easiest way is to run your command in the background with &. Then just write:

    disown -a
    
    • 5

相关问题

  • Gnome-terminal 快捷方式在选项卡中打开多个 ssh 连接

  • 通过 SSH 禁用密码访问?

  • ssh 如何使用多个私钥?

  • 如何通过一个 SSH 连接拥有多个终端会话?

  • 如何与无头服务器进行图形交互?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何安装 .run 文件?

    • 7 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    如何获得 CPU 温度?

    • 21 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Marko Smith

    如何使用命令行将用户添加为新的 sudoer?

    • 7 个回答
  • Marko Smith

    更改文件夹权限和所有权

    • 9 个回答
  • Marko Smith

    你如何重新启动Apache?

    • 13 个回答
  • Marko Smith

    如何卸载软件?

    • 11 个回答
  • Marko Smith

    如何删除 PPA?

    • 26 个回答
  • Martin Hope
    NES 如何启用或禁用服务? 2010-12-30 13:03:32 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    Olivier Lalonde 如何在结束 ssh 会话后保持进程运行? 2010-10-22 04:09:13 +0800 CST
  • Martin Hope
    David B 如何使用命令行将用户添加为新的 sudoer? 2010-10-16 04:02:45 +0800 CST
  • Martin Hope
    Hans 如何删除旧内核版本以清理启动菜单? 2010-08-21 19:37:01 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve