We have a long running process on a remote server which is kicked off manually from time to time. Unfortunately internet connections die, kernels panic and a whole host of other challenges can be encountered by the ssh session used to connect and start the job.
Screen seems like a perfect way to ensure the process isn't interrupted. However, relying on a person to always remember to start screen and then run the script to start the process is a problem waiting to happen. Is there a way from the script I can check to see if the script is being called from within screen? If it is the process will continue otherwise the script will exit with a message instructing the user to run screen first.
There is an environment variable for that. If it is being run within screen,
$TERM
will bescreen
. It's that simple; just do an if on it. For instance,will echo bar when in screen; foo otherwise.
While you're working with screen, why not run the script with 'nohup'? This will also ensure that it continues to run if you get disconnected.
It outputs to a file by default, so you can use other tools to monitor script output if you wish.