-   Linux - Software (
-   -   I need to run screen from a script (

DavidHB 12-16-2006 12:46 PM

I need to run screen from a script
I need to run "screen" from a script. Is it possible? Is a there a substitue?

What I need to do is start a session run a program and then remotely be able to connect to that session and see the stdout results. Needs to be in real time, so having the program write to a file and then read the file remotely from another console doesn't do it.



macemoneta 12-16-2006 01:17 PM

TCL/Expect was made for this. You can use screen as part of the process. For example:


set error 0

spawn -noecho /usr/bin/ssh user@host

expect "$ "
after 1000
send "screen\r"

expect {
  "$ " {
      after 1000
      send "ls\r"
  timeout {
      set error 1

after 1000
send "\01\04"

exit $error

This will connect via ssh to user@host, run screen, issue the command 'ls' and disconnect. When you reconnect to the session, you will see the output of the 'ls command.

Update: I changed the code to not wait for the completion of the command ("ls" in the example), since I think you want to use this to start a long running command.

trickykid 12-16-2006 10:59 PM

Why not just launch the screen session in a detached mode?

From the man page:


-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.
/usr/bin/ssh user@host /usr/bin/screen -d -m /path/to/command

The only downside to this is if the command you execute terminates, it will kill the screen session. But if it's a command that is long running or running a continuous daemon or service, shouldn't give you too much problems as easy way to start the screen and come back to it later to see what's going on if there's console output.

All times are GMT -5. The time now is 07:02 AM.