如何在不重启进程、不使用pm2-logrotate时将pm2 Node.js日志重定向到/dev/null?
Since you can't restart the process or use pm2-logrotate, here's a reliable Linux filesystem trick to redirect logs to /dev/null on the fly:
Locate your app's log paths
Runpm2 show <your-app-name-or-ID>and look for theout log pathanderror log pathentries (they’re usually under~/.pm2/logs/by default).Backup existing logs (optional but recommended)
If you don’t want to lose historical logs, make a copy first:cp /path/to/your-app-out.log /path/to/your-app-out.log.bak cp /path/to/your-app-error.log /path/to/your-app-error.log.bakReplace log files with symlinks to
/dev/null
Useln -sfto create a symbolic link that overrides the original log files:ln -sf /dev/null /path/to/your-app-out.log ln -sf /dev/null /path/to/your-app-error.logForce pm2 to reload log files
PM2 might still be writing to the old file handles, so run this to refresh the log streams:pm2 reloadLogsNow your app’s stdout and stderr will be sent to
/dev/nullimmediately, no restart required.
For future deployments or new apps, here are the standard ways to configure pm2 to send logs to /dev/null from the start:
Start with command-line arguments
When launching your app directly, specify the log paths explicitly:pm2 start app.js --out /dev/null --error /dev/nullThis sends both standard output and error logs to
/dev/null.Use an ecosystem configuration file
Create anecosystem.config.jsfile (or use your existing one) with log redirects:module.exports = { apps: [ { name: "your-node-app", script: "./app.js", out_file: "/dev/null", // Redirect stdout error_file: "/dev/null", // Redirect stderr // Optional: keep timestamp formatting if needed log_date_format: "YYYY-MM-DD HH:mm:ss" } ] };Then start your app with:
pm2 start ecosystem.config.jsSet global default for all pm2 apps
If you want every pm2-managed app to default to/dev/nulllogs, edit pm2’s global config file at~/.pm2/pm2.conf.jsand add:module.exports = { out_file: "/dev/null", error_file: "/dev/null" };Any new apps you start with pm2 will inherit this configuration.
内容的提问来源于stack exchange,提问作者tian-yuan

