0

I have a script which needs to run in screen so I included

#!/usr/bin/screen /bin/bash

as the hash bang and it works great. The only problem is that when the script crashes I don't know what happened, the output is lost and all I know is that screen terminated.

My script is interactive so I need to see stdout and stderr in the terminal and I also want stdout and stderr logged in case it crashed.

I tried to run the script like

./test-screen-in-bash.sh 2>&1|tee test1.log

which results in an empty test1.log file

Can somebody please explain what am I doing wrong.

Bart C
  • 1,509
  • 2
  • 16
  • 17
  • 1
    It creates a new terminal, your `2>&1` is for the current terminal and therefore is not connected to the new one and recieves none of it's data. One way to log the data is to use the -L flag with the screen command. –  May 13 '15 at 13:36
  • Thanks @JID, It work great on it's own but not sure how to include this in the hash bang, I tried `#!/usr/bin/screen -L /bin/bash` but it says L is an unknown option tough I have -L in there. – Bart C May 13 '15 at 13:54
  • 1
    I'm pretty sure you can't, is there a reason it needs to be the shebang ? If you want to run it inside screen you can do something similar to the top answer [here](http://unix.stackexchange.com/questions/162133/run-script-in-a-screen). It will allow you to pass all the flags you want :) –  May 13 '15 at 14:00
  • Thanks again @JID. The link you provided was what I was looking for. – Bart C May 13 '15 at 15:08
  • No problem, glad to have helped –  May 13 '15 at 15:11

1 Answers1

1

Thanks to @JID comments I was able to find what i was looking for.

I removed the screen from hash bang and used the method from the link provided by @JID here in the first answer.

I ended up with

#!/bin/bash
if [ -z "$STY" ]; then exec screen -L /bin/bash "$0"; fi
./myscript.sh

Now when I run the above, myscript.sh runs in screen and the whole output from the session is dumped to screenlog.n files.

Community
  • 1
  • 1
Bart C
  • 1,509
  • 2
  • 16
  • 17