Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How can I get screen to execute a command and then detach (That is, automatically in a single script without further input beyond initially starting the script)? e.g. I run myscript.sh and it automatically starts a screen session, executes a command, then detaches.

share|improve this question
add comment

3 Answers

up vote 8 down vote accepted

This is an easy one:

screen -d -m yourcommand
share|improve this answer
    
no dice. screen -d -m command, screen -list says no sockets, screen -r no sessions –  darkfeline Jul 29 '12 at 2:24
    
Somehow your command wasn't found, or isn't working correctly in the automatically-created screen environment. Try just doing screen yourcommand without the -d and -m and see how that goes first. –  Alan Curry Jul 29 '12 at 2:28
    
You're sort of right. screen terminates when the command finishes, contrary to my expectations. But your answer does work. –  darkfeline Jul 29 '12 at 2:34
1  
You can change that by enabling the zombie option in screen. Put zombie xy in your ~/.screenrc. It should also be possible to enable it for one session only b putting zombie xy in another file and using -c file but for some reason that's not working when I try it. Or just use sh -c 'yourcommand;while :;do sleep 9999; done' –  Alan Curry Jul 29 '12 at 2:39
add comment

Run the command, follow it by screen command like:

screen myscript.sh

Then press Ctrl+A (release) and then D to detach the process. To resume detached process, use:

screen -r

To list your sessions, try:

screen -list
share|improve this answer
    
Thanks, but I meant in a script, including the detach part. I've edited my question to help clarify. –  darkfeline Jul 29 '12 at 2:14
    
sorry, I misunderstood the question:) –  kenorb Jul 29 '12 at 2:35
add comment
#!/bin/bash
FECHA=$(date +%Y%m%d)
ARCHIVO="lista$FECHA.csv"
screen -dmS daily_sync rsync --bwlimit=450 -av --files-from=$ARCHIVO / [email protected]:/var/www/
share|improve this answer
2  
You don't seem to be answering the right question. How did rsync enter into this? –  terdon Feb 21 '13 at 18:57
add comment

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.