12

When I was in the office using my desktop, I started a script run.sh in background 4 times.

run.sh parameter1 &
run.sh parameter2 &
run.sh parameter3 &
run.sh parameter4 &

Then I came home, and opened a terminal on my laptop, ssh to the desktop in my office. When ran ps -u jack, I saw the started processes:

 3562 pts/1    00:00:00 bash
 4540 pts/2    00:00:00 bash
 4643 pts/3    00:00:00 bash
 4748 pts/4    00:00:00 bash

But when I typed jobs, I got nothing. Why I could see these four running background processes?

Btw, how can I bring them back to my current terminal, so that I can kill one of them, say run.sh parameter4 &?

4
  • Where did you started the script, in a remote machine?
    – fedorqui
    Commented Jun 13, 2013 at 12:17
  • @fedorqui No the script was started on my desktop directly.
    – JackWM
    Commented Jun 13, 2013 at 12:18
  • Did you run jobs command in the same shell?
    – Grzegorz
    Commented Jun 13, 2013 at 12:20
  • @Grzegorz No, I opened a new one from my laptop.
    – JackWM
    Commented Jun 13, 2013 at 12:22

2 Answers 2

10

Based on the problem statement of the question, IMHO I do not see any reason for using background or foreground. All you care is to find a process which is running in background so that you can kill it.

Run ps -ef | grep parameter3 to find processes which has parameter3 in the process name. You can adapt the grep to uniquely identify a process, given you don't have two processes with exactly same process name.

Once you have it, just do kill -9 PID and that process will be killed. So no need for bringing that process to the foreground for killing it.

Hope this helps.

3
  • I saw this jack 15766 3562 0 Jun12 pts/1 00:00:00 /bin/bash ./run.sh 015d4a826e43f416 which number is the pid?
    – JackWM
    Commented Jun 13, 2013 at 12:44
  • @JackVW the second column is the PID. So you will do kill -9 15766. If you care, the third column is PPID (PID of the parent process).
    – Bill
    Commented Jun 13, 2013 at 12:45
  • 2
    Why kill -9? It's almost always better to start by using kill without the signal parameter first. That way, the killed process can tidy up before terminating if it has been programmed that way. Then if the process still refuses to die, you can use kill -9. Commented Jun 16, 2013 at 11:06
22

This is because jobs shows background commands started from (belonging to) the same shell. The shell processes running under your desktop terminal and under ssh terminal are different.

See http://www.gnu.org/software/bash/manual/html_node/Job-Control-Basics.html

To be able to control your processes as jobs from different terminals you can use screen or tmux which are basically virtual terminal managers and can re-connect them to any number of other terminals.

For example, with screen you just start it, it opens your default shell and you work with it as you would with any other shell. Then when you get home and ssh to your office machine, you can run screen -d -r to detach the virtual terminal from your office desktop terminal and attach it to your ssh terminal, resuming the work. You can detach the virtual terminal from within screen itself by pressing Ctrl-A, d.

12
  • Thanks for the info, then how can I bring them back to my current terminal?
    – JackWM
    Commented Jun 13, 2013 at 12:23
  • 2
    If you wish to run things "in the background", so to speak, and resume them later then something like GNU Screen or tmux would be useful. This will allow your to resume from any subsequent shell sessions whereas, as described above, you can only bring background jobs to the foreground from the same shell. Commented Jun 13, 2013 at 12:24
  • @JackWM use fg
    – Bill
    Commented Jun 13, 2013 at 12:24
  • @Bill Does fg require a background process number, which I don't have?
    – JackWM
    Commented Jun 13, 2013 at 12:26
  • 2
    @JackWM you can distinguish them using parameter3. run ps -ef | grep parameter3 and this will give you the pid of a process which contains parameter3. You can adapt this to your need. I tend to keep things as simple as possible. If all you need is to identify a pid for killing it, I really do not see any need for bg and fg, making life complicated :)
    – Bill
    Commented Jun 13, 2013 at 12:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .