How to ensure only one instance of a bash script is running at a time

Here is my  fail-safe method of ensuring that only one instance of a shell script is running at a time, by combining: pid, lock directory and lock file.


function sh_lock_init {
	sh_lock_scriptName=$(basename $0)
	sh_lock_dir="/tmp/${sh_lock_scriptName}.lock" #lock directory
	sh_lock_file="${sh_lock_dir}/lockPid.txt" #lock file

function sh_aquire_lock {
	if mkdir $sh_lock_dir 2>/dev/null; then #check for lock
		echo "$sh_lock_scriptName lock acquired successfully.">&2
		touch $sh_lock_file
		echo $$ > $sh_lock_file # set current pid in lockFile
		return 0
		touch $sh_lock_file
		read sh_lock_lastPID < $sh_lock_file
		if [ ! -z "$sh_lock_lastPID" -a -d /proc/$sh_lock_lastPID ]; then # if lastPID is not null and a process with that pid exists
			echo "$sh_lock_scriptName is already running.">&2
			return 1
			echo "$sh_lock_scriptName stopped during execution, reacquiring lock.">&2
			echo $$ > $sh_lock_file # set current pid in lockFile
			return 2
	return 0

function sh_check_lock {
	[[ ! -f $sh_lock_file ]] && echo "$sh_lock_scriptName lock file removed.">&2 && return 1
	read sh_lock_lastPID < $sh_lock_file
	[[ $sh_lock_lastPID -ne $$ ]] && echo "$sh_lock_scriptName lock file pid has changed.">&2  && return 2
	echo "$sh_lock_scriptName lock still in place.">&2
	return 0

function sh_remove_lock {
	rm -r $sh_lock_dir

. /path/to/ # load sh lock functions

sh_lock_init || exit $?

[[ $lockStatus -eq 1 ]] && exit $lockStatus
[[ $lockStatus -eq 2 ]] && echo "lock is set, do some resume from crash procedures";

#monitoring example
while sh_check_lock # loop while lock is in place
	echo "$sh_scriptName running (pid $$)"
	sleep 1
	let cnt++
	[[ $cnt -gt 5 ]] && break

#remove lock when process finished
sh_remove_lock || exit $?

exit 0


  • Is safe and tested, uses a combination of file, directory and process id lock
  • You can detect if the script stopped before unlock, and trigger recovery procedures
  • You can stop the script if you remove the lock file

You can also collaborate or comment on the solution at

Json to CSV conversion tool

I’ve had to convert a json to csv and couldn’t find a proper tool that can handle nested arrays and objects serialisation, and also be able to handle large json files.

You can find the beta version here :

Json2CSV convertor features:

  • handles anything that is an array of objects  [{},{},{}]
  • converts embedded objects (e.g. [{[]},{{}},{[{}]}])  to array of simple objects (e.g. [{},{},{}]) for csv conversion
  • automatically detects the row with the highest number of columns (or generated columns ) and sets it as header

Todo :

  • add interface options for csv output
  • rafine error handling
  • load from file
my portfolio, always in construction

Under construction!

My portfolio is and probably will always be in construction, as my work experience grows so will this website expand.

Now I’m in the middle of redesigning this website, if you wish to know more about me  you can view my updated career profile or you can subscribe for updates below and I’ll sen you a email when I’ll have new things to show.

If you have questions, you can contact me by e-mail at