I would like to shoot a command to server to be run say forever! under "screen session" using ssh.

so I am using something like

ssh -t root@server screen -S myinf "python infinit1.py &"

where infinit1.py is the script. The problem the "python infinit1.py" doesn't go in a background and neither a screen called "myinf" is created.

I appreciate your hints.

| improve this question | | | | |
  • nohup is your friend. It will keep the command running even after ssh session closes. – Jim G. Jul 16 '13 at 19:38
  • Thanks, it would be great to help me out to integrate screen session as well :) – user702846 Jul 16 '13 at 20:19

This is because screen as invoked by you requires a pty to function. You will need to start screen in detached mode:

ssh root@server screen -d -m -S myinf python infinit1.py
| improve this answer | | | | |
  • it work; may I ask why when I replace "python infinit1.py" with a simple "ls" command it does not work ? – user702846 Jul 17 '13 at 5:04
  • oh, it does work, but ls will terminate quickly, taking screen with it. You're likely to be too slow to ever see the session :) – Dennis Kaarsemaker Jul 17 '13 at 5:54
  • can I specify a pending time or something similar to avoid the quick termination ?! thank you. – user702846 Jul 22 '13 at 22:19

do this

 ssh -t root@server "nohup bash -c '( ( /usr/bin/python infinit1.py &> /dev/null ) & )'"

this is because the tty that opened need to close and thus cannot keep the command running. to see the output, change /dev/null to an actual file name.

i have not tried to use remote screen so I omitted that. you may be able to get it to work but maybe the syntax is off a little bit.

| improve this answer | | | | |

You don't need the quotes or the ampersand. Use the -d -m options to start screen in a detached mode instead. Take a look at the man page for more infomation.

A working example command line is:

ssh $host screen -S myinf -d -m tail -F /var/log/dmesg
| improve this answer | | | | |
  • sudo for what !? I don't have sudo access ! – user702846 Jul 16 '13 at 23:01
  • It isn't important for the discussion. I used sudo to read a log file since I don't have infinit1.py on my test host. The important part is that everything after the options is considered a command line to run inside the screen session. – Rik Schneider Jul 16 '13 at 23:25

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.