“screen” Linux command: running long server operations independent of your network connection
First published on June 30, 2012
If you’ve manually executed any script on the command line of a remote server that took more than a few minutes to finish, you probably know how the completion of the script is dependent on your connection to the remote server. If your Internet connection drops, even temporarily, or if for some other reason you disconnect from the remote server, the script will also stop. This can be very frustrating if your script was expected to run for a few hours and you have to start it over from scratch.
nohup is one way to keep the script running no matter what happens; however, for various reasons I prefer to use it only for continuous, indefinitely running processes. For processes that are supposed to eventually finish, I recommend using screen. The basics of screen are straightforward:
- Type “screen” to open a separate shell session. Then run your command.
- Type “CTRL+a” to invisibly bring up its menu (in other words, to have it listen to your screen-specific instruction). Then type “d” to “detach” from the screen. Your command will continue to execute
- Optionally log out or drop your connection. Then log back in.
- Type “screen -r” to resume your session.