• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

flock: command not found

by Deepika

Flock is a command-line utility that allows for the management of file locks from shell scripts. It can be used to ensure that only one process of a command is running at a time. This is particularly useful in situations where multiple processes may be trying to execute the same command simultaneously, and you want to ensure that only one of them succeeds.

The basic usage of flock is to execute a command while holding a lock on a specified file. If the lock is already held by another process, flock will wait until the lock is released before executing the command. For example, the following command would execute the “my-command” command while holding a lock on the file “my-file.lock”:

# flock my-file.lock --command my-command

You can also specify the option to lock the file exclusively so that other processes cannot open the file at the same time. Flock also provides options for controlling the behavior of the lock, such as setting a timeout for acquiring the lock, or non-blocking mode which will exit immediately if the lock cannot be acquired.

Flock is particularly useful in shell scripts where multiple processes may be trying to perform the same action, and you want to ensure that only one of them succeeds. It can also be used to synchronize access to resources such as databases, where multiple processes may be trying to update the same data.

If you encounter the below error while running the command flock:

flock: command not found

you may try installing the below package as per your choice of distribution:

Distribution Command
Debian apt-get install util-linux
Ubuntu apt-get install util-linux
Alpine apk add util-linux
Arch Linux pacman -S util-linux
Kali Linux apt-get install util-linux
CentOS yum install util-linux
Fedora dnf install util-linux
OS X brew install util-linux
Raspbian apt-get install util-linux

flock Command Examples

1. Run a command with a file lock as soon as the lock is not required by others:

# flock path/to/lock.lock --command "command"

2. Run a command with a file lock, and exit if the lock doesn’t exist:

# flock path/to/lock.lock --nonblock --command "command"

3. Run a command with a file lock, and exit with a specific error code if the lock doesn’t exist:

# flock path/to/lock.lock --nonblock --conflict-exit-code error_code -c "command"

Filed Under: Linux

Some more articles you might also be interested in …

  1. CentOS / RHEL 7 : How to disable IPv6 on a specific interface only
  2. How to Disable/Enable services in Zimbra Mail Server
  3. “btrfs” command examples to Create and Manage Btrfs File System
  4. How to test a PHP script
  5. lvresize: command not found
  6. CentOS / RHEL : How to set chroot jail for vsftp for all the users
  7. gnome-software Command Examples in Linux
  8. CentOS / RHEL 6 : How do I find my current runlevel?
  9. vim: command not found
  10. awk: command not found

You May Also Like

Primary Sidebar

Recent Posts

  • ctags: Generates an index (or tag) file of language objects found in source files for many popular programming languages
  • csvtool: Utility to filter and extract data from CSV formatted sources
  • csvstat: Print descriptive statistics for all columns in a CSV file
  • csvsql: Generate SQL statements for a CSV file or execute those statements directly on a database

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright