From: Jilles Tjoelker <jilles@stack.nl>
To: Dan Muresan <danmbox@gmail.com>
Cc: dash@vger.kernel.org
Subject: Re: jobs in scripts?
Date: Wed, 9 Mar 2011 23:54:13 +0100 [thread overview]
Message-ID: <20110309225413.GA45552@stack.nl> (raw)
In-Reply-To: <AANLkTinDKV9uCZQPhh8LtX9fAwYix0+9bP5LTXAuSCAL@mail.gmail.com>
On Tue, Mar 08, 2011 at 09:02:30PM +0200, Dan Muresan wrote:
> Hi, is the following normal? With set -m the problem disappears; bash
> & ksh don't need set -m.
> SUSv3 doesn't say job control is to be disabled completely unless "set
> -m" is active. And, if job control *is* disabled, why does "job"
> report a non-empty list?
> 0.5.5.1-3ubuntu2 on Ubuntu 10.04 here.
> ---
> $ cat x
> #set -m
> sleep 100 &
> echo "Before kill:"; jobs -l
> kill -s TERM %1
> sleep 0.5
> echo "After kill:"; jobs -l
> pidof sleep
> $ dash x
> Before kill:
> [1] + 18775 Running
> kill: 4: No such process
> After kill:
> [1] + 18775 Running
> 18775
See the description of "set" in XCU 2.14 Special Built-In Utilities. It
is deliberate that "jobs" still works, as "set -m" only enables putting
jobs in separate process groups and various notices that are only
meaningful in interactive mode (there are no prompts in non-interactive
mode, therefore the notices about completed and stopped background jobs
do not happen, and if a foreground job stops in non-interactive mode
this tends to lead to unexpected results).
That much is clear. However, "kill" and "jobs -p" are less clear. Their
descriptions depend on the process group of a job, which does not exist
if job control was disabled when the job was started -- in that case the
job is run in the shell's process group. This could be interpreted as an
implicit requirement on the application that the job must have been
started with job control enabled if "kill" or "jobs -p" are to be used
on it.
In your case, I recommend getting the PID via $!. This allows 'kill'
without depending on obscure corners of shells.
If your script runs for a long time, use of $! should be paired with
'wait' (either for all $! values obtained or without parameters) so that
the shell can forget about old jobs. Some shells are buggy and remember
the jobs anyway.
--
Jilles Tjoelker
next prev parent reply other threads:[~2011-03-09 23:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-08 19:02 jobs in scripts? Dan Muresan
2011-03-09 22:54 ` Jilles Tjoelker [this message]
2011-03-09 23:52 ` Dan Muresan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20110309225413.GA45552@stack.nl \
--to=jilles@stack.nl \
--cc=danmbox@gmail.com \
--cc=dash@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.