3

Nodejs was running on PM2 for a long time. And there is a corn which clears PM2 logs everyday

0 0 * * * find /home/user/.pm2/logs* -mtime +2 -exec rm -rf {} \;

Below error occurred for 1000 times and then pm2 stopped working and then when I reloaded the instance it was working fine as usual.

What could be the reason for this error?

/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
      throw err;
      ^

[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App  starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
      throw err;
      ^

[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/user/.pm2/logs/out.log'
}
2020-09-09T00:55:33: PM2 log: App name:app id:1 disconnected
2020-09-09T00:55:33: PM2 log: App exited with code [1] via signal [SIGINT]
2020-09-09T00:55:33: PM2 log: App starting in -cluster mode-
2020-09-09T00:55:33: PM2 log: App online
/home/user/.nvm/versions/node/v12.16.1/lib/node_modules/pm2/lib/ProcessContainer.js:167
      throw err;
      ^

[Error: ENOENT: no such file or directory, open '/home/user/.pm2/logs/out.log'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/home/user/.pm2/logs/out.log'
}


aRvi
  • 2,203
  • 1
  • 14
  • 30

3 Answers3

1

You're trying to clean up logfiles by just using rm . You Can't Do That™. On Linux and other UNIX-derived OSs, the program writing the log file holds it open even when you rm it from a directory. The file doesn't actually disappear until the program writing it closes it (or terminates). This is true for all sorts of software, not just pm2.

If all you want is to clear out the log files and start over, the command pm2 flush is for you.

If you want to save old logs for a limited period of time, you should investigate pm2's log rotation addon.

Commands like this may do the trick for you

# install the pm2 addon ... notice it says pm2 install, not npm install
pm2 install pm2-logrotate
# rotate when a logfile fills to ten megabytes
pm2 set pm2-logrotate:max_size 10M
# gzip compress the rotated logs to save space
pm2 set pm2-logrotate:compress true
# force rotation at 00:00 each day even if logfile is not full 
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'

You can say pm2 config pm2-logrotate to get it to show you its current settings.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • so why is it showing an error that the file is not found and also the server is getting restarted again and again 1000 times and finally instance stops – aRvi Sep 10 '20 at 17:32
  • Because file was removed, and pm2 can't (re)start your server because you removed its file. – Vasyl Boroviak Sep 11 '20 at 15:20
  • I tried removing manually by using `rm` but pm2 didn't restart and was working fine. In fact, after restarting the server file was automatically created. And this corn works fine every day and only yesterday I got this error @VasylBoroviak – aRvi Sep 11 '20 at 15:25
  • Mate, replace rm with pm2 flush – Vasyl Boroviak Sep 11 '20 at 22:38
  • Removing files while programs have them open can really confuse the programs, especially complex ones like pm2. If this were my project I would get rid of all the code to `rm` log files, then reboot my server, then use one of the suggestions in my answer. – O. Jones Sep 12 '20 at 11:10
  • @VasylBoroviak I want to keep logs of recent 2 days – aRvi Sep 19 '20 at 16:40
  • That's great. The last suggestion about "rotateInterval" is what you need. – Vasyl Boroviak Sep 19 '20 at 23:18
1

Can you try the below two steps and check this if it works.

pm2 start bin/www -i 0 // this will start a cluster and the main app

pm2 stop 0 // this will allow the cluster to keep running
Vijay122
  • 884
  • 8
  • 12
0

Adding

out_file: "/dev/null",
error_file: "/dev/null"

to the ecosystem.config.json file worked for me. It is basically disabling the logs. This is good for testing if the reason is in the logging functionality.

This is a workaround. If it works the the rotation of logging suggested in the answer of @O.Jones should work too.

Hairi
  • 3,318
  • 2
  • 29
  • 68