【问题标题】:Running shell script with xinetd in single instance mode在单实例模式下使用 xinetd 运行 shell 脚本
【发布时间】:2012-12-28 07:29:33
【问题描述】:

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

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

【问题讨论】:

    标签: linux bash sockets shell xinetd


    【解决方案1】:

    在 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 子句会在您的进程完成后启动。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      • 2018-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多