写入管道时,applescript 块 shell 脚本 cmd

applescript blocks shell script cmd when writing to pipe

从 Applescript do shell script 命令执行时,以下脚本按预期工作。

#!/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

但是,如果将 sleep 命令(第 2 行)与 if 语句一起交换为(第 3 行)中的 echo 命令,脚本会在 运行 来自 Applescript 但 运行s 时阻塞从终端命令行很好。

有什么想法吗?

编辑:我应该提到当 consumer/reader 连接到管道时脚本可以正常工作。它只会在没有从管道读取任何内容时阻塞...

好的,下面就可以了。它基本上使用它的 jobid 杀死了工作。因为只有一个,所以就是现在的工作%%.

我很幸运遇到了 this answer 否则它会把我逼疯的:)

#!/bin/sh
echo  >  &

sleep 1

# Following is necessary. Seems to need it or
# job will not complete! Also seen at
#  
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