foreversd / forever

A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
http://github.com/foreverjs/forever
MIT License
13.87k stars 946 forks source link

Forever changes current working directory when script path is absolute #270

Open boutell opened 12 years ago

boutell commented 12 years ago

If I use:

forever start /opt/stagecoach/apps/example/deployments/2012-04-23-09-46-19/server.js

The current working directory is changed to / by forever before the script is run (verified with process.cwd()).

If I use:

forever start server.js

I do not see this problem, the current working directory is left alone.

I am not sure if the --sourceDir option is meant to help with this, but it does not appear to.

Thanks for an excellent process babysitter!

bryankaplan commented 12 years ago

I can reproduce this.

hrishikeshio commented 12 years ago

I am having trouble with --sourceDir also... It does not seem to work..

indexzero commented 11 years ago

This should be fixed. There are tests that confirm this works.

tommedema commented 10 years ago

Why was this closed?

joelweinberger commented 10 years ago

Just to add my 2 cents, I just started experiencing this problem. Perhaps it has regressed?

julianduque commented 10 years ago

I'll investigate, will reopen

jossscholten commented 10 years ago

Also seeing this issue. When forever is run from a script that references an absolute path, process.cwd() returns the script directory instead of the local directory.

luiselizondo commented 10 years ago

An easy way to reproduce the problem is to use the express-config module (https://github.com/blahed/express-config) and then run forever from any directory except the one with the application

christiangenco commented 10 years ago

+1

osher commented 10 years ago

+1

currenlty we patch it with performing cd always before running forever

pkopac commented 10 years ago

0.11.0 worked well, but 0.11.1 broke it again; solved by fixing version on 0.11.0

tommedema commented 10 years ago

is forever still maintained? any alternatives?

jrowny commented 10 years ago

+1

simonmorley commented 10 years ago

+1

hongkongkiwi commented 10 years ago

I can confirm this is still a problem in the latest version.

caomu commented 9 years ago

+1

caomu commented 9 years ago

C:\Users\guorong841>C:\Users\guorong841\AppData\Roaming\npm\forever.cmd --minUpt ime 1000ms --spinSleepTime 1000ms D:\workspace\jee\activity\server.js error: Cannot start forever error: script C:\Users\guorong841\D:\workspace\jee\activity\server.js does not exist.

jthomerson commented 9 years ago

We did not experience this issue in v0.11.1, but today when we started up some new servers and they started using v0.12.0, we had this issue. We had an absolute path to our script and it changed the CWD to /

mykwillis commented 9 years ago

I'm not sure of the history, but the way the code is now written the CWD for the spawned process will always be set to '/' if the specified script path begins with '/'. For example:

$ forever start /path/to/my/script # CWD will be '/'

This is due to the logic in forever\cli.js, line 237: options.sourceDir = options.sourceDir || (file && file[0] !== '/' ? process.cwd() : '/'); if (options.sourceDir) { options.spawnWith = {cwd: options.sourceDir}; }

Looking at this code, you can see that if --sourceDir is specified, it will be used as the CWD:

$ forever --sourceDir /path/to/my start script # CWD will be '/path/to/my'

...but that seems to be a bug (or at least an undocumented side-effect), given that the documented effect of --sourceDir is to set "The source directory for which SCRIPT is relative to". It's not at all obvious that this should actually affect the CWD for the process running the script.

Because the tryStart() method builds the full script path by joining the --sourceDir with the given script name (as per the documentation), you can't use an absolute path for your script. And so, there is no way to both specify the CWD for a script and use absolute paths.

The workaround with the current code base is to (a) set --sourceDir to the desired CWD, and (b) specify SCRIPT as a path relative to --sourceDir.

$ #desired CWD: /home/myuser $ #path to script: /usr/local/bin/myscript $ forever --sourceDir /home/myuser start ../../usr/local/bin/myscript

Personally, I think the correct approach is to treat CWD as completely independent of --sourceDir, and having an option like --cwd that can be used to specify this explicitly is probably the cleanest solution.

felipe-mulhbaier commented 5 years ago

This solved my problem, maybe it will be useful for someone:

forever start --sourceDir /var/www/html/sitedir scriptname.js