Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 12-16-2006, 12:46 PM   #1
Registered: Feb 2006
Location: Chile
Distribution: Slackware 10.2
Posts: 53

Rep: Reputation: 15
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.


Old 12-16-2006, 01:17 PM   #2
Senior Member
Registered: Jan 2005
Location: Manalapan, NJ
Distribution: Fedora x86 and x86_64, Debian PPC and ARM, Android
Posts: 4,593
Blog Entries: 2

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.

Last edited by macemoneta; 12-16-2006 at 01:27 PM.
Old 12-16-2006, 10:59 PM   #3
LQ Guru
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 256Reputation: 256Reputation: 256
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.


screen, script

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
'cannot stat' script in /etc/rc.d/, try to run script at startup quintan Linux - Software 1 11-21-2005 02:53 AM
How to turn off screen blanking screen saver in run level 3? Arodef Linux - Software 1 01-04-2005 12:50 PM
on Network Up Script run? On Battery power run script? v2-ncl Linux - General 0 12-08-2003 09:34 AM
can't run a script script from icon in konqueror scottsteibel Linux - Software 1 08-02-2003 07:59 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:23 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration