Skip to content

Instantly share code, notes, and snippets.

@dazeb
Created January 25, 2024 17:06
Show Gist options
  • Save dazeb/61cd1f31dccbae9a92e8f5658af79473 to your computer and use it in GitHub Desktop.
Save dazeb/61cd1f31dccbae9a92e8f5658af79473 to your computer and use it in GitHub Desktop.
This is a BASH snippet meant for use at the beginning of a shell/bash script that adds SCREEN every time we run the script.
#!/bin/bash
# Check if we are running inside a screen session
if [ -z "$STY" ]; then
# Check if screen is installed
if ! command -v screen &> /dev/null; then
echo "Screen is not installed. Would you like to install it now? (y/n)"
read -r install_screen
if [[ $install_screen == "y" ]]; then
# Install screen (Debian/Ubuntu example)
sudo apt-get install screen -y
else
echo "Screen is required to safely run this script."
exit 1
fi
fi
# Start a new screen session with a unique name based on the current timestamp
SESSION_NAME="script_session_$(date +%s)"
screen -dmS "$SESSION_NAME" "$0" "$@"
echo "Started a new screen session named $SESSION_NAME"
echo "If you get disconnected, log back in and type 'screen -r $SESSION_NAME' to reattach."
exit 0
fi
# The rest of your script goes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment