-1

Context


I have checked several questions about the same topic and I still get confused when they explain what a background process really is. The common answer I interpert is that:

a process that runs in the background allows you to interact with the same terminal... etc.

I have tested this by running the following command:

  • ping google.com -c 100 &
  • P.S: I also ran this same command without the & operator and used Ctrl-Z, but all that did was suspend the command

Question


When I ran this command, the output was still in my main terminal, and I could not execute any other commands until the command was finished running, or until I killed the command.

Before I list my questions, my questions is not how to redirect the output of a command to a seperate file / place. I am already aware of how to redirect the output of ping google.com -c 100 command to a different place so I can still interact with the main terminal

My questions are

  • In the most simplest terms possible, what does backgrounding a command actually do
  • What does putting a & at the end of a command do
  • What is the purpose of doing this / what do I gain out of backgrounding a command
  • When would I want to background a command

** Please let me know if you need further clarification about the quesitons or the context

Can1ne
  • 1
  • 6
    If you have this level of experience you really should not be trying to learn using Kali Linux. It's a specialist distribution for security professionals who are familiar not only with their trade but also with Linux in general. I would strongly recommend you start with a more friendly distribution such as Mint or Ubuntu – Chris Davies Aug 12 '21 at 20:33
  • 1
    I appreciate your input but with all due respect, if you do not wish to answer my question, please refrain from commenting. It is simply a waste of both of our time. I am aware that Kali Linux is an advanced debian-based distro of Linux, and I am aware it will be hard to learn. If I struggle, I struggle. Please answer my question or move on. Have a Great Evening – Can1ne Aug 12 '21 at 20:59
  • 5
    This is more a question about shell job / process management in general than Kali Linux specifically. That it's Kali is irrelevant. – DopeGhoti Aug 12 '21 at 21:01
  • You totally can run other commands while ping is running in the background, it's just really confusing because ping's output gets mixed in with everything else -- the shell prompt, what you type in as a command, what that command outputs, etc. So backgrounding is most useful (/least confusing) for things that don't constantly spit output the way ping does. – Gordon Davisson Aug 12 '21 at 23:27

1 Answers1

2

Answering these in the context of an interactive shell. The behaviors are subtly different but largely similar within the context of a shell script.

  • In the most simplest terms possible, what does backgrounding a command actually do

Backgrounding a command (or other process) allows it to execute while still allowing you to interact with the shell (i. e. start new processes).

  • What does putting a & at the end of a command do

The & delimiter signifies the end of a given command and immediately drops the process invoked by that command to background execution. It is analogous so the ; delimiter which signifies the end of a given command to be executed until it terminates, whereupon a new shell prompt is provided.

  • What is the purpose of doing this / what do I gain out of backgrounding a command
  • When would I want to background a command

These are essentially the same question. The short answer is: When you are about to execute a command which will take a long time to run, but you can continue to do other things while executing it. One example would be, for a contrived for-instance:

$ tail -f /path/to/apache-log > ~/webserverlog.txt &
$ curl --silent -o /dev/null https://localhost/path/site/you/are/debugging
$ kill %1
DopeGhoti
  • 76,081
  • Thank your for clarifying this. – Can1ne Aug 12 '21 at 22:23
  • 1
    I would add: for simple cases, to background a non-background process after the fact, CTRL-Z followed by the bg builtin command (meaning background) often does the job. – A.B Aug 13 '21 at 07:48