public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Killing-Process woes
@ 2017-06-20  6:24 Ronald Otto Valentin Fischer
  2017-06-20 13:18 ` cyg Simple
  0 siblings, 1 reply; 4+ messages in thread
From: Ronald Otto Valentin Fischer @ 2017-06-20  6:24 UTC (permalink / raw)
  To: cygwin

I'm spawning processes in background, but have problems killing them.
Here is the setup:

My script (zsh) creates one or more processes in the background, and
waits until they are finished. I have also set up a trap for SIGINT,
with the intention that if I press Control-C, the background processes
should be killed. I have verified the setup so far, that upon Control-C,
the trap function is indeed invoked, and I have all the PIDs of the
background processes. The problem is with the actual killing, and here
is why:

The background processes are actually (zsh-) scripts, which do some
setup (basically setting various environment variables), and then invoke
a (Cygwin-)Ruby program which does the "real work". The program is
executed by something like

    ruby myprog.rb

(Note that this Ruby program is NOT invoked in background).

When my SIGINT trap is entered, I can see from ps indeed the
relationship between the processes involved, for instance

    10852    9296    6224      10536  cons3    3672028 08:05:10
    /usr/bin/ruby
     9296    6224    6224      11236  cons3    3672028 08:05:10
     /usr/bin/zsh

The PID of my background process - the zsh wrapper - in this concrete
case is 9296, and we can see that this is the parent of the Ruby
process, 10852. The problem is that if I just kill 9296, the Ruby
process keeps running, orphaned:

    10852       1    6224      10536  cons3    3672028 08:05:10
    /usr/bin/ruby

I've found on Stackoverflow the suggestion to treat this as a process
group and use negative PIDs. I tried this too, but it didn't work. Here
is a similar example:

     5548   10276    5812       2376  cons3    3672028 08:20:43
     /usr/bin/ruby
    10276    5812    5812      10312  cons3    3672028 08:20:43
    /usr/bin/zsh

If I do a

    kill -- -10276

I get the error message

     kill: -10276: No such process

This happens both with the zsh-builtin kill and with /usr/bin/kill Is
there a simple way to kill the zsh process in addition to the ruby
process, or do I have to analyze the output of the ps command to
manually find the PID of the Ruby process and kill it?

Ronald
-- 
Ronald Fischer <ronald.fischer@fusshuhn.de>
http://www.fusshuhn.de/


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Killing-Process woes
  2017-06-20  6:24 Killing-Process woes Ronald Otto Valentin Fischer
@ 2017-06-20 13:18 ` cyg Simple
  2017-06-20 13:30   ` Ronald Fischer
  0 siblings, 1 reply; 4+ messages in thread
From: cyg Simple @ 2017-06-20 13:18 UTC (permalink / raw)
  To: cygwin

On 6/20/2017 2:23 AM, Ronald Otto Valentin Fischer wrote:
> I'm spawning processes in background, but have problems killing them.
> Here is the setup:
> 

Maybe review the Cygwin document at
https://cygwin.com/cygwin-ug-net/kill.html would help.

> My script (zsh) creates one or more processes in the background, and
> waits until they are finished. I have also set up a trap for SIGINT,
> with the intention that if I press Control-C, the background processes
> should be killed. I have verified the setup so far, that upon Control-C,
> the trap function is indeed invoked, and I have all the PIDs of the
> background processes. The problem is with the actual killing, and here
> is why:
> 
> The background processes are actually (zsh-) scripts, which do some
> setup (basically setting various environment variables), and then invoke
> a (Cygwin-)Ruby program which does the "real work". The program is
> executed by something like
> 
>     ruby myprog.rb
> 
> (Note that this Ruby program is NOT invoked in background).
> 
> When my SIGINT trap is entered, I can see from ps indeed the
> relationship between the processes involved, for instance
> 
>     10852    9296    6224      10536  cons3    3672028 08:05:10
>     /usr/bin/ruby
>      9296    6224    6224      11236  cons3    3672028 08:05:10
>      /usr/bin/zsh
> 
> The PID of my background process - the zsh wrapper - in this concrete
> case is 9296, and we can see that this is the parent of the Ruby
> process, 10852. The problem is that if I just kill 9296, the Ruby
> process keeps running, orphaned:
> 
>     10852       1    6224      10536  cons3    3672028 08:05:10
>     /usr/bin/ruby
> 
> I've found on Stackoverflow the suggestion to treat this as a process
> group and use negative PIDs. I tried this too, but it didn't work. Here
> is a similar example:
> 

Not implemented as you found out below.  But I don't know that the
negative process number is in use anywhere.  Are you sure it wasn't a
signal number as a option to kill?

>      5548   10276    5812       2376  cons3    3672028 08:20:43
>      /usr/bin/ruby
>     10276    5812    5812      10312  cons3    3672028 08:20:43
>     /usr/bin/zsh
> 
> If I do a
> 
>     kill -- -10276
> 
> I get the error message
> 
>      kill: -10276: No such process
> 
> This happens both with the zsh-builtin kill and with /usr/bin/kill Is
> there a simple way to kill the zsh process in addition to the ruby
> process, or do I have to analyze the output of the ps command to
> manually find the PID of the Ruby process and kill it?

Perhaps use the -f --force switch might help.

-- 
cyg Simple

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Killing-Process woes
  2017-06-20 13:18 ` cyg Simple
@ 2017-06-20 13:30   ` Ronald Fischer
  2017-06-24  8:48     ` Csaba Raduly
  0 siblings, 1 reply; 4+ messages in thread
From: Ronald Fischer @ 2017-06-20 13:30 UTC (permalink / raw)
  To: cyg Simple, cygwin

> > The background processes are actually (zsh-) scripts, which do some
> > setup (basically setting various environment variables), and then invoke
> > a (Cygwin-)Ruby program which does the "real work". The program is
> > executed by something like
> > 
> >     ruby myprog.rb
> > 
> > (Note that this Ruby program is NOT invoked in background).
> > 
> > When my SIGINT trap is entered, I can see from ps indeed the
> > relationship between the processes involved, for instance
> > 
> >     10852    9296    6224      10536  cons3    3672028 08:05:10
> >     /usr/bin/ruby
> >      9296    6224    6224      11236  cons3    3672028 08:05:10
> >      /usr/bin/zsh
> > 
> > The PID of my background process - the zsh wrapper - in this concrete
> > case is 9296, and we can see that this is the parent of the Ruby
> > process, 10852. The problem is that if I just kill 9296, the Ruby
> > process keeps running, orphaned:
> > 
> >     10852       1    6224      10536  cons3    3672028 08:05:10
> >     /usr/bin/ruby
> > 
> > I've found on Stackoverflow the suggestion to treat this as a process
> > group and use negative PIDs. I tried this too, but it didn't work. Here
> > is a similar example:
> > 
> 
> Not implemented as you found out below.  But I don't know that the
> negative process number is in use anywhere.  Are you sure it wasn't a
> signal number as a option to kill?

No, the article refered to a process group (and this indeed would be
done by negative PIDs), but as I said, this didn't work anyway.

> Perhaps use the -f --force switch might help.

No, doesn't help either.

For the time being, I have reverted to analyzing the output of ps. It is
pretty tedious:

# Get the PID of the shell script 
local wrapper_proc=$!
# Give the wrapper some time to start the Ruby process below. Without
this, the
# Ruby process would not be visible yet.
sleep 3
# Find out the PID of the child process of the wrapper
local sub_pid=$(ps |grep -oE "^ *[0-9]+ *$wrapper_proc "|awk ' {print
$1}')
# Sanity check ....
if [[ $sub_pid =~ ^[0-9]+$ ]]
then
  # Add this to the array of these child processes
     additional_pids+=$sub_pid
 else
     echo "Info: Could not extract VP pid from '$sub_pid'"
 fi

Inside my SIGINT trap, I do not only kill the processes found via
$jobstates, but also the processes collected in $additional_pids. An
awful solution, and one which is not easy to maintain and may break!

Ronald

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Killing-Process woes
  2017-06-20 13:30   ` Ronald Fischer
@ 2017-06-24  8:48     ` Csaba Raduly
  0 siblings, 0 replies; 4+ messages in thread
From: Csaba Raduly @ 2017-06-24  8:48 UTC (permalink / raw)
  To: cygwin list; +Cc: cyg Simple

On Tue, Jun 20, 2017 at 3:30 PM, Ronald Fischer  wrote:
> cyg Simple wrote:
>> Perhaps use the -f --force switch might help.
>
> No, doesn't help either.

I thought "Use the force" always works :)

Csaba
-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-06-24  8:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-06-20  6:24 Killing-Process woes Ronald Otto Valentin Fischer
2017-06-20 13:18 ` cyg Simple
2017-06-20 13:30   ` Ronald Fischer
2017-06-24  8:48     ` Csaba Raduly

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).