Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

On Linux I use flock lock command to execute a command with an exclusive lock.

What is the standard operating system command of Solaris 10 to do the same in a shell?

share|improve this question

3 Answers 3

There is no flock or similar command for Solaris. If I want to do simple locking I use mkdir as it's a atomic operation and avoids potential race conditions with the usual check file exists/touch combination.

if ! mkdir /tmp/lockdir >/dev/null 2>&1
    echo >&2 "Lock exists exiting"
    exit 1
share|improve this answer
Is mkdir always atomic? I seem to recall buggy NFS implementations where it isn't, but then Solaris NFS is less buggy than most. – Gilles May 11 '11 at 20:45
@Gilles: I don't know - I always create lock files in /tmp. – Iain May 11 '11 at 20:47
@Gilles, mkdir is meant to be atomic over NFS and is actually why it's recommended over a open/creat with O_EXCL. Now it is very well possible that there exist buggy implementations. Having said that, of all places, /tmp is the least likely to be over NFS – Stéphane Chazelas Jan 18 '13 at 15:30
@StephaneChazelas Buggy NFS implementations aren't that rare: – Gilles Jan 18 '13 at 21:51
up vote 1 down vote accepted

After a small Usenet discussion I use the following as a workaround for flock -n lockfile -c command:

#! /bin/bash

if [ $# != 4 -o "$1" = '-h' ] ; then
   echo "Usage: flock -n lockfile -c command" >&2
   exit 1


set -o noclobber
if 2>/dev/null : > "$lockfile" ; then
   trap 'rm -f "$lockfile"' EXIT
   $BASH -c "$command"
   exit 1
share|improve this answer

I'd combine the two ideas:

getLock() {
    PROG=$( basename $0 )

    if mkdir "$SHAREDLOCK" >/dev/null 2>&1
            trap 'rmdir "$SHAREDLOCK"' EXIT
            chmod 0 "$SHAREDLOCK" # discourage anyone from messing with it else the rmdir might fail
            echo >&2 "Lock ($SHAREDLOCK) exists. exiting"
            exit 1


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.