Can not connect to an already active screen throug

2019-07-06 22:04发布

问题:

Here is the part of code I have written to stuff 0 to a screen session open in one of my Ubuntu terminal tabs.

char command[60];
strcpy( command, "screen -S 8305.pts-1.MYUb  -X stuff $'0'" );
system(command);

It gets compiled fine with only a warning like

ignoring return value of ‘system’,

But when it comes to running I get the message shown below:

No screen session found.

I have tried system() with other shell commands and it works perfectly fine. The command for screen also works fine when you run it in a terminal session not in c code.

回答1:

Most probably you are running the command as a different user than the user that owns the screen. For example running the binary as sudo.

You can run ps aux to find the user under which your binary is running as.

To make the system command work you should run it as the user who owns the screen.



回答2:

I think the problem is you are using -S which creates a new named screen, and -X which submits a command to an already running screen session.

You either want:

system( "screen -S 8305.pts-1.MYUb cmd" );

OR

system( "screen -r 8305.pts-1.MYUb -X cmd" );

FYI -- I'm not sure what stuff $0 is suppose to be, and in the context of the code you supplied is not going to work -- but I believe that a different problem then the one you reported.

From man(1) page

-S sessionname
When creating a new session, this option can be used to specify a meaningful name for the session.

And

-X Send the specified command to a running screen session.