Level099: 2: Bash jobs: bg while suppressing output?

So, I thought I’d edit a file while docker was going up on a bootstrapped ssh connection (so forgot to use a multiplexer):

podman-compose up -d
Ctrl-z
bg
vim…
# screen filled with progress bar dump

Searching around, you can do that before executing by subshelling it: (ping 1.1 &)


How do you background a job, without listening to stdout (and stderr)?

1 Like

If you aren’t using Ctrl+Z, try & > /dev/null so something like ping level1techs.com > /dev/null &

If you’re using Ctrl + Z, it gets way more complicated. I think there might be a way to intercept the stdout with gdb (GDB: The GNU Project Debugger) and redirect that to /dev/null but it’s probably not worth the effort.

EDIT: bash - Redirect stdout/stderr of a background job from console to a log file? - Super User

gdb -p <PID> and then:

p dup2(open("/path/to/file",577, 420), 1)
p dup2(1, 2)
detach
quit

Now I’m going to do the classic Linux “You’re doing it wrong and here’s how to avoid the problem, but not solve it”: If it’s a big job I am always running it in nohup anyways. So nohup ping level1techs.com & will get it and run it silently in the background, redirecting stdout to nohup.out. And if you close the terminal, it’ll stay alive.

It’s too bad nohup bg %n or bg %n > /dev/null doesn’t work.

1 Like

Attaching a debugger to a process as a general rule slows the process down. Haven’t tested or thought about this, if the gdp -p actually slows stuff down.

I’m pretty much always running a multiplexer (byobu¹) anyway. Somewhat retiring the need for jobs (besides suspending).

¹ I’ve found it to be more explorable and user-friendly than tmux, it has everything I’ve ever needed, and more. See the cheat sheet.

Level099: 3 should probably be about multiplexers…?


If I know I don’t want output before, I’ll redirect to somewhere, or again, use a multiplexer.

The thing is, there are moments, where I thought I’m going to be in for a moment, and not bother running/enabling a multiplexer.
The idea of ‘well now I’m waiting for computers for no reason, while I could do something else on the same device’ only comes up after ~5-10s of running the command.

So – it’s just not a use case thought of, and not reasonably possible.

Try :
nohup command
ctrl-z
bg

Used like this, nohup will ignore input and put output into a file called nohup.out

-1

You can nohup, redirect to /dev/null, etc.
Problem being, you have to redirect output before executing. You can’t, afaik, change it in the middle of execution.

If you want to ignore the stdin and stderr then Just start a new terminal, its handy to have a key-binding for this. If there is some issue with a new terminal then install something like gnu-screen and use it to manage virtual terminals.

If the OP’s problem is only the fact that the text displayed is the console output instead of the file content in vi, then a ctrl-g will clear it and display the file content…

1 Like