I have a Python script test.py that only contains: print('hi'). I want to run it in a screen so that the output of the screen is saved by script.

I use the following command to run test.py in a screen, it works fine:

screen -dm bash -c 'python test.py'

However, I haven't managed yet to find a way to use script to save the output of the screen. How can I do it?

I unsuccessfully tried:

  • script -c "screen -dm bash -c 'python test.py'" output.txt: the output file output.txt doesn't contain hi, but only:

    Script started on Fri 26 Aug 2016 01:04:59 PM EDT
    Script done on Fri 26 Aug 2016 01:04:59 PM EDT

I use Ubuntu 14.04.4 LTS x64.



-d -m: Start screen in detached mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.

http://linux.about.com/library/cmd/blcmdl1_sh.htm :

-c string: If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.

script's man page:

-c, --command run command rather than interactive shell

4 Answers 4


You can use the -L flag to create an automatic screenlog.0 file


$ screen -dm -L sh -c 'echo hello'
$ cat screenlog.0 

If you have a long running screen session that isn't being logged then you can turn on logging later


$ screen -dm -S test sh -c 'while [ 1 ]; do date; sleep 1; done'

Now we can turn on logging

$ screen -S test -p 0 -X log

Allow some time to pass, because the logging is written in blocks, and...

$ cat screenlog.0
Fri Aug 26 13:25:49 EDT 2016
Fri Aug 26 13:25:50 EDT 2016
Fri Aug 26 13:25:51 EDT 2016
Fri Aug 26 13:25:52 EDT 2016
Fri Aug 26 13:25:53 EDT 2016
Fri Aug 26 13:25:54 EDT 2016
Fri Aug 26 13:25:55 EDT 2016
Fri Aug 26 13:25:56 EDT 2016
Fri Aug 26 13:25:57 EDT 2016
Fri Aug 26 13:25:58 EDT 2016
  • 2
    Is their a way to save output history from begining when screen is already started without -L ?
    – Nicolas
    Sep 23, 2018 at 9:55
  • Thanks for the answer. I tried the screen -dm -L bash -c 'python running_4ever.py' and see the log as well as the newly created screen as detached, I wonder how do we attach to it or kill it? I tried screen -r <screen_id> to it but didn't work.
    – Leo
    Jan 18, 2019 at 23:07
  • @Leo this sounds like you need to create a new question Jan 19, 2019 at 3:10
  • @StephenHarris Hi Stephen, I figured it out. I tried screen -dm -S <screen_name> -L bash -c "python running_4ever.py" and it worked, then I just used screen -XS <screen_name> quit to kill it. Thanks for the reply though
    – Leo
    Jan 22, 2019 at 23:16

You should do it the other way round, run script inside screen:

screen -dm bash -c 'script -c "python test.py" output.txt'

Another way to do it is to attach the screen first:

screen -r <pid or name>

and then:

Ctrl+A, H

It will start logging into screenlog.0


Could work!

screen sh -c './some-script 2>&1 | tee mylog.log'

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.