我的nohup.out文件正在快速增长。
我正在后台运行一个应用程序,它正在写nohup.out文件,
现在我需要旋转nohup.out文件,而不会杀死我的应用程序。 可以这样做吗?
你应该看看flog与logrotate结合。 如果你通过这个pipe道输出你的应用程序,那么你可以SIGHUP flog进程,而不必杀死正在运行的应用程序。
flog(文件logging器)是一个从STDIN读取input并写入文件的程序。
如果收到SIGHUP,文件将被重新打开,允许日志旋转[请参阅RH上的logrotate(8)]。
日志文件只有在flog检测到轮换发生时才会重新打开(即旧文件已经更新或inode更改)。 flog非常小(内存占用less于500字节)。
你不能旋转它。 您可以使用>nohup.out命令截断它,这将删除文件中的所有内容。
如果需要保存输出,可以先复制文件,然后截断它。 但是在比赛中有一个小窗口,输出可以在复制文件之后但在截断之前写入。 在这个窗口写的任何输出将永远丢失。
你不能。
您可以取消链接文件(rm),但数据在磁盘上仍然有足迹,只要存在打开的文件句柄,就会继续写入。
您可以重新命名文件 – 但是这并不会阻止它被写入(但是如果您定期启动后台作业,则新的文件将写入相同的文件)。
真的,你应该明确地redirect工作的输出到devise来处理这个(例如apache rotatelogs )。
Logrotate有一个copytruncate选项可用,它将在复制到正常旋转(和压缩,如果设置)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.
从logrotate手册页 。