Skip to content

Instantly share code, notes, and snippets.

@highgain86j
Created September 25, 2018 05:04
Show Gist options
  • Save highgain86j/953503417ac24a93238d240b026a3329 to your computer and use it in GitHub Desktop.
Save highgain86j/953503417ac24a93238d240b026a3329 to your computer and use it in GitHub Desktop.
A handy bash script to run almost any programs in the background using the screen command.
#!/bin/bash
function launch(){
local execbin="`which ${target}`"
local str="`screen -list "${target}" | egrep 'Detached|Attached' | awk '{print $1}'`"
ps aux | egrep -v 'grep|ps aux|screen|SCREEN' | grep pts | grep \^${USER} | grep ${target}
if [ "${str}" != "" ]; then
echo -e "\"${str}\" is present."
echo "Reattaching... (ignoring options)"
sleep 1
str="`screen -list | grep "${target}" | grep "Attached" | awk '{print $1}'`"
if [ "${str}" == "" ]; then
screen -r ${str}
else
screen -rx ${str}
fi
else
screen -dmS ${target} ${execbin} "${@}"
echo -e "Created \"`screen -list | grep "${target}" | awk '{print $1}'`\", running ${target} with PID:`pidof ${target}`."
fi
}
args0=("${@}")
args1=(`for ((c=1; c<${#args0[@]}; c++)); do echo "${args0[${c}]}"; done`)
target=${args0[0]}
launch "${args1[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment