1

The following script works as expected when executed from an Applescript do shell script command.

#!/bin/sh
sleep 10 &
#echo "hello world" > /tmp/apipe &
cpid=$!
sleep 1
if ps -ef | grep $cpid | grep sleep | grep -qv grep ; then
   echo "killing blocking  cmd..."
   kill -KILL $cpid

   # non zero status to inform launch script of problem...
   exit 1
fi

But, if the sleep command (line 2) is swaped to the echo command in (line 3) together with the if statement, the script blocks when run from Applescript but runs fine from the terminal command line.

Any ideas?

EDIT: I should have mentioned that the script works properly when a consumer/reader is connected to the pipe. It only block when nothing is reading from the pipe...

Dude named Ben
  • 507
  • 3
  • 16
  • I can concur that I've experienced this issue. Thanks for posting this. For more info and data points, see this: http://apple.stackexchange.com/questions/201518/applescript-will-not-stop-running?noredirect=1#comment243765_201518 – jml Aug 26 '15 at 18:12

1 Answers1

1

OK, the following will do the trick. It basically kills the job using its jobid. Since there is only one, it's the current job %%.

I was lucky that I came across the this answer or it would have driven me crazy :)

#!/bin/sh
echo $1 > $2 &

sleep 1

# Following is necessary. Seems to need it or
# job will not complete! Also seen at
#  https://stackoverflow.com/a/10736613/348694
echo "Checking for running jobs..."
jobs

kill %% >/dev/null 2>&1

if [ $? -eq 0 ] ; then
   echo "Taking too long. Killed..."
   exit 1
fi

exit 0
Community
  • 1
  • 1
Dude named Ben
  • 507
  • 3
  • 16