Shell Script – Sending Input to Background Process

background-processshellshell-script

I am using the following script to watch a folder, and if the size changes and a file doesn't exist, it runs a script.

#!/usr/bin/sh
SCRIPT="dim_import/xdir"

while true;
do
    size=$(du -s dim_import | cut -b 1-2)
    if [ $size -gt 32 ]; then
        if [ ! -f files ]; then
          $SCRIPT
        fi
    fi
    sleep 2
done

I run the script like so

>watcher &

The problem is that the script that gets run within the watcher program requires input. It echos out the prompt, but then the input is not sent to that script, but the the current shell. Thus if I input 1234 I get this:

sh: 1234:  not found.

How can I make sure to send the input the correct place?

I have tried many different methods and none seem to work because of the unique way I am doing this. I am also open to better methods/work around. The script also has the ability to handle the input I want to send as an argument.

EDIT: I am trying to send a number like 62918

EDIT: I could also do something like this, but I get the same error:

    SCRIPT="dim_import/xdir"

    while true;
    do
        size=$(du -s dim_import | cut -b 1-2)
        if [ $size -gt 32 ]; then
            if [ ! -f files ]; then
              echo "SR Number: "; read srNum
              $SCRIPT $srNum
            fi
        fi
        sleep 2
    done

Note: I am not using Bash. I am using Sh.

Best Answer

Well, the real question is what input you actually want to send to your SCRIPT. If you want it to have empty input, then run:

$SCRIPT < /dev/null

If you want to give it some particular input, like the contents of $size, then run:

echo "$size" | $SCRIPT

If you want to type the input, then you will have to put it back in the forground to type your input, then put it into the background again. The ability to do that depends on whether your shell has job control (most do, but the original Bourne shell did not). Do do that:

  • Run fg %watcher or fg %1 (using whatever job number you see after running jobs -l) to put the process into the foreground.
  • Type the input you want followed by return.
  • Type control-Z to put it back into the background (or use stty -a and check for susp to see what your stop character is).
  • After the program is stopped by control-Z, put it back in the background with bg %1 (or whatever the number was).

Yet another answer would be to have a named pipe. Here is an example (using tr in place of your script just to show):

$ mkfifo my_fifo
$ tr a-z A-Z < my_fifo &
$ exec 6> my_fifo
$ echo here is some input for the fifo >&6
$ HERE IS SOME INPUT FOR THE FIFO

First you run the script taking it's input from the fifo (important to run the script first). Then exec 6> ... makes file descriptor 6 of the shell write to the fifo. Then, whenever you want to send output, write it to file descriptor 6 (via >&6). Notice the final "HERE IS..." was not typed by me, it just was the output of tr.

Related Question