Bart C
Bart C

Reputation: 1557

How to use bash tee to redirect stdout and stderr to a file from script which has screen in the hash bang

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.

Upvotes: 0

Views: 538

Answers (1)

Bart C
Bart C

Reputation: 1557

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.

Upvotes: 1

Related Questions