G
G
gera72021-10-19 09:42:26
linux
gera7, 2021-10-19 09:42:26

How to run an already stopped program in the background without switching to foreground?

I run:
1. ping www.google.com
2. Ctrl+Z stop the process and exit it
The process hangs as Stopped
3. I prescribe bg in order to start the stopped process in the background and not go into the process itself.

And at the 3rd stage, it throws me into the process itself, where I see the execution of the ping command, while Ctrl + Z / Ctrl + C may not work and you need to press 10.
It turns out that bg does the same thing as fg, but it seems to freeze and does not react to stopping or interruption

How to correctly run an already stopped command in the background?
The fact that you can immediately run a command in the background with >/dev/null 2>&1 & I know, but I wonder how it should work without it and whether it should.
Ubuntu Linux Version 20.04.2

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
pfg21, 2021-10-19
@pfg21

kill -18 %pid%
send signal 18 SIGCONT Continue if stopped

X
xandox, 2021-10-19
@xandox

> And at the 3rd stage, it throws me into the process itself, where I see the execution of the ping command, while Ctrl + Z / Ctrl + C may not work
This is the output of the process, the process itself continues to run in the background and you can start another process if you want.
In order not to see the ping output, the easiest thing is to redirect the output somewhere.

S
Saboteur, 2021-10-19
@saboteur_kiev

You need to understand two points
of the jobs command, bg and fg are internal commands of the shell, and work with the child processes of this particular shell.
If you paused a child process (Ctrl-Z) or launched it directly into the background (ping google.com &), then with the fg command you bring it to the foreground. It's like in Windows you took and put the focus on the notepad window with the mouse, after which you will write in this notepad by pressing the keys.
And if you paused the process with Ctrl-Z and resumed it with bg, it will continue in the background with your shell in the foreground - you can write other commands. It's like clicking the mouse in Windows on the desktop next to the notepad, and therefore by pressing the keys you will write not in the notepad but on the desktop (the arrows run over the icons, some hotkeys can work, Enter will launch the current icon)
To understand, practice So:

sleep 100
CTRL-Z
jobs
bg 1
echo I am still in shell
fg 1
CTRL+C

And the second point - you can run the program in the background and disconnect it from the current shell at all (run it through nohup sleep 100 & and disconnect from the session, or execute
sleep 100 &
jobs
disown
jobs
ps aux | grep sleep
in this case bg / fg is already will not help, since the program running in the background already has a different parent, and is not controlled by internal bg / fg - returning it as a child process under the current shell will no longer work.
Therefore, Ctrl + C, Ctrl + Z is just a signal that you send to the current "foreground" process with which stdin is associated

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question