Screen commands

Anh-Thi Dinh
Keep the running tasks on the remote host continue after turning off the local caller.

Install

1sudo apt install screen # ubuntu

Basic command lines

1# check screen version
2screen -v
1# start new session (with name)
2screen -S <name>
1# list running sessions
2screen -ls
1# attach to a running session (without name)
2screen -x
1# attach to a running session (with name)
2screen -rx <name>
3# -x for an interactive (scrolling)
1# detach a running session
2screen -d <name> # or Ctrl + A, D
1# kill a session
2screen -X -S <name> quit

Delete sessions

  1. Reattach first: screen -r <name>
  1. Ctrl + A, K then Y
1# kill ALL auto-created sesssions
2screen -ls | grep pts | cut -d. -f1 | awk '{print $1}' | xargs kill
3
4# kill all detached sessions
5screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill

Create a screen + list of command lines

1screen -S 'dat' -dm bash -c 'cd /jekyll-site; bundle exec jekyll serve -I; exec sh'

Hotkeys

  • Detach: Ctrl + A, D
  • Reattach: Ctrl + A, R
  • Kill current session: Ctrl + A, K then Y

Errors

1# Cannot make directory '/run/screen': Permission denied
2mkdir ~/.screen && chmod 700 ~/.screen
3export SCREENDIR=$HOME/.screen
4# also add this line to ~/.zshrc or ~/.bashrc

Reference