Nitro preset, not stopping existing instance on deploy

I have an app running on Nuxt 3/Nitro, it is not SSR but does have server and public builds, the build command is:

NITRO_PRESET=cleavr pnpm start --fail-on-error

I’ve added an additional hook before this that runs pnpm build.

Now, everything works when deploy first time, but every time after that after deploy I have to go into ssh run ps aux | grep “nuxt” and kill the processes created by previous deploy and rerun the build. I’ve tried running a deployment hook that searches with ps aux the same way and tries to kill processes, but can’t really get it to execute, just for context this is what I was trying to add in hooks:

cd {{ releasePath }}
PID=$(ps auw | grep 'nuxt' | awk '{print $2}')
if [ -n "$PID" ]; then
  echo "Killing (PID: $PID)..."
  kill $PID
  while [ -n "$(ps -p $PID -o pid= 2>/dev/null)" ]; do
    sleep 1
  done
fi

Feels like since there’s Nitro preset in existence this would be part of the deploy or at least offered as a premade hook?

Open to ideas on how to the existing instance, rn I’m searching for “nuxt” but there are other apps on this same ec2 that use nuxt so I’m probably going to add env to start command with NUXT_APP_NAME or something, that being said I still can’t get the bash hook to search and kill processes (it finds the process but I get the message that it couldn’t kill it).

Hello @dino ,

First of all, welcome to the Cleavr forum.

Sorry for the inconvenience caused, Dino.

As for your custom hook failing, kill $PID requires root privilege and we don’t run deployment hooks as root user except Clean old deployments hook.

We’ll look into the issue in more detail and get back to you.

Hey @rabin

Was not aware of how hooks are deployed, but makes sense. I guess one option would be to add a disableable hook like “clean old deploys” that kills processes created by previous deploy of the app?

Thanks for your reply!

1 Like

Using a process manager: Instead of manually searching for and killing Nuxt processes, you can use a process manager such as PM2. PM2 allows you to easily manage your running processes and automatically restart them when needed. You can configure PM2 to start your Nuxt application and automatically restart it each time it is deployed.
Using a unique process ID: You have already considered the idea of using an environment variable to define a unique Nuxt process name. This can be a good solution to avoid conflict with other Nuxt processes on your EC2 server. Make sure you configure the NUXT_APP_NAME environment variable correctly so that it is different for each instance of your Nuxt application.
Using a stop script: Create a separate stop script that looks for and kills Nuxt processes. You can use the killall command to kill processes by name. For example, killall -q -w -s SIGTERM node will search for and kill all Node.js processes. You can add this script to your deployment hooks to run automatically after each deployment.

Hello @BucklandMetcalfe,

Welcome to Cleavr Forum.

We are actually using PM2 as a process manager. The issue @dino was facing may be caused by some other action.

Hello @dino,

We have been monitoring your deployments and they appear to be functioning correctly.

Have you been successful in resolving the issue? If so, could you please provide us with the details on how you resolved it?

Alternatively, if the issue remains, please provide us with the name of the deployment workflow that is causing the issue and we will investigate the matter further.