3

When I start the program in the terminal I see the output. I can Ctrl+C my way out of it to use the terminal again, but is there a way to get the output of it into the terminal again?

I am aware that I can direct the output into a file and read that but it would be nice to have it back on the screen again for testing.

  • 3
    i am not clear..you want the output after you close the program? – heemayl Sep 16 '15 at 13:55
1

Hitting Ctrl+C sends a SIGINT signal to the process running in the terminal's foreground, terminating it unless the process is deliberately ignoring SIGINT signals;

So by hitting Ctrl+C you're telling the process running in the terminal's foreground to terminate.

To run a process preventing it to occupy the terminal you can start it into the background by appending & at the end of the command:

user@user-X550CL:~/tmp$ bash script.sh &
[1] 24961
user@user-X550CL:~/tmp$ 

If you started a process in the foreground already, you can still stop it by hitting Ctrl+Z:

user@user-X550CL:~/tmp$ bash script.sh
^Z
[1]+  Stopped                 bash script.sh
user@user-X550CL:~/tmp$ 

And send it into the background and resume its execution by using the bg built-in:

user@user-X550CL:~/tmp$ bg
[1]+ bash script.sh &
user@user-X550CL:~/tmp$ 

You can send multiple processes into the background:

user@user-X550CL:~/tmp$ bash script.sh &
[1] 24961
user@user-X550CL:~/tmp$ bash script.sh &
[2] 24984
user@user-X550CL:~/tmp$ bash script.sh &
[3] 24989
user@user-X550CL:~/tmp$ 

You can list all the processes into the background by using the jobs built-in:

user@user-X550CL:~/tmp$ jobs
[1]   Running                 bash script.sh &
[2]-  Running                 bash script.sh &
[3]+  Running                 bash script.sh &
user@user-X550CL:~/tmp$ 

To move one process from the background to the foreground, you can use the fg built-in passing the job number as an argument:

user@user-X550CL:~/tmp$ fg %1
bash script.sh

Running fg without specifying a job number as an argument will have the effect of moving to the foreground the last backgrounded process:

user@user-X550CL:~/tmp$ fg
bash script.sh
  • thank you, i was unawear that ctrl-c actually killed the process (this explains a lot!) the only time i used ctrl-c was to get back to the terminal and pkill that process! What you say makes sense, and to confirm, when i fg a particular process it will return to the foreground an i will be able to see its output again? – ThriceGood Sep 16 '15 at 15:01
  • @ThriceGood Yes, check the last example. ^C means that I hit Ctrl+C, but the process was actually running in the foreground. Try running e.g. watch -n 1 echo foo & and then running fg, you'll see that the process, brought to the foreground, will start outputting to the terminal again. You can then hit Ctrl+Z / run fg to toggle it from running in the background to running in the foregroung – kos Sep 16 '15 at 15:03
  • excellent so far so good, when i ctrl-z a foreground process i get back to the terminal. when i run jobs it tells me that the process is stopped, what does this mean? that the process itself is stopped or its just stopped being in the foreground? because the process listens on a port, even when its saying stopped netstat tells me that the process is still listening, it could be just saying that the port is bound but the process itself is stopped. can you clarify this for me? – ThriceGood Sep 16 '15 at 15:25
  • 1
    @kos Just out of curiosity, what do the + and - symbols after the job number in square brackets mean in e.g. the jobs output? – Byte Commander Sep 19 '15 at 19:35
  • 1
    @ByteCommander They just indicate the last and the second-to-last backgrounded process, I think it's done to give an idea of which processes would pop out of the stack first when issuing fg / which processes would resume the execution first when issuing bg – kos Sep 19 '15 at 23:01
2

Start your application or command via

command &

and don't use Ctrl-C. You will see the output until you close the terminal.

Bring the command back into foreground with

fg

Example

Create a simple script, e.g.

#!/bin/bash
while true; do
    echo foo
    sleep 5
done

Start the script, the program will output the word foo. After some time I entered echo bar, followed by fg and Ctrl-C to terminate the script.

$ ./foo &
[1] 29544
$ foo
foo
foo
echo bar
bar
$ foo
foo
fg
[1]  + 29544 running    ./foo
foo
^C

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.