Linux Bash Scripting: Force Screen if not Cron Task
Aug 30, 2012 · 1 minute readCategory: linux
If you have a long running script that is designed to run as cron but may be run manually from the terminal then you may want to enforce screen so that the process can’t be aborted by the SSH terminal being closed for whatever reason.
We already blogged about how to force running as screen here.
This is an extension of this that also checks for running as Cron and if so, does not force screen.
# First off, ensure running from Cron and if not, Screen
if [ -t 1 ]
then
echo "Not running as Cron Task"
if [ -n "$STY" ];
then
echo 'We are inside screen, good';
else
echo "Not in a screen, please run with screen"
echo "Exiting"
exit 1
fi
else
echo "Running as Cron Task"
fi
Simply paste this somewhere close to the top of your script before it does anything and this will work nicely.