2

I want to do the following: After reboot I want to start a screen which then should execute a script. Therefore I searched for crontab and screen and I also did find some answers. However, nothing works for me. I am running a ubuntu linux with screen installed and I want to run a python script.

The following works screen -d -m which opens a screen but does not run anything. Instead, I'd like to create a screen and start a script. From what I found on the net answers told that this is accomplished by screen -d -m scriptname. In my case scriptname is main.py (which I checked that it is executeable).

However, screen -m -d /path/to/main.py does not create a screen. This is my console output

$ screen -d -m /opt/main.py
$ screen -x
There is no screen to be attached.

However, this works but does not execute the python script:

$ screen -d -m
$ screen -x

What am I doing wrong here? Thanks :)

  • What is the console output of /opt/main.py without screen? – Ipor Sircer Nov 28 '16 at 11:42
  • It is a print("blabla"). However, I do not understand why this is important. The script works – Tom Tom Nov 28 '16 at 12:42
  • Yes, and when the script finishes, screen terminates itself. What's wrong with that? – Ipor Sircer Nov 28 '16 at 12:43
  • Ah, okay. You mean that the termination of the progam automatically quits the screen? Okay, but why does a screen -d -m not do the same? – Tom Tom Nov 28 '16 at 12:44
  • Nope, this is not the case. The screen is not terminated after execution. I modified my python script such that is take about 1 minute to complete. However after screen -d -m /opt/main.py I get again the response that There is no screen to be attached.. I do not think that this is reason why it does not work – Tom Tom Nov 28 '16 at 12:48
0

Actually screen -d -m /path/to/executable-python-script.py works fine.

However, after the script has terminated, the screen is automatically closed. Also, keep in mind that something like screen -d -m /usr/bin/python3 /path/to/executable-python-script.py does NOT work!

So, use for instance the following python script testscript.py to test:

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import time

def main():
    for i in range(10):
        print("Python3 Script " + str(i))
        time.sleep(5)


main()

And then do:

  1. chmod +x testscript.py
  2. screen -d -m /path/to/testscript.py
  3. Quickly do a screen -x to check that the script is actually running

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.