0

I'm watching wpa_supplicant so I can kill my script if the password is wrong.

I background the whole code block below. I can see the echo run but the exit doesn't seem to stop my main script.

  (sudo stdbuf -o0 wpa_supplicant -Dwext -i$wifi -cwifi.conf 2>&1 \
    | grep -m 1 "pre-shared key may be incorrect" \
    && echo I see this \
    && exit) &

I suspect the exit here is just killing a thread which has been backgrounded?

Is that the case? If so how can I kill the parent here?

1

You can pass the pid of your main script to the subshell and then kill it when you want:

export mypid=$$

(sudo stdbuf -o0 wpa_supplicant -Dwext -i$wifi -cwifi.conf 2>&1 \
| grep -m 1 "pre-shared key may be incorrect" \
&& echo I see this \
&& kill $mypid) &
1

Right, exit is affecting the process which has been backgrounded, arranging for zero exit status. I wouldn't quite call it "killing", as the process was on the verge of halting anyway.

Run the wpa command in the background, and keep track of its PID so you don't have to use killall:

bash -c 'echo $$ > /tmp/pid; exec wpa_supplicant 2>&1 > /tmp/out' &
sleep 1
egrep 'pre-shared key may be incorrect' /tmp/out && kill `cat /tmp/pid`

I know, your wpa command is a bit longer, I abbreviated to call attention to the important parts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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