3

I am writing a script that will launch another process and after a period of time kill it if it hasn't exited on its own. Since starting this, I came to realize that the timeout command is the right way to do this, but I'm still confused about why one of my approaches was failing.

Here's what I was trying:

#!/bin/bash

backgroundScript.sh &

for((i=0; i<60; i++)); do
    if ! kill -0 %1; then
        exit
    fi
    sleep 1
done

kill %1

I was surprised to find that kill -0 %1 was always succeeding, even when the process in the background finished. If I used the PID for that process, so the if within the loop became:

    if ! kill -0 $!; then

it worked fine. So why wasn't the jobspec working for me there?

7
  • Sending signal 0 via the kill() system call only checks whether the process exists; it doesn't actually send any signal to the process. Aug 1, 2015 at 4:46
  • Right, but the job in %1 had terminated so the process did not exist, although the job still did seemingly since testing the pid and jobspec behave differently Aug 1, 2015 at 10:07
  • 2
    I could reproduce the behaviour. Adding a call to jobs in the loop changed the behaviour, depending on the position of the jobs call (sometimes it aborted as expected, sometimes the bug of not-aborting remained). Also, sourcing vs. calling the script ((. ./x.sh) vs. (./x.sh)) evades the bug. I'd call the mechanism of kill -0 %1 brittle. One more thing: strace showed that in the bug case the kill wasn't even executed, even if it was debug printed (via set -x).
    – Alfe
    Aug 3, 2015 at 13:26
  • The shell does not remember which processes have already terminated, it's hard to work around this restriction.
    – fuz
    Aug 3, 2015 at 15:56
  • 1
    ...for that matter, job control is off by default in scripts (more generally, in interactive shells) -- and the script here is doing nothing to reenable it. Aug 3, 2015 at 15:57

2 Answers 2

2

Job control in general, and specifically job specifications like %1, are not portable. The common approach is to save the PID from $! to a variable, and use that as the argument to kill.

3
  • Well, as I noted timeout seems to be the real way to do what I want, I just wanted to know why kill -0 $! worked and kill -0 %1 did not, so I don't think this really addresses my question Aug 3, 2015 at 16:30
  • Huh? I don't see how this could be explained more clearly. %1 only works iff the shell has job control enabled. Though I can speculate that kill ends up with a funny and/or unpredictable argument list when arguably it should fail with an error message when you try to use a jobspec when job control is not available.
    – tripleee
    Aug 3, 2015 at 17:50
  • But kill %1 does work, just kill -0 %1 is unreliable. Also, I'm not asking how to use kill to accomplish this task portably or otherwise, I'm asking why kill works differently when passed two arguments that are tightly related (a jobspec and the PID of the process in that job). I agree that using $! would be the way to go if I were still trying to use kill to reinvent timeout (which I was initially), but that's not my question. So you can in fact kill a jobspec without using set -m. Aug 3, 2015 at 17:57
2

Ok, after a bunch of digging I have uncovered why kill -0 %1 shows the job still exists while kill -0 $! shows the process has exited. bash will not reap the job until the caller has been notified. In an interactive shell that will print to the screen the job termination. In a non-interactive shell, this means the calling script has had a chance to get the return value of the process.

One of the relevant comments that tipped me off from the bash source is in jobs.c:

/* Mark all dead jobs as notified, so delete_job () cleans them out of the job table properly. POSIX.2 says we need to save the status of the last CHILD_MAX jobs, so we count the number of dead jobs and mark only enough as notified to save CHILD_MAX statuses. */

And in the wait_for_single_pid function this snippet:

/* POSIX.2: if we just waited for a job, we can remove it from the jobs
 table. */
BLOCK_CHILD (set, oset);
job = find_job (pid, 0, NULL);
if (job != NO_JOB && jobs[job] && DEADJOB (job))
  jobs[job]->flags |= J_NOTIFIED;
UNBLOCK_CHILD (oset);

Until the caller has been notified that the job is done, it is not done. The process related to it might be done, but the job itself is not done.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.