1

First off, I know about tmux and screen. My conundrum: I started a shell script over SSH expecting it would take a short amount of time, so I left my computer on overnight.

I can't terminate and restart the shell command, otherwise it will restart from the beginning. I would like to terminate my SSH connection without terminating the shell script. Is there anything I can do retroactively (i.e. command is already running in SSH terminal) that will detach it and allow me to safely disconnect my computer?

4
  • You just need to use ‘bg and fg’ that’s itCommentedMay 21, 2019 at 16:29
  • And tough luck if SIGHUP (or systemd, despite nohup kills it anyway).
    – user313992
    CommentedMay 21, 2019 at 16:38
  • @JaredNielsen there's reptyr which should allow you to move it to another tty, but frankly I haven't used it (I've only used some similar hack done by myself). But I guess you're not into experimenting, so you better leave your computer connected & running now ;_)
    – user313992
    CommentedMay 21, 2019 at 16:42
  • you can also disown the process - but you can't (easily) get it back.
    – DopeGhoti
    CommentedMay 21, 2019 at 18:07

2 Answers 2

1

I wonder if detach might work? Usually if you do that on the existing terminal session, you can then exit that shell and the process you have 'detached' will continue to run. You will still need to have suspended and backgrounded that process first. The effect is as if you had used the nohup command.

However I note that some responses indicate systemd may still kill your process -- I have not seen that to date myself so be aware.

EDIT: *DUH* for 'detach' read disown at least on CentOS and cousins. My apologies to anyone I misled. Although I do recall a 'detach' command from somewhere ...

    0

    One thing you can do is pause the job with CTRL+Z. Then have it continue as a foreground job with the fg command or a background job with the bg command. Based on your question, you are going to want the bg command. Afterwards, you can confirm whether or not it worked with the jobs command BEFORE you exit out of the session. And just as a warning, after exiting out the session, you will have no way to track when or if the script finished (other than by whatever operation the script may execute). So just as a recap:

    CTRL+Z $ jobs $ bg $ jobs $ exit 

    As another disclaimer: No one but the person working on your system can know if the environment you have set up will allow you to disconnect and allow the command to continue running in the background. It should work on most setups, but you never know. If possible, I would test it on a test script to see if you can stop and restart it

    1
    • 2
      beware that systemd may still kill your background jobs -- even if they survive the tty teardown.
      – user313992
      CommentedMay 21, 2019 at 16:44

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.