Run node applications on your QNAP using PM2

After installing node I wanted to start my node applications automatically after rebooting. Therefore i use the PM2 process manager with a process file which will be started everytime the system boots. The challenge of using PM2 in autorun.sh was the fact, that the home directory for root changes from ‘/’ to ‘/root’ after booting up.

To solve this problem I created a directory .pm2 in my node-share and made symbolic links to the two home directories used by root in autorun.sh:

ln -s /share/CACHEDEV1_DATA/node/.pm2 /root/.pm2
ln -s /share/CACHEDEV1_DATA/node/.pm2 /.pm2

At the end of autorun.sh, after all initialization is done, I start my processes with the following line:

/share/CACHEDEV1_DATA/lib/bin/pm2 start /share/CACHEDEV1_DATA/node/processes.json

The processes.json file looks as follwoing:

{
  "apps": [
    {
      "name": "my_appname",
      "script": "/share/CACHEDEV1_DATA/node/appdir/server.js",
      "exec_mode": "fork_mode"
    }
  ]
}

Now after rebooting the system my apps get all started again, adding a new app only affects processes.json.

The major drawback of this solution is the fact that all processes are run as root. As QNAP does not allow other users than root over the command shell, I don’t see a solution to improve this. Any hints solving this security issue are welcome!


Leave a Reply