1844873 Members
2544 Online
110233 Solutions
New Discussion

Shell Script

 
Pravin B
Occasional Contributor

Shell Script

Hi

I am running java class("abcd.class") five times using vbj.exe and I wanted to write a shell script, which will kill one of those program.

vbj abcd

Since I am using vbj, ps -ef command will not show the class name. Only possibility of getting pid is by 'vbj abcd &' command. This command returns pid as.
[1] 12345
where 12345 is a PID.

PROMPT>vbj abcd & > log
[1] 12345

This will not redirect pid into the log file.

Please help me out.

-Pravin
4 REPLIES 4
Mike Stroyan
Honored Contributor

Re: Shell Script

Use the $! shell variable to get the pid of the most recent background process.
vbj abcd &
echo $! > pid1
vbj efgh &
echo $! > pid2
Ovidiu D. Raita
Valued Contributor

Re: Shell Script

Did I get it right? Are you running vbj abcd & 5 times ? If yes, you should be able to run
# jobs
and you get the pids for all the background processes.
OR
you can kill them by running
# kill -9 %1 <--- this will kill the first process
# kill -9 %2 <---- this the second one

ans so on...
Simple solutions to complex problems
Kevin Ernst
Regular Advisor

Re: Shell Script

Perhaps you would not want to use 'kill -9'. Signal 9 (SIGKILL) doesn't give the killed process a chance to clean up after itself--de-allocate memory, remove temp files, etc. This might not be much of a concern for very simple binaries, but otherwise I would worry.

Try just plain 'kill' first. 'kill' without any arguments uses signal 15 (SIGTERM) by default, which is the most polite way to ask a process to terminate. If that doesn't work try 'kill -2' (SIGINT) and 'kill -1 (SIGHUP)'.

If none of those work, then you have no other resort but to use 'kill -9'. Well-written binaries will probably respond to one of the other signals first, though.
Ovidiu D. Raita
Valued Contributor

Re: Shell Script

Kevin is right. Thanks.

Ovidiu
Simple solutions to complex problems