Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using bg and fg with a given PID

I am using Ubuntu.

If I am given a job’s PID, how can I bring the paused job to the background/foreground and running state?

I know bg/fg but they require job id not PID.

Also, I pause jobs with

kill -STOP <PID>

and I know that I can resume it with

kill -CONT <PID>

but I do not know how I can use bg and fg commands with this PID.

Edit: I want to make this clear:

Scenario: I have a paused job that I know its PID. How can I bring it back and make it work background?

like image 472
HARUN SASMAZ Avatar asked Dec 10 '22 00:12

HARUN SASMAZ


2 Answers

You cannot use fg and bg with a pid. They are shell builtin-s which require a jobspec, not a pid.

like image 153
lainatnavi Avatar answered Dec 29 '22 05:12

lainatnavi


you can use top or ps command to see all of your PID and also you can use pidof command to gain PID

for example I wanna check the PID of bash command so I will write :

pidof bash

or with ps command you can write

ps -C bash 

or with ps and grep you can write :

ps aux | grep bash

or just write top and check what ever you need.

for sending a command to bg or background you have to add & at the end of your command for example I wanna send xeyes to bg so I will write :

xeyes &

if you wanna bring it to foreground or fg you will write :

fg xeyes

you can also see all of your jobs with jobs -l command.

for example I want to run xeyes command so I will write :

xeyes 

please attention to eyes when you moving your mouse, now if I want send it to bg I can use CTRL+Z on my keyboard. if you want to check it you can write jobs you will see xeyes is stopped !

left of each process is a number like this :

[1]-  6331 Running                 xeyes &
[2]+  Stopped                 xeyes

for example for me is 2 you can start your PID again like this :

bg %2

or you can find PID with -l in jobs to see PIDs too

[1]-  6331 Running                 xeyes &
[2]+  6332 Stopped                 xeyes

instead of bg %2 I can write bg 6332 to change state to Running !

and also you see + or - in this case, if the + sign was next to that process is stoped you can just write bg without any this like this :

bg # attention to + 

if you want to bring your command to fg is exactly like bg, which means you can use one of that ways that I already mentioned !

I think about your question, it means the process has received a STOP signal, and won't do anything much until it receives a CONT signal, not even terminate.

Actually the most common source of STOP signals is the user hitting CTRL+Z while the process is in the foreground, and the common way to send a CONT afterwards is typing fg or bg which continue the process in the foreground and background respectively, another way to send STOP to a process is kill -STOP PID. Similarly, CONT can be sent to a process with kill -CONT PID. Since you sent TERM signals to the processes, I assume you want them to terminate. For that to happen, the processes must receive CONT signals. You can send those by typing kill -CONT 6331 6332 in a terminal window. please aware that these PIDs is belong to me and you have to change it to yours !

like image 28
Freeman Avatar answered Dec 29 '22 06:12

Freeman