gpt4 book ai didi

linux - 在单实例模式下使用 xinetd 运行 shell 脚本

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:32 26 4
gpt4 key购买 nike

当用户尝试执行由 xinetd 启动并作为单个实例运行的远程脚本时,我想为他提供一些反馈。

通过 UDP 或 TCP 套接字,用户将一些文本发送到 xinetd 正在监听的端口。如果该脚本尚未被使用,它就会被执行,但如果没有,我想给用户一些反馈,比如“抱歉,该脚本已被使用。稍后再试。”这怎么可能呢?感谢您的帮助。

最佳答案

在 Linux 上,您可以使用 util-linux 中的 flock 实用程序:

if ! ( flock -n 42 || exit 1

# Things to do in the single instance
echo "Processing..."
sleep 60
echo "Done"

exit 0
) 42> /var/tmp/mylockfile # any unique filename
then
# Things to do when the script is in use
echo "Sorry, the service is in use."
fi

只需确保 () 中的子 shell 以成功结束(如此处的 exit 0),否则 then 子句将在您的过程完成后启动。

关于linux - 在单实例模式下使用 xinetd 运行 shell 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13685411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com