Handling background scripts and foreground scripts became necessary part of scripting in linux. For examples,
how to avoid running a background script if the script is already running background? Can a script test itself it is already running?
how to avoid executing a script residing in $PATH from dmenu, bemenu or any other equivalent, if the script is written to run in terminal interactively or foreground?
To avoid running a background script that is already running, put the following at the top of the script.
SCRIPTNAME=$(basename -- "$0")
if pidof -x "$SCRIPTNAME" -o $$ >> /dev/null 2>&1; then
notify-send -t 9000 "Already Running" "The script is already running. So, clean exit."
exit 0
fi
.....
To avoid executing a script residing in $PATH from dmenu, bemenu or any other equivalent if the script is written to run in terminal interactively or foreground, put the following at the top of the script.
case $(ps -o stat= -p $$) in
*+*)
echo "continue..."
;;
*)
notify-send -t 2700 "clean exit" "the script is to be run only in terminal."
exit 0
;;
esac
.....