Start detached screen session

I would like to start screen detached session, but I want to evaluate some bash. It seems screen only supports an executable and binary. I want do do some magic like:

screen -d -m script.sh 2>&1 | tee logfile.log

but this apparently does not work. Quoting does not help here. The only idea I have is to create a wrapper script that will do this redirection and then to call it directly, like:

screen -d -m wrapper_script.sh

Any other ideas? Thanks.


You could have screen run a bash shell, which in turn runs your script.

I just tested this:

screen -d -m bash -c 'vmstat 5 3 2>&1 | tee logfile.log'

which gave me this:

kenny@t520:~$ cat logfile.log 
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa
 1  0      0 5864764 103072 884512    0    0    66    36 1236 1818 17  6 77  0
 0  0      0 5864252 103072 884512    0    0     0     0  311  574  1  1 98  0
 1  0      0 5861532 103080 884512    0    0     0     4 1244 2302  4  2 94  0

So this should work too:

screen -d -m bash -c 'script.sh 2>&1 | tee logfile.log'

Have you tried specifying the -L screen option to write to a log?

You can also specify the name of the log using the file ~/.screenrc - here is the contents of mine as an example:

defutf8 on
deflog on
logfile /home/me/convert.log
multiuser on