pm2 start in a symbolic linked directory should not follow symlink

Created on 2 Oct 2015  ยท  23Comments  ยท  Source: Unitech/pm2

starting an app in a symblic linked directory
pm2 info reports path as the resolved link

that breaks workflows where deployment changes the target of the symlink.
As pm2 restart uses the "old" target

In progress

Most helpful comment

I wrote about symlinks here

All 23 comments

:+1: have the same issue

I tried different way of linking and relinking folders, I cannot reproduce this issue,
Could you give me a scenario to reproduce this,
Thank you

on linux / ubuntu, pm2 version 0.15.7

suppose binary in daemondir

ln -s daemondir linkdeamon
cd linkdaemon
pm2 start app.js

pm2 info shows
path: daemondir/app.js

and not as expected
linkdaemon/app.js

Thanks, why not starting your app like that:

$ pm2 start linkdeamon/app.js

So the folder does not get resolved

Your're right, like this it works.

Why I do it the way I do it?

I think because usually with node you are inside a directory while developing
doing stuff with npm install, grunt gulp, etc, so it felt like the normal way to do, and I think more people will therefore experience this issue.

with my deployment script I actually have done npm install an build tasks before and made an archive.
but after deploying ( extracting ) i still do something like

pushd $linkeddaemondir
npm rebuild
pm2 restart
popd

@Unitech pm2 restart app-name after switching symlinks where app-name is located is the problem case

i can't descibe why, but for me I have to start my processes in the correct directory. So the workaround above doesn't work.

+1 -- doesn't work when "npm start/stop/restart" are used to kick-off pm2 in the new working directory after a deploy

Having the same issue, for example with a service like deploybot where releases are kept on disk and a current symlink changes the actual version.

Yep, we just hit same problem. We do deploys by switching symlinks to diff folders for fast rollbacks (capistrano style), but our symlink location is resolved on pm2 start and nothing ever gets updated. Please advice!

For everyone reading this. I managed to solve it by actually adding some extra symlinks. Here's our app.json for PM2:

{
  "apps": [
    {
      "name": "my-app",
      "script": "index.js",
      "args": [],
      "watch": true,
      "node_args": "",
      "merge_logs": true,
      "cwd": "/usr/local/share/my-app/current",
      "env": {
        "NODE_ENV": "development"
      }
    }
  ]
}

where the /usr/local/share/my-app/current is:

[ec2-user@ip-blalblabla~]$ ls -ltra /usr/local/share/flow-api/
total 20
drwxr-xr-x 9 root     root     4096 Dec 10 09:24 ..
drwxrwxr-x 2 ec2-user ec2-user 4096 Dec 10 09:58 shared
drwxrwxr-x 6 ec2-user ec2-user 4096 Jan  4 13:25 deploy-cache
lrwxrwxrwx 1 ec2-user ec2-user   45 Jan  4 13:26 current -> /usr/local/share/my-app/releases/1451913926
drwxr-xr-x 5 ec2-user ec2-user 4096 Jan  4 13:26 .
drwxrwxr-x 5 ec2-user ec2-user 4096 Jan  4 13:26 releases

@tnolet Something is wrong in your explanation? Are you sure you pasted the correct paths?

@hguillermo: where it says 'flow-api' is should read 'flow-api'. This is due to my sad and failed attempt ot cover up our app name :smile:

It didn't work for us. At the end our issue is related to this: https://github.com/Unitech/pm2/issues/1623

Hopefully that gets fixed soon.

+1.
can't pm2 use process.env.PWD instead of process.cwd()?

Having a similar problem with pm2 1.1.1.

When switching the version underneath pm2 and using startOrGracefulReload, pm2 processes are erroring (which renders the process quite usless) due to the lack of CWD. Anyone knows how this can be combated?

@teu: @gansbrest suggested before that starting PM2 with upstart or similar will help. I am sure he can explain the logic behind it.

@teu Basically you need to make sure you run pm2 outside of your build dir. So if you have structure like this:

current --> build-1
build-1
build-2
build-3

You need to cd outside of the build directory and run pm2 from there. That way your directory will never be deleted. Hope that helps.

Thanks @gansbrest already figured it out by trial and (a lot of) error. Good catch, should go in the pm2 docs.

I wrote about symlinks here

@soyuka True, missed it, great resource, thanks!

@gansbrest it works Thanks!

EDIT: Even after pm2 delete and recreating the app with the correct symlink, reboot is still switching over to catfood-0853a10 and that is even more mysterious to me.

Hello, something about the way I am doing this is simply not working. Each time I reboot my server, the CWD and absolute path to the script are replaced with the target of the symlink I was using at some point in the past.

I read through the instructions and I can't tell what I am doing incorrectly. Can someone point me in the right direction? Thanks!

$ ls -l
total 132
lrwxrwxrwx 1 misterikkit misterikkit     15 Dec 23 20:07 catfood -> catfood-72d1d3c
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 07:04 catfood-0853a10
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 08:06 catfood-30b614e
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 19:50 catfood-461515d
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 19:47 catfood-5199879-dirty
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 20:07 catfood-72d1d3c
drwxr-xr-x 4 misterikkit misterikkit   4096 Dec 23 08:53 catfood-7d48e66
-rw-r--r-- 1 misterikkit misterikkit 100110 Dec 23 20:21 catfood.log

$ pm2 start catfood/run.sh --name catfood-backend --log $HOME/catfood.log

$ pm2 info catfood-backend
 Describing process with id 0 - name catfood-backend 
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ status            โ”‚ online                                                โ”‚
โ”‚ name              โ”‚ catfood-backend                                       โ”‚
โ”‚ namespace         โ”‚ default                                               โ”‚
โ”‚ version           โ”‚ 1.0.0                                                 โ”‚
โ”‚ restarts          โ”‚ 0                                                     โ”‚
โ”‚ uptime            โ”‚ 9s                                                    โ”‚
โ”‚ entire log path   โ”‚ /home/misterikkit/catfood.log                         โ”‚
โ”‚ script path       โ”‚ /home/misterikkit/catfood/run.sh                      โ”‚
โ”‚ script args       โ”‚ N/A                                                   โ”‚
โ”‚ error log path    โ”‚ /home/misterikkit/.pm2/logs/catfood-backend-error.log โ”‚
โ”‚ out log path      โ”‚ /home/misterikkit/.pm2/logs/catfood-backend-out.log   โ”‚
โ”‚ pid path          โ”‚ /home/misterikkit/.pm2/pids/catfood-backend-0.pid     โ”‚
โ”‚ interpreter       โ”‚ bash                                                  โ”‚
โ”‚ interpreter args  โ”‚ N/A                                                   โ”‚
โ”‚ script id         โ”‚ 0                                                     โ”‚
โ”‚ exec cwd          โ”‚ /home/misterikkit                                     โ”‚
โ”‚ exec mode         โ”‚ fork_mode                                             โ”‚
โ”‚ node.js version   โ”‚ N/A                                                   โ”‚
โ”‚ node env          โ”‚ N/A                                                   โ”‚
โ”‚ watch & reload    โ”‚ โœ˜                                                     โ”‚
โ”‚ unstable restarts โ”‚ 0                                                     โ”‚
โ”‚ created at        โ”‚ 2020-12-23T20:23:11.634Z                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

# After reboot it becomes...

$ pm2 info catfood-backend
 Describing process with id 0 - name catfood-backend 
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ status            โ”‚ online                                                โ”‚
โ”‚ name              โ”‚ catfood-backend                                       โ”‚
โ”‚ namespace         โ”‚ default                                               โ”‚
โ”‚ version           โ”‚ 1.0.0                                                 โ”‚
โ”‚ restarts          โ”‚ 0                                                     โ”‚
โ”‚ uptime            โ”‚ 12s                                                   โ”‚
โ”‚ script path       โ”‚ /home/misterikkit/catfood-0853a10/index.js            โ”‚
โ”‚ script args       โ”‚ N/A                                                   โ”‚
โ”‚ error log path    โ”‚ /home/misterikkit/.pm2/logs/catfood-backend-error.log โ”‚
โ”‚ out log path      โ”‚ /home/misterikkit/.pm2/logs/catfood-backend-out.log   โ”‚
โ”‚ pid path          โ”‚ /home/misterikkit/.pm2/pids/catfood-backend-0.pid     โ”‚
โ”‚ interpreter       โ”‚ node                                                  โ”‚
โ”‚ interpreter args  โ”‚ N/A                                                   โ”‚
โ”‚ script id         โ”‚ 0                                                     โ”‚
โ”‚ exec cwd          โ”‚ /home/misterikkit/catfood-0853a10                     โ”‚
โ”‚ exec mode         โ”‚ fork_mode                                             โ”‚
โ”‚ node.js version   โ”‚ 15.5.0                                                โ”‚
โ”‚ node env          โ”‚ N/A                                                   โ”‚
โ”‚ watch & reload    โ”‚ โœ”                                                     โ”‚
โ”‚ unstable restarts โ”‚ 0                                                     โ”‚
โ”‚ created at        โ”‚ 2020-12-23T07:33:20.840Z                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alexpts picture alexpts  ยท  3Comments

chaos-git picture chaos-git  ยท  3Comments

ghost picture ghost  ยท  3Comments

morfies picture morfies  ยท  3Comments

liujb picture liujb  ยท  3Comments