10

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.

11

There is an environment variable for that. If it is being run within screen, $TERM will be screen. It's that simple; just do an if on it. For instance,

if [ $TERM != 'screen' ]; then echo foo; else echo bar; fi

will echo bar when in screen; foo otherwise.

  • 2
    It could also be screen-256color if the user has configured 256 colour support. – mgorven Oct 3 '12 at 6:00
  • or screen-bce on systems with byobu installed. echo $TERM screen-bce – nelaaro Oct 9 '12 at 7:53
7

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.

  • 1
    While running with nohup would achieve the same outcome of ensuring the process continues to run would I not still have the same issue of someone potentially forgetting to run the script with nohup? Or is there a way in a script to ensure 'nohup' was used or do I need to create a wrapper script which calls my original script with nohup? – cclark Oct 3 '12 at 6:04
  • Yeah. While I love my padawans (junior sysadmins) like I love my kids, they frequently forget those kinds of things. So everything gets wrapped up so that they don't have to remember what things to type in to prefix the important commands. – Magellan Oct 3 '12 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.