Skip to content

Instantly share code, notes, and snippets.

@henri
Last active September 30, 2024 20:17
Show Gist options
  • Save henri/60d7db9b6961a90fa40e91b3c1662fe7 to your computer and use it in GitHub Desktop.
Save henri/60d7db9b6961a90fa40e91b3c1662fe7 to your computer and use it in GitHub Desktop.
screen cheat sheet
# screen is useful command as it is included by default on some distributons / operating systemss (eg MacOS).
# even if you really like tmux, screen is useful in some situations. Both screen and tmux have a good use cases.
# list all screen sessions
screen -list
# start a command in screen
screen -S my_commands_awesome_name -md bash -lc /usr/bin/path_to_my_awesome_command
# start a command via ssh in screen
ssh -i .ssh/my_identity_file -p 22 [email protected] "screen -S name_of_session -md bash -lc \"/usr/bin/my_super_command\" && exit"
# execute a set of bash commands (with pipes) in a detached screen
screen -S log_memory_now -dm bash -c '~/bin/my_memory_usage.bash | tee -a /tmp/memory_usage.log'
# create an alias (use funcsave / store in .alias / .bashrc for quick reference
# https://github.com/henri/handy-alias/edit/master/linux_alias.bash
alias log_memory_now_bg "screen -S log_memory_now -dm bash -c '~/bin/my_memory_usage.bash | tee -a /tmp/memory_usage.log'"
# execute multiple commands in a detached screen called "do_it_later"
screen -S do_it_later -dm bash -c "sleep 50; echo do_it_later > /tmp/do_it_later.txt"
# leave screen running after the command compeltes.
screen -dm bash -c 'sleep 50; exec fish'
# name a screen when starting it up
screen -S myasomesession_name
# start a session in the background with a name
screen -S sleepyhead -dm sleep 60
# start a fish alias in detached screen session and close the session when the process completes.
screen -S my_amazing_session_name -dm fish -c my_super_fish_alias
# kill the sleepyhead session
screen -S sleepyhead -X quit
# change the name of a screen session
screen -S origional-name -X sessionname mynew-session-name
# kill detached screen sessions
screen -ls | grep Detached | cut -d. -f1 | awk '{print $1}' | xargs kill
# send a control-c to the myasomesession_name screen session (window 0)
screen -S myasomesession_name -p 0 -X stuff '^C'
# deal with hung processes (first kill the PID, then wipe the screen session)
# these sometimes show as (Dead ???) if the process has already terminated) in which case you can skip the kill command.
screen -ls # PID at the start of the session name
kill -9 <PID>
screen -wipe <PID>
# clear dead screen sockets - as per the instructions on screen - the following will clear detached screens
screen -wipe
# if you run kill but without -9 then it will typically screen (the parent process) will be notified that child process has
# been terminated and screen will automatically remove it from the list of sessions (eg - it will tidy up).
kill <PID>
# this document assumes standard bind key of control-a
# full list : https://manpages.ubuntu.com/manpages/lunar/en/man1/screen.1.html#default%20key%20bindings
# detach from screen
control-a control-d
@henri
Copy link
Author

henri commented May 19, 2022

Some very basic usage of screen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment