0

when i run

$ ls -R / & 

although the & (which suppose to relegate the process into the background) i end up having to sit through the entire listing of my system. pressing CTRL+C or CTRL+Z doesn't help. what am i doing wrong?

muru
  • 72,889
  • 1
    "what am i doing wrong?" What are you intending to do? – muru May 30 '19 at 00:41
  • just see what happens when i send a process into the background for practice/learning reasons @muru – Daniel Eliazarov May 30 '19 at 00:42
  • Then maybe pick commands that don't have lots of output. You can't do anything about the output once the process has started. If you want to stop the process, see – muru May 30 '19 at 00:44
  • 2
    It sounds like you saw what happens, then - what is the question you want answered at this point? – Michael Homer May 30 '19 at 00:45
  • @muru so the outputs come in even if the job is in the background? is that stdout and i could send it to the /dev/null? – Daniel Eliazarov May 30 '19 at 00:46
  • 2
    @DanielEliazarov yes. If you want to discard the output, see https://unix.stackexchange.com/a/109986/70524 – muru May 30 '19 at 00:50
  • @muru and why does it show me exit 1 when i do $ ls -R / 1>/dev/null 2>&1 & when i go chck the status in jobs -l? – Daniel Eliazarov May 30 '19 at 00:54
  • 1
    Maybe I don't understand what you're after, but you can still a rogue background job that fills your screen with garbage this way: Ctrl-S, blindly enter kill %, Ctrl-M, Ctrl-Q (or Ctrl-S, fg, Ctrl-M, Ctrl-Q, Ctrl-C). –  May 30 '19 at 01:16

1 Answers1

1

When you start ls -R / in the background, the command is immediately started and control is given back to the invoking shell (your interactive shell). However, the standard output stream and the standard error stream of ls are still connected to the terminal, so that's why you see the output of the command in the terminal.

In comments you ask a follow-up question that shows that you know that you may redirect the output of the command to /dev/null to avoid having it stream to the terminal:

ls -R / >/dev/null 2>&1 &

The follow-up question is "Why does it show me Exit 1 when I check the job's status with jobs -l.

You get a non-zero exit status from ls because it did not run completely successfully. It did not, because you tried to do a recursive listing of all files on the whole system, and, presumably, some of the directories on your systems are not readable by the user that you used to run the command as. If you had not redirected the standard error stream to /dev/null (by removing 2>&1 from the command line), you would probably have seen errors in the terminal.

Kusalananda
  • 333,661