I have an executable which is on a Server. Many users can login to this server, using SSH and execute the binary.
I would like to have an exclusive access to each user executing this binary. If possible, I would like to have a queue of users, which would be served one by one.
Is it possible to do this using bash script?
Suppose I call my binary as my.exec
and the script as access.sh
.
I can guarantee that each user will access my.exec
only through access.sh
.
So it will be easy to write a wrapper on top of this executable.
In addition the executable needs to be supplied variable arguments.
I have a partial solution here -
#!/bin/bash
# Locking
LOCK_FILE="/tmp/my.lock"
function create_lock_and_execute {
echo "Waiting for Lock"
while [ -f ${LOCK_FILE} ]
do
username=`ls -l ${LOCK_FILE} | cut -f 3 -d " "`
echo $username is using the lock.
sleep 1
done
(
# Wait for lock ${LOCK_FILE} (fd 200).
flock -x 200
echo "Lock Acquired..."
# Execute Command
echo "Executing Command : $COMMAND"
${COMMAND}
) 200>${LOCK_FILE}
}
function remove_lock {
rm -f ${LOCK_FILE}
}
# Exit trap function
function exit_on_error {
echo "Exiting on error..."
remove_lock
exit 1
}
# Exit on kill
function exit_on_kill {
echo "Killed, exiting..."
remove_lock
exit 1
}
# Exit normaly
function exit_on_end {
echo "Exiting..."
remove_lock
exit 0
}
trap exit_on_kill KILL
trap exit_on_error ERR
trap exit_on_end QUIT TERM EXIT INT
create_lock_and_execute
Thanks