0

If I have a command line program executed as a backgrounded thread (AKA with ./program &) that interacts with the command line, how can I send commands to it? Is there a way (say, using 'pipe') to send characters as though they were being inputted on the command line?

Simplified command-line example:

Without & (normal case):

~/home/temp> ./testprogram

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> Welcome to Testprogram. Enter command: quit

~/home/temp> Goodbye!

With & (my case!):

~/home/temp> ./testprogram &

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> quit

~/home/temp> Command not found.

A ps -e | grep testprogram shows that it's still running, but now can never terminate outside of a 'kill' command.

What would be ideal is something like the following:

~/home/temp> ./testprogram &

~/home/temp> Welcome to Testprogram. Enter command:

~/home/temp> quit

~/home/temp> Command not found.

~/home/temp> quit | /cat/proc/testprogram

~/home/temp> Goodbye!

How exactly do I do this?

tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

0

You can bring the program back using fg:

~/home/temp> ./testprogram &
Welcome to Testprogram. Enter command:
~/home/temp> ls /tmp  # do whatever else you want to do on the shell
# ... later
~/home/temp> jobs     # what was running?
[1]+  Running                 ./testprogram &
~/home/temp> fg       # Brings testprogram to foreground, does not re-prompt
quit
Goodbye!
~/home/temp>

If you have more than one backgrounded jobs, you can choose which to foreground using "fg %1" "fg %2"...

Note: you can put a foregrounded job back into the background by stopping (Ctrl-z) the job then typing bg at the shell prompt.

Dean Serenevy
  • 1,284
  • 12
  • 13