debek
debek

Reputation: 389

Run command in new screen window

I have a small problem. I want to create a new screen and after that automaticly run echo 'You are using screen'. I tried this but doesn't work:

[root@test ~]# screen -S screen_name -X echo 'You are using screen'
No screen session found.

Have you got any solution for this?

Upvotes: 2

Views: 1608

Answers (1)

ottomeister
ottomeister

Reputation: 5828

From the error message, it appears that you did not create a screen session named "screen_name" before you tried to send your echo command into that session. Try the following sequence of commands.

First, create a screen session named "screen_name". For convenience also specify the -d -m options to start this session in a detached mode, so that you can immediately proceed to run more commands without having to detach from the new session.

[root] screen -S screen_name -d -m

Check that the new session really does exist:

[root] screen -ls
There is a screen on:
    83216.screen_name   (Detached)
1 Socket in /var/folders/k4/fdf21jn93jv5n3h5b1w9mpkm0000gp/T/.screen.

Send your command into the session:

[root] screen -S screen_name -X echo 'You are using screen'

You won't see anything interesting happen when you do that, because the session is not attached to a terminal, but you will notice that you don't get an error message. You also won't see the result of that echo command when you attach to the session later, because echo only pops up a status-line message in the session, and that status line will be dismissed as you attach to the session.

If you want to do something that will be visible when you attach, try something like:

[root] screen -S screen_name -X exec cal 2 2020

Then when you attach to the session by doing:

[root] screen -r screen_name

you will see the result of the cal command (a calendar printout for the month of February 2020) in the screen session.

Upvotes: 2

Related Questions