jon-hall / pm2-windows-service

Easily install (and uninstall) PM2 as a Windows service
231 stars 66 forks source link

How do I launch scripts with custom env variables ? #22

Closed RodolpheGohard closed 2 years ago

RodolpheGohard commented 7 years ago

Here is how I'd do it with only pm2:

# Inject what is declared in env_production
$ pm2 start process.json --env production

i'm looking for a way to tell the service to start pm2 with --env production. How can I do this ?

jon-hall commented 7 years ago

This currently isn't possible as far as I can tell from checking what pm2 does with that extra flag, since pm2-windows-service is currently very naive in how it processes the startup scripts - it just string splits and calls pm2.start with each substring. This feature would require the ability to pass an object with the env property set.

I'll need to figure out a decent way of allowing richer configuration in the PM2_SERVICE_SCRIPTS environment variable (the simplest would just be allowing inline JSON but that might be pretty unpleasant to configure).

I'll start looking into this and will update here as soon as I have something - thanks for the feedback and raising my attention on this feature as it'll hopefully bring support for collecting any additional configuration for the service scripts, which was a big oversight on my behalf.

yarivat commented 7 years ago

Hi, I have a configFactory file in my project, which checks the process.env , we added a system variable (ENV) to windows which have the current env (prod/qa/dev). when running under pm2-windows-service process.env is '[object,object]' and idea how to resolve?

jon-hall commented 7 years ago

I'm still looking into the best way to solve the problem presented in this issue (passing commandline options to pm2 when run as a service), @yarivat - I don't believe pm2-windows-service in its current state would interfere with the environment (my basic tests are able to access properties on process.env without issues).

So, if the environment variable is set at machine/system level, the only thing I can think of by reading your description of the issue is that you're accessing process.env (the entire environment object) when you actually want process.env.ENV? If that's just a typo in the issue description - sending me a minimal reproduction case would help debug the problem.

yarivat commented 7 years ago
  1. machine -windows 2012 server, node 4.3.3

  2. added machine environment variable ENV and set to qa

  3. the code that reads the above variable is (configFactory.js)

    function getConfig(environemt){
    var env = environemt || process.env.ENV;
    console.log(process.env.ENV)
    if(env == undefined){
        env = "dev";
    }
    
    var config = require('./configs/config.' + env + ".js");
    config.env = env;
    return config;
    }
  4. I have PM2-windows-service running in the services window

  5. on command line I do pm2 start c:\qa\socketio_server.js

  6. socketio_server.js have this lines

process.chdir(__dirname);
.....
var config = require('./configFactory').getConfig();
  1. in configFactory.js the console.log prints : '[object,object]'
jon-hall commented 7 years ago

@yarivat, thanks for the additional info - the only way I've been able to reproduce this issue is when the environment variable isn't visible in the command prompt that I run pm2 start from (e.g. executing echo %ENV% yields %ENV% instead of qa).

Once I set the ENV environment variable at machine level, and close and reopen the cmd prompt I'm working in, the value of process.env.ENV updates from [Object object] to the value specified in the environment variable.

If you can check the visibility of the environment variable in the command prompt that you run pm2 start in by executing echo %ENV% and see if it returns the expected value. If not you probably need to close and reopen the command prompt to reload the environment, or maybe even do a reboot. If the echo is returning the expected value, make sure to run pm2 kill to shutdown the old daemon and then re-launch with pm2 start and see if the problem persists (you may also need to restart the service to fully reload it).

If you still have the issue after all that then I don't think there's much more I can do, since I can't reproduce the issue and it doesn't actually seem to be caused by this package (pm2-windows-service) but is more of an issue with running under pm2 itself (I get the same behaviour when I run under the service and when I just run it manually using pm2 on the commandline) - so maybe raise a ticket with pm2 instead?

yarivat commented 7 years ago

Thanks, will check it

RodolpheGohard commented 7 years ago

I'll start looking into this and will update here as soon as I have something - thanks for the feedback and raising my attention on this feature as it'll hopefully bring support for collecting any additional configuration for the service scripts, which was a big oversight on my behalf.

Just saw the comments, thanks for looking into this !

yarivat commented 7 years ago

checked the visibility of the env variable on a command line, and it is visible ! the error still persists I'll need to check if the same error raises when I do pm2 start without pm2 -windows-service is running on background

On Tue, Jan 3, 2017 at 12:10 PM, Rodolphe notifications@github.com wrote:

I'll start looking into this and will update here as soon as I have something - thanks for the feedback and raising my attention on this feature as it'll hopefully bring support for collecting any additional configuration for the service scripts, which was a big oversight on my behalf.

Just saw the comments, thanks for looking into this !

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/jon-hall/pm2-windows-service/issues/22#issuecomment-270083250, or mute the thread https://github.com/notifications/unsubscribe-auth/AHxckudzY63NNj2SezqlX43jfc2sIIb7ks5rOh6jgaJpZM4LUO_p .

--

Yariv Attar

Customer Success

M +972.54.6503153

[image: Logo XS]

Create Better BackOffice

www.backand.com http://www.modubiz.com/

jon-hall commented 2 years ago

Closing this as the package is now deprecated, users should migrate to pm2-installer.