@sounisi5011/check-pid-file

Use the PID file to check which processes have already started and prevent multiple executions

Usage no npm install needed!

<script type="module">
  import sounisi5011CheckPidFile from 'https://cdn.skypack.dev/@sounisi5011/check-pid-file';
</script>

README

@sounisi5011/check-pid-file

Go to the latest release page on npm Supported Node.js version: ^12.17.x || 14.x || 15.x || 16.x || 17.x Tested with Jest Commitizen friendly Minified Bundle Size Details Install Size Details Dependencies Status Build Status Maintainability Status

Use the PID file to check which processes have already started and prevent multiple executions.

Attention

This package DOES NOT use the file locking provided by the file system. Because Node.js does not provide an API for file locking.

Features

  • Automatically creates PID files and deletes them when the program is terminated.

  • Overwrites a PID file specified with a non-existent process id.

Installation

npm install @sounisi5011/check-pid-file
yarn add @sounisi5011/check-pid-file
pnpm add @sounisi5011/check-pid-file

Usage

const { isProcessExist } = require('@sounisi5011/check-pid-file');

// ----- //

(async () => {
  try {
    // Read the PID file and check if the specified process exists.
    // If it does not exist, recreate the PID file.
    // It also automatically deletes the PID file when the program is terminated.
    if (await isProcessExist('/path/to/pid-file.pid')) {
      // The process exists with the number specified in the PID file.
      // In this case, the program SHOULD NOT continue processing because there is already another process running.
    } else {
      // The following cases are applicable:
      // + The process does not exist with the number specified in the PID file.
      // + The same number as the current process id is specified in the PID file.
      // + The structure of the PID file is unknown.
      // In this case, the program SHOULD continue processing because no other processes are running.
    }
  } catch (error) {
    // Exceptions may be thrown due to file system errors, etc.
  }
})();

References