Asked By
H. George
0 points
N/A
Posted on - 07/22/2016
When we are working on multitenant Linux based systems that shares the same hardware resource with a number of users we need shells that can run on just one instance on every single computer connected. How can we temporarily achieve that if we are not ready for complicated automation?
Just A Shell For Everyone
The below is a sample code in bash:
LOCKFILE=/tmp/lock-‘example’
If [-e ${LOCKFILE} ] && kill -0 ‘cat $[LOCKFILE]’; then
Echo “Instance already running”
Exit 1
Fi
Trap “rm –f {$LOCKFILE}; exit” INT TERM EXIT
Echo $$> $LOCKFILE
The above can be an example which we can follow to stop multiple instances and save our physical memory. Let us start with defining an instance we are try to prevent from multiplying by defining a lockfile as we have shown in the first line of the bash code. In the second line of the code we are checking if the file exists and if the process id of the file is running, if so we simply provide an output that the file is running. Now we create a trap so that if multiple instances are already there we can try a clean exit for the lockfile even if that fails we create a lockfile to store the process id of the previous multiple instance case.