LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Sending text to process (https://www.linuxquestions.org/questions/linux-newbie-8/sending-text-to-process-736351/)

Gachl 06-29-2009 04:47 AM

Sending text to process
 
Hi!
I have some TF2 servers running in a screen, now I want to create a little script that sends a specific text to all of these servers. Now finding the PID etc isn't a problem, more the sending text to process. How do I do that?

eg.
Code:

for PID in `pgrep -x srcds_run`; do $PID < "sm_reloadadmins\n"; done

david1941 06-29-2009 04:57 AM

You might investigate named pipes: http://www.linuxjournal.com/content/...pes-fifos-bash

Gachl 06-29-2009 04:59 AM

how would I make the process read the pipe?

david1941 06-29-2009 05:06 AM

Have the receiving process read the pipe; it will block waiting for the input. When data is written, the receiving process will read it and take whatever action is coded.

Gachl 06-29-2009 05:13 AM

Well, I still have to write in the process itself
The TF2 server is basically a foreground application for the console where I can type commands and it executes it on the server (eg. I type status and it writes the current server status).
now if I use this:
Code:

./start_server_cp < serverfifo
I won't see any IO of the server's console.

edit: no thats wrong, the process simply stopps until something is in serverfifo..

david1941 06-29-2009 06:17 AM

If you just need a state indication, you could do something like this:
Code:

until [ -f /tmp/something ]; do echo hi; done;
and write the flag flie like this from another process:
Code:

touch /tmp/something
That way the flag fle could be used to control as many processes as you wish.

Gachl 06-29-2009 08:20 AM

This is WAY too complex..
Is there a way of mapping the stdin of a process to two sources, which would be in my case my keyboard and my named pipe? (And of course stdout to the console). And all this without blocking.

pixellany 06-29-2009 08:27 AM

Quote:

Originally Posted by Gachl (Post 3590011)
Is there a way of mapping the stdin of a process to two sources, which would be in my case my keyboard and my named pipe?

Assuming that this IS possible, it does not sound like a very good idea.......How would the process know which source to pay attention to?

Gachl 06-29-2009 08:33 AM

Good point.
I also could set my pipe as stdin and stdout and then use some kind of weird contraption of tee and tail -f to read+write the servers I/O.
But for some reason
Code:

./start_server_testing <> serverfifo
doesn't write the servers output to the serverfifo *confused*.

Edit: assuming multiple input sources are possible the process can mix the two because I am 1000% sure, if something writes to the pipe nothing is written to the console. So I think this wouldnt cause any trouble.

Edit2: Damn this can't be so hard to send text to a process. Linux is so powerful, this should be a damn small task for it :(

pixellany 06-29-2009 11:04 AM

Quote:

Originally Posted by Gachl (Post 3590023)
Edit2: Damn this can't be so hard to send text to a process. Linux is so powerful, this should be a damn small task for it :(

I did not attempt to understand the details, but the advice offered by @david1941 did not seem all that complex....In fact, I must go back and thank him for getting into my thick head what a "named pipe" is.....

Gachl 06-30-2009 02:39 AM

Quote:

Originally Posted by pixellany (Post 3590212)
I did not attempt to understand the details, but the advice offered by @david1941 did not seem all that complex....In fact, I must go back and thank him for getting into my thick head what a "named pipe" is.....

agree. But I don't want to run an additional script if I start the server just to get the pipe work.

Also why isn't program <> pipe writing stdout to the pipe? I still can see all console output and the pipe remains empty.


All times are GMT -5. The time now is 09:03 AM.