Created
September 25, 2018 05:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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