Search code examples
cbashshellgnu-screen

Can not connect to an already active screen through system() in c


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.


Solution

  • 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.