Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a job dispatcher bash shell script containing below codes:

for (( i=0; i<$toBeDoneNum; i=i+1 ))
do
    while true
    do
            processNum=`ps aux | grep Checking | wc -l`
            if [ $processNum -lt $maxProcessNum ]; then
                break
            fi
            echo "Too many processes: Max process is $maxProcessNum."
            sleep $sleepSec
    done
    java -classpath ".:./conf:./lib/*" odx.comm.cwv.main.Checking $i
done

I run the script like this to be in the background:

./dispatcher.sh &

I want to terminate this dispatcher process with kill -9. But I didn't record the pid of the dispatcher process at the first time. I used jobs, jobs -l, jobs -r and jobs -s. Nothing showed. Even this fg cannot bring the process to foreground.

fg
bash: fg: current: no such job

But I think this dispatcher process is still running because it still continues to assign java program to run (already used top and ps -ef to check). How should I terminate this job dispatcher bash shell script process?

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You should be able to just kill the script by name using the pkill command.

$ pkill -9 dispatcher.sh

excerpt from man page

pgrep, pkill - look up or signal processes based on name and other 
               attributes

OPTIONS
       -signal
       --signal signal
              Defines the signal to send to each matched process.  Either 
              the numeric or the symbolic signal name can be used.  (pkill 
              only.)

See the man page for pkill for more info.

Finding processes

If you find that you no longer know a process's process ID (PID) you can find it a couple of ways.

pgrep

You can use pgrep to find a process by name.

$ pgrep dispatcher.sh
12345

You can then perform a kill -8 12345.

ps

Most people learned to find PID's using ps. You can look for your process in the output like this.

$ ps -eaf | grep [d]ispatcher.sh
saml      2735     1  0 Jan11 ?        00:02:50 dispatcher.sh

The PID is the 2nd column in the output (typically). The above trick where I wrap the first letter of the process I'm looking for eliminates the grep from showing up in the results. Try it without the square brackets to see what I mean.

share|improve this answer
    
The thing I cannot realize is that even jobs, pgrep and ps cannot find the dispatcher process. But new jobs are still being assigned. Really strange. –  Marcus Thornton Jan 13 at 4:03
    
Looks like you made yourself a little form bomb type of script. Look for while and for processes. –  slm Jan 13 at 4:15
    
What do you mean by form bomb? Does my syntax go wrong? –  Marcus Thornton Jan 13 at 5:52
    
Sorry that should've said fork bomb. unix.stackexchange.com/questions/89003/… –  slm Jan 13 at 5:55
    
@MarcusThornton Given the code you've posted, I can't think of anything to add to slm's answer. If you need more help, post the whole script. Also post ps l lines for some of these Java processes, look at the PPID line and see what their parent is. –  Gilles Jan 13 at 21:55

If you can see the Java processes spawned by the dispatcher in the output of the ps, try to check the PPID of these processes. Check if it is a bash process, and if so, try killing it.

share|improve this answer

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.