Thursday, November 13, 2014

GNU screen for long running server processes

Use screen to keep a long process running on a server after you close the ssh session. I started a screen session with:
    screen -S sessionname
In order to find the screen session later you might want to rename it using sessionname. Or on the first screen invocation use the s flag -S sessionname
I started the R software in this screen session, started a long running process. Then detached the session with:
I could re-attach the session later with:
    screen -r sessionname
If the session was not detached properly, it might be necessary to detach it and re attach it:
   screen -d -r sessionname
Screen detach before leaving the ssh session. You may want to use the autodetach configuration option.

Log out of your ssh session. Log in back again later:
screen -ls # list sessions
screen -r sessionname # attach a session

There might be solutions to move an already started process to GNU screen but its not straightforward:
"You cannot do this, easily. I'd suggest making it a habit to start screen as the first thing you do after opening a console. However, for your actual problem, there's another thing you could try: after having launched your job from the terminal, background it by typing ctrl-z and then bg. After that, detach the job from it's parent shell; in bash you'd do disown -h %. After that, you can safely close the terminal and the job will continue running."

More tips in this discussion on screen.

No comments: