Run node dist/index.js in a terminal and open the webpage. That tells you the service started at least once. But if the backend exits in the middle of the night, who starts it again? If the entire machine loses power and restarts, who starts the program responsible for restarting it? In May I searched for something quite direct: “linux run program in background and auto restart on crash”. There are two lifecycles here: PM2 can supervise the application process, while the operating system has to take the first step after the machine boots.1
Which file should PM2 start?
First, run the service manually from its deployment directory and verify the actual entry point, working directory, and environment variables. The same service delivered in different forms needs different PM2 commands. A JavaScript file generated by ncc needs Node to interpret it. An executable with its runtime included can run directly under the operating system.2
Here are example configurations for the two entry points. Choose one based on the artifact you're shipping. Assume PM2 is installed on the server and you're in the release directory. In the second command, asset-service is a Linux program with execute permission.3
# 交付的是 JavaScript
pm2 start ./dist/index.js --name assets
# 交付的是可执行文件
pm2 start ./asset-service --name assets --interpreter none
--interpreter none tells PM2 to launch the file directly. The business application can carry its own Node runtime, while PM2 installed through npm on the server still uses its own Node environment. The commands here illustrate external supervision. An application can also import pm2 and call connect() and start() itself, which puts the PM2 dependency and startup logic inside the application.34
Run pm2 list, pm2 describe assets, and pm2 logs assets as the same system account to check which process you're looking at, where it started, and what it printed before exiting. If manual startup works but the process can't read its database or environment configuration under PM2, compare the working directories and environment variables first. Relative paths depend on the working directory. Explicitly setting cwd and env in a configuration file pins both conditions down.35
How do you recover from a process crash versus a server reboot?
| Scenario | What's still running | Recovery |
|---|---|---|
| The application process exits abnormally | The PM2 daemon is still running | PM2 restarts the program according to its restart policy. For repeated startup failures, configure a restart delay or exponential backoff to give dependencies time to recover and yourself time to inspect the logs.6 |
| The entire Linux machine reboots | Both the previous PM2 process and the application are gone | Run pm2 startup to get a startup-service configuration command tailored to the machine, then execute the installation command it prints. Check the process list and run pm2 save as the application account. The system can then restore that list after boot.1 |
| The server already uses systemd for all services | systemd starts with the system | Point a service unit's ExecStart= directly at the executable, or at Node and the JavaScript entry point. Use Restart=on-failure for abnormal exits and enable the unit to start at boot.7 |
On May 5 I first read about PM2 environment variables, restart strategies, and graceful shutdown, then looked into systemd. On May 9 I added an example systemd service configuration to AssetMan's README for deployment. By June I was interested in a different question: could I put process management inside the application too and give users just one entry point to launch?8
What went wrong when I put PM2 inside the bundled entry point?
On June 26 I tried invoking PM2 from AssetMan's app.js. The local logs confirm that PM2 successfully started the unbundled backend/app.js. I also asked how to find and manage PM2 from the terminal once the application had started it. The June 28 commit shows the design: the parent calls pm2.start(), supplies the current file as script, and enables automatic restart.9
Importing PM2 from the entry point also adds its dependencies to the JavaScript shipped with each release. I later rebuilt the entry points before and after that commit using the same dependencies and ncc minification settings. Counting only dist/index.js, adding PM2 took the file from 2.01 MiB to 3.30 MiB, an increase of about 64.4%. Keeping the same PM2 startup code but adding -e pm2 to ncc brought it back to 2.01 MiB. Most of the increase came from dependencies bundled into the main file. This comparison build still needs to be able to load pm2 at runtime. If the server's PM2 starts the application externally, the application entry point doesn't need to import it.10
The actual startup failure happened when I ran the dist/index.js generated by ncc. The terminal stack trace pointed at this line in the bundle:
var ClientJS = path.resolve(path.dirname(module.filename), 'Daemon.js');
PM2 was locating its own daemon entry point, Daemon.js. The stack stopped in Client.launchDaemon with ERR_INVALID_ARG_TYPE: the value passed to path.dirname() was undefined. A library can make different assumptions about file locations when loaded from its original module directory versus merged into a single JavaScript file. PM2 failed while looking for its own entry point. The application hadn't even reached the point where it listened on a port.11
I then tried a tiny start.js that used child_process.fork() to start index.js from the same directory. On an exit event it waited 100 milliseconds and launched it again. The local Linux ZIP I still have uses this entry point, and the June 29 source removed the embedded PM2 startup logic. The script illustrates a parent restarting a child, but also restarts it on normal exit. If the parent itself exits, nothing remains to supervise the service.12
My recollection is that the eventual deployment used PM2 plus a single executable. The local ZIP retains the intermediate script, and I didn't keep the complete production PM2 configuration or binary build command. A deployment like this is easier to read once you've established who starts whom: the business application serves requests, an external process manager can supervise it directly, and the operating system handles recovery after a reboot.
References
Footnotes
-
PM2 contributors. (n.d.). Startup script. PM2 documentation. Saving the app list to be restored at reboot ↩ ↩2
-
PM2 contributors. (n.d.). Process Management. PM2 documentation. Starting a script or executable with pm2 start ↩
-
PM2 contributors. (n.d.). Ecosystem file. PM2 documentation. Configuration attributes. Strzelewicz, A. (n.d.). ForkMode.js (v6.0.8). GitHub. Interpreter selection in fork mode ↩ ↩2 ↩3
-
PM2 contributors. (n.d.). PM2 API. PM2 documentation. Programmatic API ↩
-
PM2 contributors. (n.d.). Environment Variables. PM2 documentation. Applying env from the configuration when starting a process ↩
-
PM2 contributors. (n.d.). Restart strategies. PM2 documentation. Restart delay and exponential backoff restart delay ↩
-
systemd contributors. (n.d.). systemd.service. freedesktop.org. Restart= ↩
-
CaiBorui [S2thend]. (2025, May 9). Add systemd deploy nodejs guide [Commit 68e62a8]. GitHub. AssetMan deployment notes ↩
-
CaiBorui [S2thend]. (2025, June 28). Add start app with pm2 for easier deployment [Commit d7b7d5c]. GitHub. Changes to the backend entry point ↩
-
CaiBorui. (2026, November 23). Comparison of AssetMan backend entry point sizes [Local rebuild]. Used
backend/app.jsfrom commit d7b7d5c and its parent with identical remaining source and local dependencies. Linux, Node.js 22.12.0, ncc 0.38.3, PM2 6.0.8. Built withncc build -m app.js -o dist. The main files measured 2,103,776 and 3,458,578 bytes. The new entry point built with-e pm2measured 2,105,937 bytes. 1 MiB = 1,048,576 bytes. ↩ -
PM2 contributors. (n.d.). Client.js (v6.0.8). GitHub. Client.launchDaemon ↩
-
CaiBorui [S2thend]. (2025, June 29). Add env file support for production build [Commit d532b54]. GitHub. Removing the in-app PM2 startup branch ↩