# How do I make a shell script that sends out result to a procedure

I'm presently running a web server console program in a screen due to the fact that I require to both read it and also periodically send commands.

I would certainly such as to run the application as a daemon behind-the-scenes (start/stop it with init).

I can tail -f the log, yet that will not allow me send input to the procedure.

Exists any kind of means to set this up to make sure that I can both read and also send input, yet still have it running in the history?

I would certainly additionally such as to be able to send input to the daemon from various procedures too (a shell script that can send a "Stop \ n" command, as an example).

0
2019-05-13 05:41:20
Source Share

### Read from a pipeline, contact a documents

If you desire the daemon to read input generated by some approximate procedure, you require to connect that procedure to a pipeline. Below the approximate procedure is you resembling commands, and also it is mosting likely to run in a various context. So create a named pipe (usually called a fifo in unix contexts).

mkfifo /var/run/daemon.fifo
</var/run/daemon.fifo /path/to/daemond --option >daemon.log


And simply write commands to the pipeline:

echo 'FORWARD 10' >/var/run/daemon.fifo
echo 'LEFT 72' >/var/run/daemon.fifo


This is not likely to function as is nonetheless : there is a great chance that the daemon will certainly exit when it sees an end of documents on its typical input, which takes place as quickly as the first procedure that contacts the pipeline ends. You can make use of tail -f to stay clear of that trouble.

</var/run/daemon.fifo tail -c +1 -f | {
echo  >/var/run/daemon.pid
exec /path/to/daemond --option >daemon.log
}


With some tail executions, you might get attacked by buffering : the tail procedure will certainly wait till it has actually collected sufficient bytes to send out some result. I do not assume this is understandable in the POSIX tool kit ; if that is a trouble, make use of an unimportant C or Perl or Python program. Regarding I can inform the tail from GNU coreutils (as located on Linux and also in other places) is secure on this regard.

When you stop the daemon, echo >/var/run/daemon.fifo will certainly eliminate the tail procedure.

### Beginning the program inside screen

Instead of conjuring up the daemon straight from your solution supervisor (are you actually making use of simply SysV init, or something added like wrapper manuscripts or Upstart?), invoke

screen -c daemon.screenrc -L -d -m -S daemon_name /path/to/daemond --option


Since the daemon will not be a youngster procedure of the solution supervisor, you require to see to it to send a signal to the appropriate procedure. Just how to do that relies on specifically just how the daemon is begun and also by what.

It is technically possible to attach a running procedure to a terminal, yet there is a threat you'll crash the program, so this is most definitely out for a manufacturing system.

The -L alternative makes screen write every little thing that shows up in its window to a documents. The documents name is given up daemon.screenrc with the logfile instruction.

0
2019-05-16 23:21:53
Source