Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am working on automating a maintenance routine that involves starting and stopping a script that is running in a screen session. My approach is to kill the screen session, and then restart it and run the command from within a script using the abilities to both create a screen and pass a command without needing to attach to the screen.

However, I am having difficulties with this. I can create the screen correctly without it attaching using screen -d -m -S screen_name. However, if I run a command based on:

screen -S screen_name-X stuff "command 1"'echo -ne '\015''"command 2"'echo -ne '\015''

with the echo -ne '\015' being wrapped with backticks rather than single quotes. It is to simulate the user pressing the enter key as the commands I use are moving to a directory and executing a script located there. This command works, but only if the screen has been attached to once it has been created. As I am trying to automate the process of creating the screen and running the commands within it I would like to avoid having to attach and detach within a script. I will be trying the suggestion of creating a shell script containing the commands I need to execute within the screen and edit according to my results.

Is there a way to create a screen and run a command within the screen either in one command, or without having to attach to the screen after creating but before execution of the command?

Thanks in advance.

*Update - having tried the suggestion to place the commands I need to execute within a shell script I have been able to successfully create a screen and execute the commands from within the screen, but I am getting the behaviour that when the script stops running the screen closes as well. This shouldnt be a problem as the script is a logging script that should only stop with the knowledge of the sys admin or through the script I am trying to develop, however it would be preferable to have the screen setup in such a way that the screen does not disappear if the script is stopped. Is it possible to achieve this behaviour? *

share|improve this question
    
I ran into this same problem and found a solution on superuser.com for anyone else who stumbles upon this problem superuser.com/questions/342463/… –  Dan Herbert Jul 7 '12 at 1:50
    
To keep the screen open you could use something like: screen bash -c 'echo "test"; /bin/bash' –  gwyn Nov 28 '12 at 17:30
    
For your auto-closing issue, you could use a special .screenrc that contains the line zombie kr, which will keep a finished window open, and you can press k to close the winodw, or r to run the command in the window again. I have this for my default .screenrc. –  Martin C. Jul 11 at 8:56

4 Answers 4

I think you may be running into several issues.

If the command finishes before you re-attach, the screen will go away. You can demo this by using:

screen -d -m ls -l

It will run the ls -l command, but screen -list won't show it as the screen process has ended.

I also have no idea what you are trying to do with this \015 stuff. Perhaps updating your question would help, as what I think you're trying to do is run multiple commands in a screen session. This should be as simple as:

screen -d -m bash "command1 ; command2 ; command3"

If these are used a lot, perhaps you should make a shell script that runs just these commands, then use a more simple:

screen -d -m /path/to/script.sh
share|improve this answer
    
is there a way to get similar behavior without the screen detaching immediately? Say I want to start an ssh session and then detach right after; screen -d -m ssh will detach at the password prompt before the session is made. –  Trevor Alexander Dec 23 '13 at 7:06

I've encountered this problem before, it was a bug with the cygwin implementation.

What I did was create a ".screenrc_detaching" having only the following command

#detach
detach 

and then start screen with

screen -c ~/loginScripts/tempScreenrc/.screenrc_detaching

Then you have your screen session and its already been attached and detached and you're good to pump commands to it.

Easy ! :P

share|improve this answer

Use screen -X -S -p 0 [Your command. Yes, it requires the code for Enter key press]

http://osdir.com/ml/gnu.screen/2003-09/msg00029.html

share|improve this answer

You can use expect or autoexpect.

See: http://expect.nist.gov/

share|improve this answer
    
You may also want to try pexpect -- expect in python -- noah.org/wiki/Pexpect –  Clinton Blackmore Jan 20 '10 at 19:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.