PHPackages                             naowas/console-mutex - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. naowas/console-mutex

ActiveLibrary[CLI &amp; Console](/categories/cli)

naowas/console-mutex
====================

Mutex for Laravel Console Commands.

11.0.0(2y ago)099↓100%MITPHPPHP ^8.2

Since May 26Pushed 2y agoCompare

[ Source](https://github.com/naowas/laravel-console-mutex)[ Packagist](https://packagist.org/packages/naowas/console-mutex)[ RSS](/packages/naowas-console-mutex/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (9)Versions (98)Used By (0)

[![Mutex for Laravel Console Commands](art/1380x575-optimized.jpg)](art/1380x575-optimized.jpg)

Laravel Console Mutex
=====================

[](#laravel-console-mutex)

[![Buy me a coffee](https://user-images.githubusercontent.com/1286821/181085373-12eee197-187a-4438-90fe-571ac6d68900.png)](https://buymeacoffee.com/dmitry.ivanov)

[![StyleCI](https://camo.githubusercontent.com/c7c85737552d8e0408ffec500862ca7c3652bc6cb4b4c864d6cf0e9d0027dc13/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f35393537303035322f736869656c643f6272616e63683d6d6173746572267374796c653d666c6174)](https://github.styleci.io/repos/59570052?branch=master)[![Build Status](https://camo.githubusercontent.com/4448b26211beedd1ffc55f12e34ed791ce669c6679e1bed1b498af13a4db66bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f646d697472792d6976616e6f762f6c61726176656c2d636f6e736f6c652d6d757465782f74657374732f6d6173746572)](https://github.com/dmitry-ivanov/laravel-console-mutex/actions?query=workflow%3Atests+branch%3Amaster)[![Coverage Status](https://camo.githubusercontent.com/fe16c52c7946c5800bb5f99753d62420459848fcde108eb67955dcaa47aeb4e3/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f646d697472792d6976616e6f762f6c61726176656c2d636f6e736f6c652d6d757465782f6d6173746572)](https://app.codecov.io/gh/dmitry-ivanov/laravel-console-mutex/branch/master)

[![Packagist Version](https://camo.githubusercontent.com/cfde3757391239b12a1a4261b6ff0b9a1351dbbfbf93f6a843879ea7c9b1a28f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696c6c756d696e617465642f636f6e736f6c652d6d75746578)](https://camo.githubusercontent.com/cfde3757391239b12a1a4261b6ff0b9a1351dbbfbf93f6a843879ea7c9b1a28f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f696c6c756d696e617465642f636f6e736f6c652d6d75746578)[![Packagist Stars](https://camo.githubusercontent.com/3a5664878e134e1bc9b6f5a08a40356104d98e5e999bad0c785c209eee4dd6a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f696c6c756d696e617465642f636f6e736f6c652d6d75746578)](https://camo.githubusercontent.com/3a5664878e134e1bc9b6f5a08a40356104d98e5e999bad0c785c209eee4dd6a2/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f73746172732f696c6c756d696e617465642f636f6e736f6c652d6d75746578)[![Packagist Downloads](https://camo.githubusercontent.com/7654ba5164e20cdf7ee1ec786b0a8c0a48c44ca393d48f9faa066b7fd082f717/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696c6c756d696e617465642f636f6e736f6c652d6d75746578)](https://camo.githubusercontent.com/7654ba5164e20cdf7ee1ec786b0a8c0a48c44ca393d48f9faa066b7fd082f717/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f696c6c756d696e617465642f636f6e736f6c652d6d75746578)[![Packagist License](https://camo.githubusercontent.com/09534dca380120b9161e6c0ab75a382fdd3850474e90e681bcec4ba53bc545f5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f696c6c756d696e617465642f636f6e736f6c652d6d75746578)](https://camo.githubusercontent.com/09534dca380120b9161e6c0ab75a382fdd3850474e90e681bcec4ba53bc545f5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f696c6c756d696e617465642f636f6e736f6c652d6d75746578)

Mutex for Laravel Console Commands.

LaravelConsole Mutex9.x[9.x](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/9.x)8.x[8.x](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/8.x)7.x[7.x](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/7.x)6.x[6.x](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/6.x)5.8.\*[5.8.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.8)5.7.\*[5.7.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.7)5.6.\*[5.6.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.6)5.5.\*[5.5.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.5)5.4.\*[5.4.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.4)5.3.\*[5.3.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.3)5.2.\*[5.2.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.2)5.1.\*[5.1.\*](https://github.com/dmitry-ivanov/laravel-console-mutex/tree/5.1)[![Laravel Console Mutex - Demo](doc/img/demo.gif)](doc/img/demo.gif)

Table of contents
-----------------

[](#table-of-contents)

- [Usage](#usage)
- [Strategies](#strategies)
- [Advanced](#advanced)
    - [Set custom timeout](#set-custom-timeout)
    - [Handle multiple commands](#handle-multiple-commands)
    - [Set custom storage folder](#set-custom-storage-folder)
- [Troubleshooting](#troubleshooting)
    - [Trait included, but nothing happens?](#trait-included-but-nothing-happens)
    - [Several traits conflict?](#several-traits-conflict)
- [Sponsors](#sponsors)
- [License](#license)

Usage
-----

[](#usage)

1. Install the package via Composer:

    ```
    composer require illuminated/console-mutex
    ```
2. Use `Illuminated\Console\WithoutOverlapping` trait:

    ```
    use Illuminated\Console\WithoutOverlapping;

    class ExampleCommand extends Command
    {
        use WithoutOverlapping;

        // ...
    }
    ```

Strategies
----------

[](#strategies)

Mutex can prevent overlapping by using various strategies:

- `file` (default)
- `mysql`
- `redis`
- `memcached`

The default `file` strategy is acceptable for small applications, which are deployed on a single server. If your application is more complex and deployed on several nodes, you should consider using another mutex strategy.

You can change strategy by using the `$mutexStrategy` field:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected string $mutexStrategy = 'mysql';

    // ...
}
```

Or by using the `setMutexStrategy()` method:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function __construct()
    {
        parent::__construct();

        $this->setMutexStrategy('mysql');
    }

    // ...
}
```

Advanced
--------

[](#advanced)

### Set custom timeout

[](#set-custom-timeout)

By default, if mutex sees that the command is already running, it will immediately quit. You can change that behavior by setting a timeout in which mutex can wait for another running command to finish its execution.

You can set the timeout by specifying the `$mutexTimeout` field:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    // In milliseconds
    protected ?int $mutexTimeout = 3000;

    // ...
}
```

Or by using the `setMutexTimeout()` method:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function __construct()
    {
        parent::__construct();

        // In milliseconds
        $this->setMutexTimeout(3000);
    }

    // ...
}
```

Here's how the `$mutexTimeout` field is treated:

- `0` - no waiting (default);
- `{int}` - wait for the given number of milliseconds;
- `null` - wait for the running command to finish its execution;

### Handle multiple commands

[](#handle-multiple-commands)

Sometimes it might be useful to have a shared mutex for multiple commands. You can easily achieve that by setting the same mutex name for all of those commands.

You should use the `getMutexName()` method for that:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function getMutexName()
    {
        return 'shared-for-command1-and-command2';
    }

    // ...
}
```

### Set custom storage folder

[](#set-custom-storage-folder)

If you're using the `file` strategy, mutex files would be stored in the `storage/app` folder.

You can change that by overriding the `getMutexFileStorage()` method:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    public function getMutexFileStorage()
    {
        return storage_path('my/custom/path');
    }

    // ...
}
```

Troubleshooting
---------------

[](#troubleshooting)

### Trait included, but nothing happens?

[](#trait-included-but-nothing-happens)

`WithoutOverlapping` trait overrides the `initialize()` method:

```
trait WithoutOverlapping
{
    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        $this->initializeMutex();

        parent::initialize($input, $output);
    }

    // ...
}
```

If your command overrides the `initialize()` method too, you have to call the `initializeMutex()` method by yourself:

```
class ExampleCommand extends Command
{
    use WithoutOverlapping;

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        // You have to call it first
        $this->initializeMutex();

        // Then goes your custom code
        $this->foo = $this->argument('foo');
        $this->bar = $this->argument('bar');
        $this->baz = $this->argument('baz');
    }

    // ...
}
```

### Several traits conflict?

[](#several-traits-conflict)

If you're using another `illuminated/console-%` package, you'll get the "traits conflict" error.

For example, if you're building a [loggable command](https://github.com/dmitry-ivanov/laravel-console-logger), which doesn't allow overlapping:

```
class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

    // ...
}
```

You'll get the traits conflict, because both of those traits are overriding the `initialize()` method:

> If two traits insert a method with the same name, a fatal error is produced, if the conflict is not explicitly resolved.

To fix that - override the `initialize()` method and resolve the conflict:

```
class ExampleCommand extends Command
{
    use Loggable;
    use WithoutOverlapping;

    protected function initialize(InputInterface $input, OutputInterface $output)
    {
        // Initialize conflicting traits
        $this->initializeMutex();
        $this->initializeLogging();
    }

    // ...
}
```

Sponsors
--------

[](#sponsors)

[![Laravel Idea](art/sponsor-laravel-idea.png)](https://laravel-idea.com)
[![Material Theme UI Plugin](art/sponsor-material-theme.png)](https://material-theme.com)

License
-------

[](#license)

Laravel Console Mutex is open-sourced software licensed under the [MIT license](LICENSE.md).

[![Support on Patreon](https://user-images.githubusercontent.com/1286821/43086829-ff7c006e-8ea6-11e8-8b03-ecf97ca95b2e.png)](https://patreon.com/dmitryivanov) [![Buy me a coffee](https://user-images.githubusercontent.com/1286821/181085373-12eee197-187a-4438-90fe-571ac6d68900.png)](https://buymeacoffee.com/dmitry.ivanov)

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity92

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 97% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~30 days

Recently: every ~36 days

Total

94

Last Release

788d ago

Major Versions

6.x-dev → 7.x-dev2021-01-03

7.x-dev → 8.x-dev2021-03-04

8.2.0 → 10.02023-10-20

8.2.1 → 10.1.02023-10-20

10.1.0 → 11.0.02024-03-13

PHP version history (13 changes)0.1.1PHP &gt;=7.0.0

0.1.2PHP &gt;=5.4.0

1.0.2PHP &gt;=5.5.9

1.1.6PHP &gt;=5.6.4

5.5.0PHP &gt;=7.0

5.6.0PHP ^7.1.3

6.0.0PHP ^7.2

7.0.0PHP ^7.2.5

8.0.0PHP ^7.3

6.2.0PHP ^7.2.5|^8.0

8.1.0PHP ^7.3|^8.0

8.2.0PHP ^8.1

11.0.0PHP ^8.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/107a3d814e216961097eaf38b248a257b46bca5f352cfd835f7ab899da28acb1?d=identicon)[naowas](/maintainers/naowas)

---

Top Contributors

[![dmitry-ivanov](https://avatars.githubusercontent.com/u/1286821?v=4)](https://github.com/dmitry-ivanov "dmitry-ivanov (391 commits)")[![naowas](https://avatars.githubusercontent.com/u/17976272?v=4)](https://github.com/naowas "naowas (5 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (2 commits)")[![dmirogin](https://avatars.githubusercontent.com/u/5470439?v=4)](https://github.com/dmirogin "dmirogin (2 commits)")[![mihan007](https://avatars.githubusercontent.com/u/939122?v=4)](https://github.com/mihan007 "mihan007 (1 commits)")[![rafacouto](https://avatars.githubusercontent.com/u/969061?v=4)](https://github.com/rafacouto "rafacouto (1 commits)")[![SBLAdam](https://avatars.githubusercontent.com/u/81582882?v=4)](https://github.com/SBLAdam "SBLAdam (1 commits)")

---

Tags

consolelaravelsemaphoremutexcommandlockermutex-lockno-overlapping

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/naowas-console-mutex/health.svg)

```
[![Health](https://phpackages.com/badges/naowas-console-mutex/health.svg)](https://phpackages.com/packages/naowas-console-mutex)
```

###  Alternatives

[illuminated/console-mutex

Mutex for Laravel Console Commands.

146938.2k1](/packages/illuminated-console-mutex)[nunomaduro/laravel-console-menu

Laravel Console Menu is an output method for your Laravel/Laravel Zero commands.

815412.0k48](/packages/nunomaduro-laravel-console-menu)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
