Keep processes running in the background and even after SSH session disconnects

Problem

I started running a computation-intensive python script on an SSH session. But the session kept disconnecting, and I couldn't wait so long to the end. I lost all my results all the time.

Solutions

The solutions are all about running the script in the background without interrupting. And nohup, disown, screen, tmux, reptyrjobs, bg and fg are involved.

1. If you don't disconnect the session, jobs, bg and fg work fine

  • Ctrl+z to pause the process
  • bg to run it in the background
  • fg to bring it back if you want

You can keep switching as you want. jobs is to show the running processes in the current session.

2. If you need to disconnect the session, disown works

disown -h

disown -h disown all jobs running. So it'll keep running after disconnection.

Warning: it'll still stop working if it prints out something. can be used to stopping print

3. If you need to disconnect the session but you need the output, nohup works

nohup python3 code.py &

The & makes it run in the background so we can keep interacting with the terminal. And now you are free to close the session.

If you want to bring it back to the foreground, use fg. If you want to bring it back to background, use Ctrl+z. But it's stopped after Ctrl+zbg is to make it run again. The output will be saved in a file.

4. If you want to bring it back after reconnecting, you need to create a screen or tmux first (Best solution)

For nohub, once you log off the ssh session, it's hard to bring it back again. So you have to wait until it ends.

screen works better to solve this problem.  You don't even need nohup here.

  • ssh into your remote session. Type screen. Then start the process you want.

  • Press Ctrl+A then Ctrl+D. This will "detach" your screen session but leave your processes running. You can now log out of the remote session.

  • If you want to come back later, log on again screen -rand type screen -r This will "resume" your screen session, and you can see the output of your process.

screen -r is to list all screens so that we can identify the status of each screen.

5. If you forgot screen or you want to bring it back after disown, reptyr works

  • start a screen
  • reptyr the process to it

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值