Unitech / Unitech/pm2

Run a shell script before killing the process

Open
#5,576 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
43.3k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

What's going wrong?

This is more a question than an issue.
I start an app with PM2 by running a shell script.
When I stop the process, I need to run a shell script before PM2 kills the first process.
How can I do that?
The reason for running a stop script is that in the start script we insert a row in the database, and we must remove the row in the database before stopping the process.

If I could get an example how to do this, I'll be incredibly happy.
If not, I would like to make this as a request that it should be possible to run something before killing the process.

How could we reproduce this issue?

Start an app a by running a shell script (bash).
Insert a row in the database in a table.

Stop the app by running a shell script.
Remove the row from the database before the process is killed.

Supporting information

--- PM2 report ----------------------------------------------------------------
Date                 : Mon Mar 27 2023 10:02:15 GMT+0200 (Central European Summer Time)
===============================================================================
--- Daemon -------------------------------------------------
pm2d version         : 5.3.0
node version         : 16.19.1
node path            : /usr/bin/pm2
argv                 : /usr/bin/node,/usr/lib/node_modules/pm2/lib/Daemon.js
argv0                : node
user                 : haar
uid                  : 1000
gid                  : 1000
uptime               : 0min
===============================================================================
--- CLI ----------------------------------------------------
local pm2            : 5.3.0
node version         : 16.19.1
node path            : /usr/bin/pm2
argv                 : /usr/bin/node,/usr/bin/pm2,report
argv0                : node
user                 : haar
uid                  : 1000
gid                  : 1000
===============================================================================
--- System info --------------------------------------------
arch                 : x64
platform             : linux
type                 : Linux
cpus                 : 12th Gen Intel(R) Core(TM) i7-1255U
cpus nb              : 2
freemem              : 631934976
totalmem             : 4111659008
home                 : /home/haar
===============================================================================
--- PM2 list -----------------------------------------------
┌────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
└────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
===============================================================================
--- Daemon logs --------------------------------------------
/home/haar/.pm2/pm2.log last 20 lines:
PM2        | 2023-03-24T16:37:59: PM2 log: App [webserver:3] exited with code [0] via signal [SIGINT]
PM2        | 2023-03-24T16:37:59: PM2 log: App [linkExpress:2] exited with code [1] via signal [SIGINT]
PM2        | 2023-03-24T16:37:59: PM2 log: pid=25907 msg=process killed
PM2        | 2023-03-24T16:37:59: PM2 log: pid=25909 msg=process killed
PM2        | 2023-03-27T10:02:15: PM2 log: ===============================================================================
PM2        | 2023-03-27T10:02:15: PM2 log: --- New PM2 Daemon started ----------------------------------------------------
PM2        | 2023-03-27T10:02:15: PM2 log: Time                 : Mon Mar 27 2023 10:02:15 GMT+0200 (Central European Summer Time)
PM2        | 2023-03-27T10:02:15: PM2 log: PM2 version          : 5.3.0
PM2        | 2023-03-27T10:02:15: PM2 log: Node.js version      : 16.19.1
PM2        | 2023-03-27T10:02:15: PM2 log: Current arch         : x64
PM2        | 2023-03-27T10:02:15: PM2 log: PM2 home             : /home/haar/.pm2
PM2        | 2023-03-27T10:02:15: PM2 log: PM2 PID file         : /home/haar/.pm2/pm2.pid
PM2        | 2023-03-27T10:02:15: PM2 log: RPC socket file      : /home/haar/.pm2/rpc.sock
PM2        | 2023-03-27T10:02:15: PM2 log: BUS socket file      : /home/haar/.pm2/pub.sock
PM2        | 2023-03-27T10:02:15: PM2 log: Application log path : /home/haar/.pm2/logs
PM2        | 2023-03-27T10:02:15: PM2 log: Worker Interval      : 30000
PM2        | 2023-03-27T10:02:15: PM2 log: Process dump file    : /home/haar/.pm2/dump.pm2
PM2        | 2023-03-27T10:02:15: PM2 log: Concurrent actions   : 2
PM2        | 2023-03-27T10:02:15: PM2 log: SIGTERM timeout      : 1600
PM2        | 2023-03-27T10:02:15: PM2 log: ===============================================================================

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source file, test, or entry point is named. Start by tracing how PM2 handles stop signals and process termination, using the reported SIGINT and SIGTERM timeout behavior as context. Define the expected pre-stop hook behavior and verify that database cleanup can complete before the process is killed.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, shell
Domain
cli, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.