Take the 2-minute tour ×

Was wondering how I can start up a command such as:

while :; do ./myCommand; done;

But instead of doing the usual

screen -S nameOfMyScreen

Then the command

while :; do ./myCommand; done;

Then detach the screen

^a ^d (Control "a" the control "d"

I would like it to start and detach. Thanks!

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted
screen -d -m sh -c "while :; do ./myCommand; done;"


  • -d -m starts screen in detached mode (create session but don't attach to it)
  • sh -c commandline starts a shell which executes the given command line (necessary, since you are using the while builtin).
share|improve this answer
The version of screen on my system says that the -c option is to read an alternate config file instead of .screenrc. –  MikeSep Jan 5 '10 at 16:39
The -c is not passed to screen but rather to sh. After the screen command line parser reads sh (i.e. something without a dash), it know that no more options follow and that everything else is a command that should be executed. –  Heinzi Jan 5 '10 at 16:40
Ahh, I stand corrected. :) –  MikeSep Jan 5 '10 at 20:53
add comment

From screen -h, these look useful:

-dmS name     Start as daemon: Screen session in detached mode.
-X            Execute <cmd> as a screen command in the specified session.

I haven't done this myself, but that's where I'd start.


The top of the help also says

Use: path/to/screen [-opts] [cmd [args]]

so the -X switch may be to execute a screen command as opposed to a shell command. You might just be able to put your command after the -dmS <name> without any -X switch.

share|improve this answer
+1. A combination of these will do your job. Since your command is a script, you might have to either put it in a file and pass that or use bash -c or something similar. –  Noufal Ibrahim Jan 5 '10 at 16:33
add comment

Your Answer


