Provide arguments (via bash script) for a process that is already running

I am trying to have a bash script that does the following (in pseudocode):

#!/bin/bash run myapp (which needs arguments given from stdin) /* do some extra stuff */ provide arguments to hanging process myapp 

For example, let's say you started myapp, and after starting it asks for your name. Ie, I run it through bash, but I don’t want to give it a name yet. I just want it to be executed for now, but for now bash is doing some other things, and then I want to provide my name (still through bash). How can I do it?

+4
source share
2 answers

You can use an anonymous channel:

 # open a new file descriptor (3) and provide as stdin to myapp exec 3> >(run myapp) # do some stuff .... # write arguments to the pipe echo "arg1 arg2 -arg3 ..." >&3 

The advantage over a named pipe is that you don’t have to worry about cleaning up and you don’t need write permissions.

+5
source

You can use named pipe :

 # create the named pipe mkfifo fifo # provide the named pipe as stdin to myapp ./myapp < fifo # do something else # ... # write the arguments to the named pipe ./write_args_in_some_way > fifo # remove the named pipe rm fifo 

You can also use an anonymous feed as indicated by @ hek2mgl's answer, which is probably better in this case. However, there are several advantages (which may not apply in this case) of named pipes against anonymous pipes, as described in this Stackexchange question .

+3
source

Source: https://habr.com/ru/post/1491714/


All Articles