Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

I'm 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, say you run myapp, and after it runs it asks for your name. I.e., I run it via bash, but I don't want to give it a name just yet. I just want it to run for now, while in the meantime bash does some other stuff, and then I want to provide my name (still via bash). How do I do this?

like image 414
shblsh Avatar asked Feb 16 '23 22:02

shblsh


2 Answers

You can use an anonymous pipe:

# 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 the fact that you don't need to worry about cleaning up and you won't need any write permissions.

like image 68
hek2mgl Avatar answered Feb 18 '23 12:02

hek2mgl


You can used a 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 used an anonymous pipe, as indicated in the answer by @hek2mgl, which is probably better in this case. There are, however, a few advantages (which may not apply in this case) of named pipes vs anonymous pipes, as explained in this Stackexchange question.

like image 30
cabad Avatar answered Feb 18 '23 12:02

cabad