Quantcast

Killing Ant process through shell script when multiple Java processes are running...

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Killing Ant process through shell script when multiple Java processes are running...

Harry_
Hello,

I want to schedule a task using ant using crontab.

On shell script will start the ant and the task I want to run. This is easy.

One shell script will kill the above ant process.

But the problem is that multiple java processes are running on machine. I only want to kill only the ant process, How can I do that. Any Ideas.

Like noting process ID of ant process created above and then killing it with kill script. I do not know how to do this. Someone point me to link where I can get information about how to do this.

Thanks and Regards
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Killing Ant process through shell script when multiple Java processes are running...

Scot P. Floess-2

what about pkill on the ant process itself?

On Tue, 24 Nov 2009, Harry_ wrote:

>
> Hello,
>
> I want to schedule a task using ant using crontab.
>
> On shell script will start the ant and the task I want to run. This is easy.
>
> One shell script will kill the above ant process.
>
> But the problem is that multiple java processes are running on machine. I
> only want to kill only the ant process, How can I do that. Any Ideas.
>
> Like noting process ID of ant process created above and then killing it with
> kill script. I do not know how to do this. Someone point me to link where I
> can get information about how to do this.
>
> Thanks and Regards
> --
> View this message in context: http://old.nabble.com/Killing-Ant-process-through-shell-script-when-multiple-Java-processes-are-running...-tp26498920p26498920.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

Scot P. Floess
27 Lake Royale
Louisburg, NC  27549

252-478-8087 (Home)
919-890-8117 (Work)

Chief Architect JPlate   http://sourceforge.net/projects/jplate
Chief Architect JavaPIM  http://sourceforge.net/projects/javapim

Architect Keros          http://sourceforge.net/projects/keros

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Killing Ant process through shell script when multiple Java processes are running...

Brian Pontarelli
In reply to this post by Harry_
It will depend on the OS that you are running and grabbing the PID. In most unix systems, the PID of the last executed process is in the variable $!. You can store this in a file for later retrieval if necessary. You can also add a very specific -D property and then use a combination of the ps, grep and awk commands to figure out the PID.

java -Dmyantprocess

ps -efww | grep 'myantprocess' | awk '{print $1}'

All of this again depends on your OS and the available tools you have.

-bp


On Nov 24, 2009, at 10:32 AM, Harry_ wrote:

>
> Hello,
>
> I want to schedule a task using ant using crontab.
>
> On shell script will start the ant and the task I want to run. This is easy.
>
> One shell script will kill the above ant process.
>
> But the problem is that multiple java processes are running on machine. I
> only want to kill only the ant process, How can I do that. Any Ideas.
>
> Like noting process ID of ant process created above and then killing it with
> kill script. I do not know how to do this. Someone point me to link where I
> can get information about how to do this.
>
> Thanks and Regards
> --
> View this message in context: http://old.nabble.com/Killing-Ant-process-through-shell-script-when-multiple-Java-processes-are-running...-tp26498920p26498920.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Killing Ant process through shell script when multiple Java processes are running...

Harry_
Thanks for the reply...

I am working on Ubuntu and I did the following:

While running the ant I output the PID into a file like:

ant -f perf_NormalLoad.xml & echo $! > /tmp/java.pid

For stopping the process I used:

PIDFILE=`cat /tmp/java.pid`
echo 'Killing Java process from PID file'  
echo $PIDFILE
kill -9 $PIDFILE

This is also killing the child processes started by the ant. Please let me know if I can improve on this.

And one more thing I would like to ask is about how to get the list of processes created by a process. I want this to make sure that all the processes created by parent ant process are also killed once I Killed ant process. Any way ant can write the output of new processes created into a file. Then we can read them and  kill them after killing the ant process.

I was trying jps command but it lists all the running jvm process ID's but I want only the ID's of processes started by ant process we started (whose ID we recorded in a file). Is there any way to perform this.

Regards


Brian Pontarelli wrote
It will depend on the OS that you are running and grabbing the PID. In most unix systems, the PID of the last executed process is in the variable $!. You can store this in a file for later retrieval if necessary. You can also add a very specific -D property and then use a combination of the ps, grep and awk commands to figure out the PID.

java -Dmyantprocess

ps -efww | grep 'myantprocess' | awk '{print $1}'

All of this again depends on your OS and the available tools you have.

-bp


On Nov 24, 2009, at 10:32 AM, Harry_ wrote:

>
> Hello,
>
> I want to schedule a task using ant using crontab.
>
> On shell script will start the ant and the task I want to run. This is easy.
>
> One shell script will kill the above ant process.
>
> But the problem is that multiple java processes are running on machine. I
> only want to kill only the ant process, How can I do that. Any Ideas.
>
> Like noting process ID of ant process created above and then killing it with
> kill script. I do not know how to do this. Someone point me to link where I
> can get information about how to do this.
>
> Thanks and Regards
> --
> View this message in context: http://old.nabble.com/Killing-Ant-process-through-shell-script-when-multiple-Java-processes-are-running...-tp26498920p26498920.html
> Sent from the Ant - Users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
> For additional commands, e-mail: user-help@ant.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@ant.apache.org
For additional commands, e-mail: user-help@ant.apache.org
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Killing Ant process through shell script when multiple Java processes are running...

robert lazarski
On Wed, Nov 25, 2009 at 6:49 AM, Harry_ <[hidden email]> wrote:
>
> And one more thing I would like to ask is about how to get the list of
> processes created by a process. I want this to make sure that all the
> processes created by parent ant process are also killed once I Killed ant
> process. Any way ant can write the output of new processes created into a
> file. Then we can read them and  kill them after killing the ant process.
>

I use this in bash for killing the desired process and its child
processes. Some pid's might duplicate in the list that gets sent to be
killed, which is not a problem. ymmv.

function killSubs () {
    process_list=`ps -ef`
    printf "\n\n'ps' output before PID's were killed: \n$process_list"
>> $stdout_this_script
    sub_ids_all=`ps ef -o pid,ppid`
    printf "\n\nsub_ids_all: \n$sub_ids_all" >> $stdout_this_script
    sub_ids=`ps ef -o pid,ppid | grep $CURRENT_PID | awk '{ print $1 '}`
    printf "\n\nsub_ids for $CURRENT_PID: \n$sub_ids" >> $stdout_this_script
    # guarantee at least one pid is sent to be killed,
    # ideally its also able to find child id's
    sub_ids=$CURRENT_PID" "$sub_ids
    printf "\n\nsub_ids with CURRENT_PID $CURRENT_PID: \n$sub_ids" >>
$stdout_this_script
    while read -ra pid_number; do
      for i in "${pid_number[@]}"; do
          child_id=`ps ef -o pid,ppid | grep $i | awk '{ print $1 '}`
          printf "\n\nfound child_id: \n$child_id , from ppid: $i " >>
$stdout_this_script
          sub_ids=$child_id" "$sub_ids
      done
    done <<< "$sub_ids"
    printf "\n\nkilling child pids: $sub_ids " >> $stdout_this_script
    kill -9 $sub_ids >> $stdout_this_script 2>&1
    return
}

stdout_this_script="my_logs_`date +%F_%H_%M_%S`.log"
CURRENT_PID=$!
killSubs >> $stdout_this_script 2>&1

- R

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Killing Ant process through shell script when multiple Java processes are running...

Brian Pontarelli

On Nov 25, 2009, at 3:06 AM, robert lazarski wrote:

> On Wed, Nov 25, 2009 at 6:49 AM, Harry_ <[hidden email]> wrote:
>>
>> And one more thing I would like to ask is about how to get the list of
>> processes created by a process. I want this to make sure that all the
>> processes created by parent ant process are also killed once I Killed ant
>> process. Any way ant can write the output of new processes created into a
>> file. Then we can read them and  kill them after killing the ant process.
>>
>
> I use this in bash for killing the desired process and its child
> processes. Some pid's might duplicate in the list that gets sent to be
> killed, which is not a problem. ymmv.
>
> function killSubs () {
>    process_list=`ps -ef`
>    printf "\n\n'ps' output before PID's were killed: \n$process_list"
>>> $stdout_this_script
>    sub_ids_all=`ps ef -o pid,ppid`
>    printf "\n\nsub_ids_all: \n$sub_ids_all" >> $stdout_this_script
>    sub_ids=`ps ef -o pid,ppid | grep $CURRENT_PID | awk '{ print $1 '}`
>    printf "\n\nsub_ids for $CURRENT_PID: \n$sub_ids" >> $stdout_this_script
>    # guarantee at least one pid is sent to be killed,
>    # ideally its also able to find child id's
>    sub_ids=$CURRENT_PID" "$sub_ids
>    printf "\n\nsub_ids with CURRENT_PID $CURRENT_PID: \n$sub_ids" >>
> $stdout_this_script
>    while read -ra pid_number; do
>      for i in "${pid_number[@]}"; do
>          child_id=`ps ef -o pid,ppid | grep $i | awk '{ print $1 '}`
>          printf "\n\nfound child_id: \n$child_id , from ppid: $i " >>
> $stdout_this_script
>          sub_ids=$child_id" "$sub_ids
>      done
>    done <<< "$sub_ids"
>    printf "\n\nkilling child pids: $sub_ids " >> $stdout_this_script
>    kill -9 $sub_ids >> $stdout_this_script 2>&1
>    return
> }
>
> stdout_this_script="my_logs_`date +%F_%H_%M_%S`.log"
> CURRENT_PID=$!
> killSubs >> $stdout_this_script 2>&1
>

Seems like it should be simpler than this since PS can list out all processes and threads along with their parent's PID. You already know the parent's PID and then you can just run a ps -efww and grep for that. Then, a small awk and for loop should work. Something like this:

for p in $(ps -efww | grep $PID | awl '{print $2}'); do
  kill -9 $p
done

Not sure if my assumptions are correct, but it seems like that would work. You could also format the output of PS to be very specific and just contain the PID and parent PID (at least I think you can do that).

-bp



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...