0

I have wrote a very simple shell script to practice command "bg" , "fg" and "jobs":

#!/bin/bash

myvar=1
while [ $myvar -le 100000000 ]
do
    echo $myvar
    myvar=$(( $myvar + 1 ))
done

I use SecureCRT to connect a ubuntu machine.I have cloned several tab which is connected to this machne.

When I get the process pid and use kill -19 pid to suspend it , and I run jobs to see if it has become a background job, yes it is:

imcas@ubuntu:/home/wuchang$ jobs
[1]+  Stopped                 bash testShell

but what is strange is ,when I run command jobs in another tab of SecureCRT,I cannot see the stopped job:

imcas@ubuntu:/$ jobs
imcas@ubuntu:/$ jobs
imcas@ubuntu:/$ 

Anyone can explain this for me?

  • 3
    jobs are subprocess of you bash session. jobs syntax is only a way to wrap process. from other bash session you must use ps command. try ps -fu imcas (assuming icmas is you user) – Archemar Dec 10 '14 at 7:31
  • So , if I am in another session , and I use ps to get the pid,how can I resume the stopped job? – wuchang Dec 10 '14 at 7:49
  • 2
    as per man bash, try kill -SIGCONT – Archemar Dec 10 '14 at 8:48
1

Jobs are a shell concept. A job is a subprocess¹ of a shell that the shell tracks. A shell instance running on one terminal doesn't know anything about the jobs of another shell instance. When you run jobs in the shell running in the second tab of your terminal emulator, this lists the jobs in that shell. The shell running in the second tab doesn't know anything about the jobs running in the first tab.

Jobs are processes, so you can manipulate them as such. You'll need to identify the job's process ID. Process IDs are a kernel concept; a process ID uniquely identifies a process on a given machine at a given time. You can run ps -t pts/42 to list the processes running on the terminal /dev/pts/42; run the command tty in a shell to see on which terminal it's running. (That's terminal, not terminal emulator software. Tabs in a multi-tab terminal emulator are unrelated terminals.)

Once you've identified the process ID of the process you're interested in, you can send it a signal with the kill command. The signal SIGCONT tells a process to resume executing, if it was suspended (if it wasn't suspended, the signal does nothing). The process will execute in the background; if you want to bring it to the foreground, you need to do that with the fg command in the shell that's running on that terminal (because you need to get the shell to forego being in the foreground).

¹ You can nitpick about jobs being process groups but that's irrelevant here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.