Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 &?

share|improve this question

migrated from stackoverflow.com Jun 13 '13 at 15:08

This question came from our site for professional and enthusiast programmers.

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

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
I saw this jack 15766 3562 0 Jun12 pts/1 00:00:00 /bin/bash ./run.sh 015d4a826e43f416 which number is the pid? –  JackWM Jun 13 '13 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 Jun 13 '13 at 12:45
    
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. –  Adrian Pronk Jun 16 '13 at 11:06
add comment

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.

share|improve this answer
    
Thanks for the info, then how can I bring them back to my current terminal? –  JackWM Jun 13 '13 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. –  Fred Clausen Jun 13 '13 at 12:24
    
@JackWM use fg –  Bill Jun 13 '13 at 12:24
    
@Bill Does fg require a background process number, which I don't have? –  JackWM Jun 13 '13 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 Jun 13 '13 at 12:37
show 7 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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