1

I have a program reading from standard input in a while loop. I need to run it in the background, even after I close the console. If the name of the program is prog, how can I do that?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
Kbdman
  • 134
  • 1
  • 10
  • If I type the command echo -e "\004" |./prog &,after I close the console,the process end – Kbdman Nov 16 '15 at 07:06
  • Possible duplicate of [How can i put the current running linux process in background](http://stackoverflow.com/questions/13676457/how-can-i-put-the-current-running-linux-process-in-background) – Marco Nov 16 '15 at 07:13
  • 4
    If you close the console, how can it read from stdin? –  Nov 16 '15 at 07:37
  • What is your `stdin`? A tty? And **why** do you ask? Please edit your question to motivate it – Basile Starynkevitch Nov 16 '15 at 07:39
  • For backgrounding a process and having it run detached from the console/terminal it started on: `$ bg %1; disown %1`. (Or whatever PID your program has.) –  Nov 16 '15 at 07:39

3 Answers3

2

You'll have to provide stdin redirected from some source other than the keyboard (which disappears when you log out), but

nohup prog < inputfile > outputfile 2> errorlogfile

should do the trick.

Darwin von Corax
  • 5,201
  • 3
  • 17
  • 28
2

When I started with UNIX 24 years ago I had the same question.

If you are a newbie then what you are looking for is tmux: Here you can start a program that reads from STDIN, log out, log back in some time later, and continue.

Otherwise nohup is the correct answer.

Ole Tange
  • 31,768
  • 5
  • 86
  • 104
0

You might be looking for screen

$ screen
$ prog < inputfile
# CTRL-A, CTRL-D to detach from the screen tty
# Log out or close console; log back in, or start another console later. 
# To re-attach to the screen tty:
$ screen -r
matt2000
  • 1,064
  • 11
  • 17