9

My nohup.out file is growing fast.

I am running an application in the background, it's writing nohup.out file,

now I need to rotate nohup.out file without killing my application. Can this be done?

  • Copy, then truncate. – Jenny D Aug 27 '14 at 12:17
  • He may loose some precious log entries between these two operations you suggested. I would not recommend it. – Cleankod Jan 3 '17 at 11:03
5

You should take a look at flog in combination with logrotate. If you pipe your application output through this you can then SIGHUP the flog process without having to kill your running application.

flog (file logger) is a program that reads input from STDIN and writes to a file.

If SIGHUP is received, the file will be reopened, allowing for log rotation [see logrotate(8) on RH.]

The log file will only be reopened if flog detects that rotation has occurred (ie, old file gone or inode changed). flog is extremly small (less than 500 bytes memory footprint.)

  • Can nohup use a pipe instead of nohup.out? – Brian Minton Aug 27 '18 at 20:14
  • 1
    You can use file redirection (>) to output to a file other than nohup.out as per the man page: To save output to FILE, use 'nohup COMMAND > FILE' – jas_raj Aug 28 '18 at 16:00
3

You cannot rotate it. You can truncate it with the command >nohup.out, which will delete all contents in the file.

You can copy the file first and then truncate it, if you need to save the output. But there is a small window for a race, where output can be written after you copied the file but before you truncated it. Any output written during that window will be forever lost.

  • 1
    I believe that truncating won't free up any space on the disk until the file handle is closed. – symcbean Aug 22 '14 at 8:45
  • @symcbean Your belief is incorrect. Truncating the file free up the space immediately. – kasperd Aug 22 '14 at 8:46
  • Logrotate copytruncate should do the trick. – Iain Aug 22 '14 at 10:04
  • @Iain The steps I describe are equivalent to copytruncate in logrotate. But nohup.out is rarely the kind of file you would configure a cron job to be rotating. – kasperd Aug 22 '14 at 10:07
2

You can't.

You can unlink the file (rm) but the data still has a footprint on the disk and will continue to be written to as long as there is an open file handle.

You can rename the file - but again this does not stop it being written to (but if you are starting background jobs regularly the newer ones will write to the same file).

Really you should explicitly redirect the output of the job to something designed to handle this (e.g. apache rotatelogs).

  • logrotate copytruncate anyone ? – Iain Aug 22 '14 at 10:03
  • That totally depends on whether nohup appends to nohup.out line-by-line, or whether it maintains a pointer to nohup.out's location. See serverfault.com/questions/221337/… for an example. – pepoluan Aug 24 '14 at 18:19
1

Logrotate has a copytruncate option available, which will truncate (empty) your specified file after it has been copied into the normal rotation (and compression, if set) scheme.

   copytruncate
          Truncate  the  original log file in place after creating a copy,
          instead of moving the old log file and optionally creating a new
          one,  It  can be used when some program can not be told to close
          its logfile and thus might continue writing (appending)  to  the
          previous log file forever.  Note that there is a very small time
          slice between copying the file and truncating it, so  some  log-
          ging  data  might be lost.  When this option is used, the create
          option will have no effect, as the old log file stays in  place.

From the logrotate manpage.

1

You can do something like this:

cp nohup.out nohup.out.save # save it somewhere
echo "" > nohup.out

You don't need to kill your app.

1

Not sure if I'm too late on this. But for others who stumble upon this forum now, I didn't have to try out anything with flog or anything.

My job starts simply like this:

nohup <my process> &

This starts my job and starts appending to a nohup.out in that directory.

All I had to do was to setup a logrotate which usually comes with the distro and configure something like this in /etc/logrotate.conf

~/my abosolute path/nohup.out {
    size 1024k
    copytruncate
    rotate 100
    maxage 100
}

And it worked!! nohup.out was getting truncated while getting the new appends on the same file. The process didn't die either. And a new nohup.out like nohup.out-20190528 this was created.

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.