pm2.startup isn't working via API

Created on 25 May 2017  ·  12Comments  ·  Source: Unitech/pm2

What's going wrong?
Trying to run a script on startup using PM2 API but got error while used pm2.startup method:

    var user = (opts.user || process.env.USER);
                    ^


TypeError: Cannot read property 'user' of undefined
    at module.exports.CLI.startup (/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/lib/API/Startup.js:166:21)
    at pm2.dump (/home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js:62:23)
    at fin (/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/lib/API/Startup.js:364:24)
    at ex (/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/lib/API/Startup.js:371:30)
    at ex (/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/lib/API/Startup.js:377:16)
    at /home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/lib/API/Startup.js:378:9
    at /home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2-axon-rpc/lib/client.js:45:10
    at Parser.<anonymous> (/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2-axon/lib/sockets/req.js:67:8)
    at emitOne (events.js:96:13)
    at Parser.emit (events.js:191:7)

How could we reproduce this issue?

pm2.connect((err) => {
    if (err) {
        console.error(err);
        process.exit(2);
    }

    pm2.start({
        script: `${__dirname}/daemon.js`,
        name: 'foo'
    }, (err, apps) => {
        pm2.dump((err, ret) => {
            if (err) throw err;
            pm2.startup(); // <-- here is the error
            pm2.disconnect();
        });
        if (err) throw err;
    });
});

Supporting information

PM2 version: v2.6.4
Node version: v7.10.0
Ubuntu 16
Open for PR Startup Bug

All 12 comments

Because you need to use the correct arguments first.
Secondly you need to run this under root to be able to make it work, pm2 startup need root right to register the PM2 daemon init file to systemd for ubuntu

@vmarchaud OK, passed user to opts like this pm2.startup(undefined, { user: 'finom' }); and used sudo bug got another error:

Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=finom
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/finom/.pm2
PIDFile=/home/finom/.pm2/pm2.pid

ExecStart=/home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js resurrect
ExecReload=/home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js reload all
ExecStop=/home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-finom.service
Command list
[ 'chmod +x /etc/systemd/system/pm2-finom.service',
  'systemctl enable pm2-finom',
  'systemctl start pm2-finom',
  'systemctl daemon-reload',
  'systemctl status pm2-finom' ]
>>> Executing chmod +x /etc/systemd/system/pm2-finom.service
[DONE]
>>> Executing systemctl enable pm2-finom
[DONE]
>>> Executing systemctl start pm2-finom
Job for pm2-finom.service failed because the control process exited with error code. See "systemctl status pm2-finom.service" and "journalctl -xe" for de
tails.
[ERROR] Exit code : 1
[PM2][ERROR] systemctl start pm2-finom failed, see error above.
$ systemctl status pm2-finom.service
● pm2-finom.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-finom.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since пт 2017-05-26 13:19:04 EEST; 2min 21s ago
     Docs: https://pm2.keymetrics.io/
  Process: 9166 ExecStart=/home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js resurrect (code=exited, status=203/EXEC)

тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: Starting PM2 process manager...
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Control process exited, code=exited status=203
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: Failed to start PM2 process manager.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Unit entered failed state.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Failed with result 'exit-code'.
$ journalctl -xe
...
...
...
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: Starting PM2 process manager...
-- Subject: Unit pm2-finom.service has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit pm2-finom.service has begun starting up.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[9166]: pm2-finom.service: Failed at step EXEC spawning /home/finom/Dropbox/sync/my/github/mongo-git-
-- Subject: Process /home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /home/finom/Dropbox/sync/my/github/mongo-git-backup/index.js could not be executed and failed.
--
-- The error number returned by this process is 13.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Control process exited, code=exited status=203
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: Failed to start PM2 process manager.
-- Subject: Unit pm2-finom.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit pm2-finom.service has failed.
--
-- The result is failed.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Unit entered failed state.
тра 26 13:19:04 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Failed with result 'exit-code'.
тра 26 13:19:06 finom-HP-ENVY-15-Notebook-PC sudo[9114]: pam_unix(sudo:session): session closed for user root
тра 26 13:21:26 finom-HP-ENVY-15-Notebook-PC systemd[1]: Configuration file /etc/systemd/system/pm2-finom.service is marked executable. Please remove exe
lines 2388-2427/2427 (END)

Any ideas how to fix or at least localize?

Btw you have unreachable code: https://github.com/Unitech/pm2/blob/master/lib/API/Startup.js#L79-L80

Because pm2.startup isn't made to be used with the API, see this code.
In your unit file (in /etc/systemd/system/pm2-finom.service), it should be :

[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=finom
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

Environment=PATH=/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/finom/.pm2
PIDFile=/home/finom/.pm2/pm2.pid

ExecStart=pm2 resurrect
ExecReload=pm2 reload all
ExecStop=pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-finom.service

It may not work, i don't know how you installed pm2 but the main part are :

ExecStart=pm2 resurrect
ExecReload=pm2 reload all
ExecStop=pm2 kill

@vmarchaud hmm, got it. Will think about how to handle that.

I installed pm2 locally. Can I request a fix? Allow to pass PM2 path to startup options.

isn't made to be used with the API

But it's documented there.

@finom Indeed the documentation is misleading. I would be great if you modify PM2 to achieve what you want to do and them make a PR to make it available for everyone :)

@vmarchaud I think it should look like

template.replace(/%PM2_PATH%/g, opts.pathToPm2, pathprocess.mainModule.filename)

Looks good?

Should be more like :

// if opts.pm2Path exist, use it else fallback on the process main module
template.replace(/%PM2_PATH%/g, opts.pm2Path || process.mainModule.filename)

@vmarchaud I'm trying to run it locally, got the following template output:

...
ExecStart=/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/bin/pm2 resurrect
ExecReload=/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/bin/pm2 reload all
ExecStop=/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/bin/pm2 kill
...

Manual execution of /home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/bin/pm2 resurrect works fine but I still get errors:

Job for pm2-finom.service failed because the control process exited with error code. See "systemctl status pm2-finom.service" and "journalctl -xe" for details.

$ systemctl status pm2-finom.service
● pm2-finom.service - PM2 process manager
   Loaded: loaded (/etc/systemd/system/pm2-finom.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since пт 2017-05-26 16:28:08 EEST; 3min 19s ago
     Docs: https://pm2.keymetrics.io/
  Process: 12759 ExecStart=/home/finom/Dropbox/sync/my/github/mongo-git-backup/node_modules/pm2/bin/pm2 resurrect (code=exited, status=1/FAILURE)

тра 26 16:28:07 finom-HP-ENVY-15-Notebook-PC systemd[1]: Starting PM2 process manager...
тра 26 16:28:08 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Control process exited, code=exited status=1
тра 26 16:28:08 finom-HP-ENVY-15-Notebook-PC systemd[1]: Failed to start PM2 process manager.
тра 26 16:28:08 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Unit entered failed state.
тра 26 16:28:08 finom-HP-ENVY-15-Notebook-PC systemd[1]: pm2-finom.service: Failed with result 'exit-code'.

How to get where is the error which causes process exit with status 1?

@finom No idea, could you check the systemd logs ? also make a pastebin with the content of ~/.pm2/pm2.log if you can

@vmarchaud https://pastebin.com/41T9pYHY

$ journalctl -u systemd
-- No entries --

(not sure do I get systemd log right)

Also I noticed that my current user (which is the same as I used at opts.user) has no access rights to /.pm2/pm2.log and I had to use sudo. Maybe that's the problem.

Yeah try to chown the ~/.pm2 dir to your user and verify that it can read/write on it.

@vmarchaud I can do this but I decided to stop my research. I wanted to create a script which can be transparently loaded on boot (i. e. my-tool --restore-on-reboot) See https://github.com/Unitech/pm2/issues/2880. But that looks too hard. Thank you for your help. I'm still going to use pm2.start but not more than that. API isn't ready for my case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phra picture phra  ·  3Comments

chaos-git picture chaos-git  ·  3Comments

lefam picture lefam  ·  3Comments

rangercyh picture rangercyh  ·  4Comments

psparago picture psparago  ·  3Comments