[comp.unix.shell] Problems with Job Control in csh

streeter@theory.lcs.mit.edu (Kenneth B Streeter) (04/09/91)

I'm having problems with trying to do job control in a csh script.  I
want to be able to commence a background job (I'm using the &
metacharacter), use it for some request-handling, and then later kill
the backgrounded job.

I'm trying something like the following: (text in brackets is stuff
specific to my application, and not generally necessary)

    #!/bin/csh -f

    xterm -e "<my-executable>" &
    # <processing deleted>
    kill %1


The intent with the 'kill' is to kill the backgrounded job (the
xterm).  If I do a 'jobs' before the kill, the xterm job is listed as
an active job (Running) under job control with job number [1].
However, the kill statement terminates with "%1: No such process".

What is wrong with my understanding here?
--
Kenneth B. Streeter         | ARPA: streeter@im.lcs.mit.edu
MIT LCS, Room NE43-350      | UUCP: ...!uunet!im.lcs.mit.edu!streeter
545 Technology Square       | (617) 253-2614    (work)
Cambridge, MA 02139         | (617) 225-2249    (home)  

chet@odin.INS.CWRU.Edu (Chet Ramey) (04/09/91)

>I'm having problems with trying to do job control in a csh script.

There's no job control in non-interactive shells.  This is true of csh,
bash, and ksh.

-- 
Chet Ramey			  Internet: chet@po.CWRU.Edu
Case Western Reserve University	  NeXT Mail: chet@macbeth.INS.CWRU.Edu

``Now,  somehow we've brought our sins back physically -- and they're pissed.''

pfalstad@phoenix.Princeton.EDU (Paul Falstad) (04/10/91)

streeter@theory.lcs.mit.edu (Kenneth B Streeter) wrote:
>I'm having problems with trying to do job control in a csh script.  I
>want to be able to commence a background job (I'm using the &
>metacharacter), use it for some request-handling, and then later kill
>the backgrounded job.
>    xterm -e "<my-executable>" &
>    # <processing deleted>
>    kill %1

You can't refer to process with the "%<number>" construct in
noninteractive shells in csh; you can only do it when job control is in
effect.  Really, this should not require job control to be in effect;
for example, this script will work fine in zsh, and also in itcsh, I
believe.  But if you're using an sh-derived shell (sh,ksh,bash,...) this
will work fine:

xterm -e "foobar" &
# ...
kill -9 $!

I think the only way to do this in csh is to do something like this:

xterm -e "foobar" &
# ...
kill -9 -$$
# end of script

where the "kill" kills off the shell and the xterm by killing the
whole process group.  You're better off not to write the script in csh IMHO.

--
Paul Falstad, pfalstad@phoenix.princeton.edu | 10 PRINT "PRINCETON CS"
[Your blood pressure just went up.]          | 20 GOTO 10
Princeton University would like to apologize to everyone for this article.

mju@mudos.ann-arbor.mi.us (Marc Unangst) (04/11/91)

pfalstad@phoenix.Princeton.EDU (Paul Falstad) writes:
> xterm -e "foobar" &
> # ...
> kill -9 $!

Blech.  Here, try this:

TEMP=/tmp/$$.`whoami`
(xterm -e "foobar" &) 2>$TEMP
# ...
kill -9 `cat $TEMP`

--
Marc Unangst               |
mju@mudos.ann-arbor.mi.us  | "Bus error: passengers dumped"
...!hela!mudos!mju         |