lockf(1)2007年10月27日 13時09分25秒

ふと、面白いコマンドを見つけた。FreeBSD では、結構古い部類に入るコマンドだ。UNIX や FreeBSD の知識はそれなりにあるとは思っているが、インストールされているコマンドの数は多い。また、特定のプラットフォームだけにしかないコマンドは、古くから入っていたとしてもなかなか気が付かないこともある。

% man lockf
NAME
     lockf -- execute a command while holding a file lock

SYNOPSIS
     lockf [-ks] [-t seconds] file command [arguments]

DESCRIPTION
     The lockf utility acquires an exclusive lock on a file, creating it if
     necessary, and removing the file on exit unless explicitly told not to.
     While holding the lock, it executes a command with optional arguments.
     After the command completes, lockf releases the lock, and removes the
     file unless the -k option is specified.  BSD-style locking is used, as
     described in flock(2); the mere existence of the file is not considered
     to constitute a lock.

lockf はコマンドを実行中の間、指定されたファイルをロックするコマンドだ。使い方としては、nice や time などの様に、使いたいコマンドを渡せばいいようだ。

この様に、シェルスクリプトなどから直接使える排他制御を出来る機構があると、色々と便利だ。