1

I have found this solution to do a process locking mechanism in python:

import socket
import sys
import time

def get_lock(process_name):
    global lock_socket
    lock_socket = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
    try:
        lock_socket.bind('\0' + process_name)
        print 'I got the lock'
    except socket.error:
        print 'lock exists'
        sys.exit()

get_lock('running_test')
while True:
    time.sleep(3)

this was found here: Check to see if python script is running

Can the same be achieved in bash? If so, could someone please post a working example?

Many thanks

Community
  • 1
  • 1
Jan
  • 1,054
  • 13
  • 36
  • Without more explanation why you need the above in `bash` probably would't get much answers. It smells like [xy-problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Explain what you want achieve instead of _locking socket in bash_ ... – clt60 Oct 13 '14 at 10:39

2 Answers2

3

While I wrote a bash script editing a file, meanwhile multiple process instances of this script need run simultaneously, and this caused some race condition which may mess my file. I found a mutual exclusive lock solution to solve my problem:

    # Add mutual exclusive lock in case there's race condition.
    {
        flock 201
        echo 'replace this echo with actions for your case' >> /tmp/file_to_edit
    } 201> /tmp/file_used_as_lock 

Please note flock has a option -n, which means failure rather than wait. So with this option, you can decide to output error message instead of waiting.

Please refer to this http://mywiki.wooledge.org/BashFAQ/045.

Community
  • 1
  • 1
Qiu Yangfan
  • 871
  • 11
  • 25
0

I've used the following code fragment to avoid multiple concurrent invocation of a bash script

#!/usr/bin/env bash
ScriptName=${0##*/}
FlockFile="/tmp/${ScriptName}.lck"
touch ${FlockFile} 2>/dev/null
[[ $? -ne 0 ]] && echo "Cannot access lock file ${FlockFile}" && exit 2
exec 5>${FlockFile}
flock -nx 5
[[ $? -ne 0 ]] && echo "Another instance of ${ScriptName} is running" && exit 1
chmod 666 ${FlockFile} 2>/dev/null
# Main script here
Alessandro Da Rugna
  • 4,571
  • 20
  • 40
  • 64