Re: (kill -1 $$)

From: Stephen Riehm (stephen.riehm_at_gmx.net)
Date: 11/30/04


Date: Tue, 30 Nov 2004 01:49:40 +0100

John DuBois wrote:
> In article <87brdhwl5l.fsf@jidanni.org>,
> Dan Jacobson <jidanni@jidanni.org> wrote:
>
>>Why does this script print 3? [bash]
>>echo 1 $$;(echo 2 $$; kill -1 $$; echo 3); echo 4
>>1 8360
>>2 8360
>>3
>>Hangup
>>I would have thought it would print 4, not 3.
>>Is the only way to stop the subshell "exit"?
>
>
> The value of $$ does not change when you fork a subshell with (). Yes, it
> seems odd, but that's the defined behavior: it's the PID of the last *exec'ed*
> shell in the process' ancestry, not the last *forked* shell. If you did get
> the subshell's pid (with a little more work), then yes, a signal 1 would kill
> it.
>
> John

How bizarre!

Even this doesn't work:

--- start
sh

cat << _EOF_ > ./suicidal_script.sh
#!/bin/sh
echo 2 $$
kill -1 $$
echo 3
_EOF_

chmod +x ./suicidal_script.sh

echo 1 $$; ./suicidal_script.sh; echo 4
--- end

Doing this I got:
1 639
2 639
3
Hangup
Exit 129
(and the shell started on the first line was killed!)
Note: sh is actually bash, if that makes any difference.

I would have at least expected a script started by the shell to run in a
NEW process, and a fresh view of the world (as far as $$ is concerned).

Strangely the original test works as Dan expected in ksh on OS-X!!
(available at www.kornshell.com)

Steve
(Sorry, no answer this time, just more confusion)



Relevant Pages

  • Command ps
    ... Whene I execute this shell in AIX 4.3 ... kill -9 $k ... echo "ECHEC DE TROUVER LE PID DE LA CONNEXION." ...
    (comp.unix.shell)
  • Re: take file name from command line arguement and write to file
    ... the Bourne Again Shell. ... > echo $a ... splitting, parameter substitutions etc. ... script and first sees the eval command, at first it behaves the usual ...
    (comp.unix.shell)
  • Re: exec - usage within a sheel script
    ... When you run a shell program, ... earlier shell processed that command line and split it ... (or echo $@), the 'echo' command receives three arguments: ...
    (comp.unix.shell)
  • Re: [patch 7/9] unprivileged mounts: allow unprivileged fuse mounts
    ... But I'd consider 'kill -9 not working' to be DoS ... before enabling fuse on a multiuser system, ... echo -n "." ... swapoff /c/swap ...
    (Linux-Kernel)
  • Re: ls * problem
    ... echo * = No Match ... I seem to get more errors as above with the machines that have not got this ... When i change to a bash shell and then run ls * it shows the files as ... What would happen if i got rid of the fileutils package from the machines ...
    (RedHat)