How to limit size of nohup.out logs

k.nguyen picture k.nguyen · Dec 7, 2015 · Viewed 13.6k times · Source

I use the command:

nohup <command to run> & 

and it logs to the nohup.out file, however this log file could get pretty BIG so was wondering if there was a way to automatically save off the output to nohup1.out, nohup2.out nohup3.out etc when the nohup.out gets too big. This is all without terminating the original command.

Answer

fenwick picture fenwick · Jun 12, 2016

logrotate(8) is likely what you need and probably already in your Linux distro. Specify in /etc/logrotate.conf the size limit and how many rotations you want:

From an example on thegeekstuff.com:

/tmp/output.log {
        size 1k
        create 700 user user
        rotate 4
}
  • size 1k – logrotate runs only if the filesize is equal to (or greater than) this size.
  • create – rotate the original file and create the new file with specified permission, user and group.
  • rotate – limits the number of log file rotation. So, this would keep only the recent 4 rotated log files.

Then run logrotate /etc/logrotate.conf

I ignored the example's file status location since logrotate is already running as a cron job by my system and a status file already exists (in a different location from the example's).