Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting up a screen (unix command) + running a command in 1 command?

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!

like image 334
EpicDewd Avatar asked Jan 05 '10 16:01

EpicDewd


People also ask

What is screen command in Unix?

screen command in Linux provides the ability to launch and use multiple shell sessions from a single ssh session. When a process is started with 'screen', the process can be detached from session & then can reattach the session at a later time.

How to install the screen command in Linux?

Installation of screen command: To install the screen command simply go to the terminal and type the following command: sudo apt install screen screen: It will start a new window within the screen.

How to start a new window in Linux?

Examples: 1 Installation of screen command: To install the screen command simply go to the terminal and type the following command: sudo apt install screen 2 screen: It will start a new window within the screen. ... 3 -S: It will start a new window within the screen and also gives a name to the window. ... More items...

How do I run a command in the background in Linux?

To execute a command in the background but don’t attach to the screen session (useful for system startup scripts), run: To terminate the current screen session (not put it into the background but close ), press Ctrl + D or type: List the currently running screen sessions:

How do I use-X to execute a screen command?

-X Execute <cmd> as a screen command in the specified session. I haven't done this myself, but that's where I'd start. 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.


2 Answers

screen -d -m sh -c "while :; do ./myCommand; done;"

Explanation:

  • -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).
like image 92
Heinzi Avatar answered Oct 07 '22 08:10

Heinzi


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.

Update:

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.

like image 34
Mike Seplowitz Avatar answered Oct 07 '22 09:10

Mike Seplowitz