문제

I would like to give a user some feedback when he is trying to execute a remote script that is started by xinetd and runs as single instance.

Via an UDP or TCP socket, the user sends some text to the port where xinetd is listening. If the script is not already in use, it is executed, but if not I would like to give the user some feedback like "Sorry, this script is already in use. Try again later." How could this be done? Thanks for any help.

도움이 되었습니까?

해결책

On Linux you can use the flock utility from util-linux:

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

Just make sure that the subshell in () ends with success (like with exit 0 here), otherwise the then-clause kicks in after your process is finished.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top