9

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?

HARUN SASMAZ
  • 567
  • 1
  • 7
  • 17

3 Answers3

15

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

lainatnavi
  • 1,453
  • 1
  • 14
  • 22
4

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 !

Freeman
  • 9,464
  • 7
  • 35
  • 58
  • Those are not all portable examples. I just tried the first two on macOS Catalina and they did not work! They do work on Debian Linux though. – clearlight Mar 01 '20 at 14:39
  • thnx for your reply macOS is unix and his or her question is about linux Ubuntu! – Freeman Mar 01 '20 at 14:40
  • Fair enough. Just pointing out the non-bash specific items. Wasn't aware of pidof, so wanted to try it. – clearlight Mar 01 '20 at 14:41
  • 1
    So, in this case 6331 is the pid of process? I am not sure fg 6331 works – HARUN SASMAZ Mar 01 '20 at 14:47
  • I already have PID of a process, what I need is to make a paused job come back to work in background – HARUN SASMAZ Mar 01 '20 at 14:48
  • I updated the scenario of my question, you have a good point but the thing I am looking for is quite different – HARUN SASMAZ Mar 01 '20 at 14:52
  • @HARUNSASMAZ or better than all that, use `pgrep`. But shells do not look up their children by PID: since it's the shell who exec'ed them, it just saves the arguments it had passed to `execve`. –  Mar 01 '20 at 17:47
  • This answer contains other innacuracies; for instance, Ctrl-Z (`VSUSP`) sends a `TSTP`, not a `STOP` signal, and it sends it the the whole foreground _process group_ (job), not just to a single process. –  Mar 01 '20 at 17:54
2

I hope you find a way from now but in case and for other here is a workaround to get job's id from pid (and then use fg, bg command):

myJobId=$(jobs -l | grep $myPID | cut -c2)

$myPID contain the pid of the process

jobs -l gives the list of all jobs with their pid. Then grep $myPID gives the corresponding line from that list. Then cut -c2 gives the second character of that line which is the id of the job. this last command should be replace by a regex or something like that to get any digit between the first [] of the string (but it work fine for me as i have only 2 bg process)

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • Surround the pid with spaces and quotes so not to match another process then this solution is fine: `myJobId=$(jobs -l | grep " $myPID " | cut -c2)` – Julien Carsique Feb 11 '21 at 08:52