带有管道的 Shell 脚本在 crontab 中不起作用

带有管道的 Shell 脚本在 crontab 中不起作用

我有一个 shell 脚本,手动运行时工作正常,但通过 crontab 运行时失败。该脚本基本上执行以下操作:

Python script to get audio data and pipe to stdout | ffmpeg the data from stdin and pipe to stdout | stream the data from stdin

当通过 crontab 运行时,流式传输失败,抱怨 stdin ( ) 处没有数据...No (more) data available on standard input

我发现答案似乎暗示了 crontab 中文件描述符的问题,但我希望了解有关该问题的更多详细信息以及解决该问题的最佳方法。

编辑: 通过分别尝试每个单独的命令来解决问题表明问题是在 python 脚本中开始的,该脚本抱怨:

close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr"

而不是输出音频数据。遵循建议这里这里(将 sys.stdout.flush() 添加到文件末尾)我可以看到实际的错误消息是:

Traceback (most recent call last):
  File "/home/*username*/testing.py", line 109, in <module>
    sys.stdout.flush()
IOError: [Errno 9] Bad file descriptor

所以也许这更多是一个Python问题..尽管从错误来看它似乎仍然与stdin/stdout有关

答案1

事实证明,问题确实是 python 文件和 cron,但不是我预期的文件描述符(stdin/stdout)的问题。

相反,根据这个答案在运行 cron 时有一行要求用户输入是导致问题的原因。我通过删除用户输入的请求解决了这个问题,因为对我来说这是不必要的。

相关内容