0

I have this process:

$ bash start.sh

I run this command:

$ pidof bash

And receive this:

9427

I want to run commands in the script stdin(the script has its own commands) I want to run command "give 1 shotgun to player"
I tried this:

$ give 1 shotgun to player > /proc/9427/fd/0

But I got this:

-bash: give: command not found

After, I tried this:

$ echo "give 1 shotgun to player" > /proc/9427/fd/0

But the message "give 1 shotgun to player" was written in the process, but script's command wasn't activated!

2

You cannot intercept the already existing stdin of a process, at least not the way you are trying. See Writing to stdin of a process for details.

There are two basic options to control a process from another using stdin but they must be specified when launching the process.

Input from a file (script)

If you can prepare the whole input before launching the process to be controlled, create a file and use it as the stdin of the controlled process. Example:

$ cat commands
give 1 shotgun to player
shoot

$ ./start.sh < commands

Input from a process

If you use another program to issue commands that control your process, connect them both using the pipe symbol (|). The following example shows how to simply control when to issue the commands.

$ cat controller.sh
#!/bin/sh
sleep 5
echo "give 1 shotgun to player"
sleep 2
echo "shoot"

$ ./controller.sh | ./start.sh

This way you can also use echo as you tried to simulate the script option without using any script file:

$ echo "give 1 shotgun to player\nshoot" | ./start.sh
  • The process is already started! – Bogdan Jan 28 at 12:48
  • this starts again start.sh script – Bogdan Jan 28 at 12:52
  • Yes, I’ve presented commands that launch a process with the specified input. I don’t know how to intercept stdin of a process, you have to wait for another answerer. – Melebius Jan 28 at 12:59
2

The Q&A Melebius linked to actually provides a solution that let's you write to stdin of another process:

Using mkfifo you can create a named pipe. You can then redirect stdin of your process to the named pipe and write to it from another process:

$ mkfifo myfifo.pipe
$ ./start.sh < myfifo.pipe

... and from another terminal:

$ echo "give 1 shotgun to player" > myfifo.pipe
  • 1
    @Melebius no, you have to redirect stdin when starting the program. But you don't have to specify what is being written to it in advance. I guess the OP is starting a game(server). He can then "give 1 shotgun to player" whenever he wants. – danzel Jan 29 at 12:35
  • Yes you right, but game server won't start! – Bogdan Jan 30 at 8:38
  • @Bogdan does it start when you run ./start.sh? Could you provide a link to the script? – danzel Jan 30 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.