{
    "content": [
        {
            "type": "text",
            "text": "# flock(1) (man)\n\n**Summary:** flock - manage locks from shell scripts\n\n**Synopsis:** flock [options] file|directory command [arguments]\nflock [options] file|directory -c command\nflock [options] number\n\n## Flags\n\n| Flag | Long | Arg | Description |\n|------|------|-----|-------------|\n| -c | --command | — | Pass a single command, without arguments, to the shell with -c. |\n| -E | --conflict-exit-code | — | The exit status used when the -n option is in use, and the conflicting lock exists, or the -w option is in use, and the  |\n| -F | --no-fork | — | Do not fork before executing command. Upon execution the flock process is replaced by command which continues to hold th |\n| -x | --exclusive | — | Obtain an exclusive lock, sometimes called a write lock. This is the default. |\n| -n | --nonblock | — | Fail rather than wait if the lock cannot be immediately acquired. See the -E option for the exit status used. |\n| -o | --close | — | Close the file descriptor on which the lock is held before executing command. This is useful if command spawns a child p |\n| -s | --shared | — | Obtain a shared lock, sometimes called a read lock. |\n| -u | --unlock | — | Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it ma |\n| -w | --timeout | — | Fail if the lock cannot be acquired within seconds. Decimal fractional values are allowed. See the -E option for the exi |\n| — | --verbose | — | Report how long it took to acquire the lock, or why the lock could not be obtained. |\n| -V | --version | — | Display version information and exit. |\n| -h | --help | — | Display help text and exit. |\n\n## Examples\n\n- `Note that \"shell> \" in examples is a command line prompt.`\n- `shell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo; /bin/echo $?`\n- `Set exclusive lock to directory /tmp and the second command will fail.`\n- `shell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?`\n- `Set shared lock to directory /tmp and the second command will not fail. Notice that`\n- `attempting to get exclusive lock with second command would fail.`\n- `shell> flock -x local-lock-file echo 'a b c'`\n- `Grab the exclusive lock \"local-lock-file\" before running echo with 'a b c'.`\n- `(; flock -n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile`\n- `The form is convenient inside shell scripts. The mode used to open the file doesn’t`\n- `matter to flock; using > or >> allows the lockfile to be created if it does not already`\n- `exist, however, write permission is required. Using < requires that the file already`\n- `exists but only read permission is required.`\n- `[ ${FLOCKER} != $0 ] && exec env FLOCKER=\"$0 flock -en $0 $0 $@ ||`\n- `This is useful boilerplate code for shell scripts. Put it at the top of the shell`\n- `script you want to lock and it’ll automatically lock itself on the first run. If the`\n- `env var $FLOCKER is not set to the shell script that is being run, then execute flock`\n- `and grab an exclusive non-blocking lock (using the script itself as the lock file)`\n- `before re-execing itself with the right arguments. It also sets the FLOCKER env var`\n- `to the right value so it doesn’t run again.`\n- `shell> exec 4<>/var/lock/mylockfile; shell> flock -n 4`\n- `This form is convenient for locking a file without spawning a subprocess. The shell opens`\n- `the lock file for reading and writing as file descriptor 4, then flock is used to lock`\n- `the descriptor.`\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (6 lines)\n- **DESCRIPTION** (10 lines)\n- **OPTIONS** (1 lines) — 12 subsections\n  - -c --command (2 lines)\n  - -E --conflict-exit-code (4 lines)\n  - -F --no-fork (4 lines)\n  - -e -x --exclusive (2 lines)\n  - -n --nb --nonblock (3 lines)\n  - -o --close (3 lines)\n  - -s --shared (2 lines)\n  - -u --unlock (4 lines)\n  - -w --wait --timeout (4 lines)\n  - --verbose (2 lines)\n  - -V --version (2 lines)\n  - -h --help (2 lines)\n- **EXIT STATUS** (8 lines)\n- **EXAMPLES** (31 lines)\n- **AUTHORS** (2 lines)\n- **COPYRIGHT** (4 lines)\n- **SEE ALSO** (2 lines)\n- **REPORTING BUGS** (2 lines)\n- **AVAILABILITY** (6 lines)\n\n## Full Content\n\n### NAME\n\nflock - manage locks from shell scripts\n\n### SYNOPSIS\n\nflock [options] file|directory command [arguments]\n\nflock [options] file|directory -c command\n\nflock [options] number\n\n### DESCRIPTION\n\nThis utility manages flock(2) locks from within shell scripts or from the command line.\n\nThe first and second of the above forms wrap the lock around the execution of a command, in a\nmanner similar to su(1) or newgrp(1). They lock a specified file or directory, which is\ncreated (assuming appropriate permissions) if it does not already exist. By default, if the\nlock cannot be immediately acquired, flock waits until the lock is available.\n\nThe third form uses an open file by its file descriptor number. See the examples below for\nhow that can be used.\n\n### OPTIONS\n\n#### -c --command\n\nPass a single command, without arguments, to the shell with -c.\n\n#### -E --conflict-exit-code\n\nThe exit status used when the -n option is in use, and the conflicting lock exists, or\nthe -w option is in use, and the timeout is reached. The default value is 1. The number\nhas to be in the range of 0 to 255.\n\n#### -F --no-fork\n\nDo not fork before executing command. Upon execution the flock process is replaced by\ncommand which continues to hold the lock. This option is incompatible with --close as\nthere would otherwise be nothing left to hold the lock.\n\n#### -e -x --exclusive\n\nObtain an exclusive lock, sometimes called a write lock. This is the default.\n\n#### -n --nb --nonblock\n\nFail rather than wait if the lock cannot be immediately acquired. See the -E option for\nthe exit status used.\n\n#### -o --close\n\nClose the file descriptor on which the lock is held before executing command. This is\nuseful if command spawns a child process which should not be holding the lock.\n\n#### -s --shared\n\nObtain a shared lock, sometimes called a read lock.\n\n#### -u --unlock\n\nDrop a lock. This is usually not required, since a lock is automatically dropped when the\nfile is closed. However, it may be required in special cases, for example if the enclosed\ncommand group may have forked a background process which should not be holding the lock.\n\n#### -w --wait --timeout\n\nFail if the lock cannot be acquired within seconds. Decimal fractional values are\nallowed. See the -E option for the exit status used. The zero number of seconds is\ninterpreted as --nonblock.\n\n#### --verbose\n\nReport how long it took to acquire the lock, or why the lock could not be obtained.\n\n#### -V --version\n\nDisplay version information and exit.\n\n#### -h --help\n\nDisplay help text and exit.\n\n### EXIT STATUS\n\nThe command uses <sysexits.h> exit status values for everything, except when using either of\nthe options -n or -w which report a failure to acquire the lock with an exit status given by\nthe -E option, or 1 by default. The exit status given by -E has to be in the range of 0 to\n255.\n\nWhen using the command variant, and executing the child worked, then the exit status is that\nof the child command.\n\n### EXAMPLES\n\nNote that \"shell> \" in examples is a command line prompt.\n\nshell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo; /bin/echo $?\nSet exclusive lock to directory /tmp and the second command will fail.\n\nshell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?\nSet shared lock to directory /tmp and the second command will not fail. Notice that\nattempting to get exclusive lock with second command would fail.\n\nshell> flock -x local-lock-file echo 'a b c'\nGrab the exclusive lock \"local-lock-file\" before running echo with 'a b c'.\n\n(; flock -n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile\nThe form is convenient inside shell scripts. The mode used to open the file doesn’t\nmatter to flock; using > or >> allows the lockfile to be created if it does not already\nexist, however, write permission is required. Using < requires that the file already\nexists but only read permission is required.\n\n[ ${FLOCKER} != $0 ] && exec env FLOCKER=\"$0 flock -en $0 $0 $@ ||\nThis is useful boilerplate code for shell scripts. Put it at the top of the shell\nscript you want to lock and it’ll automatically lock itself on the first run. If the\nenv var $FLOCKER is not set to the shell script that is being run, then execute flock\nand grab an exclusive non-blocking lock (using the script itself as the lock file)\nbefore re-execing itself with the right arguments. It also sets the FLOCKER env var\nto the right value so it doesn’t run again.\n\nshell> exec 4<>/var/lock/mylockfile; shell> flock -n 4\nThis form is convenient for locking a file without spawning a subprocess. The shell opens\nthe lock file for reading and writing as file descriptor 4, then flock is used to lock\nthe descriptor.\n\n### AUTHORS\n\nH. Peter Anvin <hpa@zytor.com>\n\n### COPYRIGHT\n\nCopyright © 2003-2006 H. Peter Anvin. This is free software; see the source for copying\nconditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR\nPURPOSE.\n\n### SEE ALSO\n\nflock(2)\n\n### REPORTING BUGS\n\nFor bug reports, use the issue tracker at https://github.com/karelzak/util-linux/issues.\n\n### AVAILABILITY\n\nThe flock command is part of the util-linux package which can be downloaded from Linux Kernel\nArchive <https://www.kernel.org/pub/linux/utils/util-linux/>.\n\n\n\nutil-linux 2.37.2                            2021-06-02                                     FLOCK(1)\n\n"
        }
    ],
    "structuredContent": {
        "command": "flock",
        "section": "1",
        "mode": "man",
        "summary": "flock - manage locks from shell scripts",
        "synopsis": "flock [options] file|directory command [arguments]\nflock [options] file|directory -c command\nflock [options] number",
        "flags": [
            {
                "flag": "-c",
                "long": "--command",
                "arg": null,
                "description": "Pass a single command, without arguments, to the shell with -c."
            },
            {
                "flag": "-E",
                "long": "--conflict-exit-code",
                "arg": null,
                "description": "The exit status used when the -n option is in use, and the conflicting lock exists, or the -w option is in use, and the timeout is reached. The default value is 1. The number has to be in the range of 0 to 255."
            },
            {
                "flag": "-F",
                "long": "--no-fork",
                "arg": null,
                "description": "Do not fork before executing command. Upon execution the flock process is replaced by command which continues to hold the lock. This option is incompatible with --close as there would otherwise be nothing left to hold the lock."
            },
            {
                "flag": "-x",
                "long": "--exclusive",
                "arg": null,
                "description": "Obtain an exclusive lock, sometimes called a write lock. This is the default."
            },
            {
                "flag": "-n",
                "long": "--nonblock",
                "arg": null,
                "description": "Fail rather than wait if the lock cannot be immediately acquired. See the -E option for the exit status used."
            },
            {
                "flag": "-o",
                "long": "--close",
                "arg": null,
                "description": "Close the file descriptor on which the lock is held before executing command. This is useful if command spawns a child process which should not be holding the lock."
            },
            {
                "flag": "-s",
                "long": "--shared",
                "arg": null,
                "description": "Obtain a shared lock, sometimes called a read lock."
            },
            {
                "flag": "-u",
                "long": "--unlock",
                "arg": null,
                "description": "Drop a lock. This is usually not required, since a lock is automatically dropped when the file is closed. However, it may be required in special cases, for example if the enclosed command group may have forked a background process which should not be holding the lock."
            },
            {
                "flag": "-w",
                "long": "--timeout",
                "arg": null,
                "description": "Fail if the lock cannot be acquired within seconds. Decimal fractional values are allowed. See the -E option for the exit status used. The zero number of seconds is interpreted as --nonblock."
            },
            {
                "flag": "",
                "long": "--verbose",
                "arg": null,
                "description": "Report how long it took to acquire the lock, or why the lock could not be obtained."
            },
            {
                "flag": "-V",
                "long": "--version",
                "arg": null,
                "description": "Display version information and exit."
            },
            {
                "flag": "-h",
                "long": "--help",
                "arg": null,
                "description": "Display help text and exit."
            }
        ],
        "examples": [
            "Note that \"shell> \" in examples is a command line prompt.",
            "shell1> flock /tmp -c cat; shell2> flock -w .007 /tmp -c echo; /bin/echo $?",
            "Set exclusive lock to directory /tmp and the second command will fail.",
            "shell1> flock -s /tmp -c cat; shell2> flock -s -w .007 /tmp -c echo; /bin/echo $?",
            "Set shared lock to directory /tmp and the second command will not fail. Notice that",
            "attempting to get exclusive lock with second command would fail.",
            "shell> flock -x local-lock-file echo 'a b c'",
            "Grab the exclusive lock \"local-lock-file\" before running echo with 'a b c'.",
            "(; flock -n 9 || exit 1; # ... commands executed under lock ...; ) 9>/var/lock/mylockfile",
            "The form is convenient inside shell scripts. The mode used to open the file doesn’t",
            "matter to flock; using > or >> allows the lockfile to be created if it does not already",
            "exist, however, write permission is required. Using < requires that the file already",
            "exists but only read permission is required.",
            "[ ${FLOCKER} != $0 ] && exec env FLOCKER=\"$0 flock -en $0 $0 $@ ||",
            "This is useful boilerplate code for shell scripts. Put it at the top of the shell",
            "script you want to lock and it’ll automatically lock itself on the first run. If the",
            "env var $FLOCKER is not set to the shell script that is being run, then execute flock",
            "and grab an exclusive non-blocking lock (using the script itself as the lock file)",
            "before re-execing itself with the right arguments. It also sets the FLOCKER env var",
            "to the right value so it doesn’t run again.",
            "shell> exec 4<>/var/lock/mylockfile; shell> flock -n 4",
            "This form is convenient for locking a file without spawning a subprocess. The shell opens",
            "the lock file for reading and writing as file descriptor 4, then flock is used to lock",
            "the descriptor."
        ],
        "see_also": [],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 6,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "OPTIONS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "-c --command",
                        "lines": 2,
                        "flag": "-c",
                        "long": "--command"
                    },
                    {
                        "name": "-E --conflict-exit-code",
                        "lines": 4,
                        "flag": "-E",
                        "long": "--conflict-exit-code"
                    },
                    {
                        "name": "-F --no-fork",
                        "lines": 4,
                        "flag": "-F",
                        "long": "--no-fork"
                    },
                    {
                        "name": "-e -x --exclusive",
                        "lines": 2,
                        "flag": "-x",
                        "long": "--exclusive"
                    },
                    {
                        "name": "-n --nb --nonblock",
                        "lines": 3,
                        "flag": "-n",
                        "long": "--nonblock"
                    },
                    {
                        "name": "-o --close",
                        "lines": 3,
                        "flag": "-o",
                        "long": "--close"
                    },
                    {
                        "name": "-s --shared",
                        "lines": 2,
                        "flag": "-s",
                        "long": "--shared"
                    },
                    {
                        "name": "-u --unlock",
                        "lines": 4,
                        "flag": "-u",
                        "long": "--unlock"
                    },
                    {
                        "name": "-w --wait --timeout",
                        "lines": 4,
                        "flag": "-w",
                        "long": "--timeout"
                    },
                    {
                        "name": "--verbose",
                        "lines": 2,
                        "long": "--verbose"
                    },
                    {
                        "name": "-V --version",
                        "lines": 2,
                        "flag": "-V",
                        "long": "--version"
                    },
                    {
                        "name": "-h --help",
                        "lines": 2,
                        "flag": "-h",
                        "long": "--help"
                    }
                ]
            },
            {
                "name": "EXIT STATUS",
                "lines": 8,
                "subsections": []
            },
            {
                "name": "EXAMPLES",
                "lines": 31,
                "subsections": []
            },
            {
                "name": "AUTHORS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "COPYRIGHT",
                "lines": 4,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "REPORTING BUGS",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "AVAILABILITY",
                "lines": 6,
                "subsections": []
            }
        ]
    }
}