I'm wondering if PM2 can run a shell script (bash) before stopping/killing the process? I know it can start a process running a shell script and I want to handle some things before stopping/killing the process down.
Regards Håkan
I have tried to get this to work, but since I'm new to PM2 I can't get it to work.
Can you provide a total example, please?
pm2 start sleep sleep.sh
pm2 list
pm2 trigger sleep pre-stop "./test.sh"
Not any process has received a command (offline or unexistent)
sleep.sh just sleeps for 1000 seconds.
test.sh just echoes Hello.
So, my pm2 trigger command gives an error. Would appreciate an example very much. @neon mike
Regards Håkan
Yes, you can configure PM2 to run a bash or shell script before stopping or killing a process using the pre-stop event. This event is triggered before a process is stopped or killed, allowing you to execute any necessary cleanup tasks beforehand. To configure the pre-stop event, you can use the following command:
pm2 trigger APP_NAME pre-stop "./my-script.sh"
Replace APP_NAME with the name of your PM2 application and my-script.sh with the path to your bash or shell script. When the pre-stop event is triggered, PM2 will execute the specified script before stopping or killing the process. Additionally, you can use the pre-restart event to run a script before restarting a process and the pre-delete event to run a script before deleting a process from PM2.