Outline
Sometimes we run in the background to make a program, usually executed with nohup ./xxx & let the program running in the background, assuming that over a period of time to log on G, and if there is a problem you want to view the log file is apparently open a very troublesome thing, is there any way you can reduce the file size of it?
nohup command interpreter:
grammar:
nohup [command] [args] [&]
Description: nohup command runs specified by the Command parameter and any related Arg parameters of command, ignore all the hang up signal. The program runs in the background using nohup command after logout. To run a background in nohup command, add & (representation "and" sign) to the end of the command, if you do not specify a redirect, the default output to the log files in the current directory in nohup.out,
Redirection: nohup ./execute.sh> /home/xxx/log.log 2> & 1 &: this log redirected to the specified directory.
nohup and & differences
&: Refers to run in the background
nohup: do not hang up the run, and did not pay attention to the function running in the background ,, refers to, with the nohup command to run the command permanent execution continues, and the user terminal does not matter, for example, we disconnect the SSH connection will not affect his run, do not pay attention to the meaning nohup running in the background; & is running in the background
Segmentation nohup.out, do not let it grow indefinitely
Ideas: timing nohup.out cut into multiple small files, but have to make nohup.out will not grow indefinitely (under normal circumstances is a program can not be interrupted).
a, per day (as needed to set the time), the timing of the day before the log cut points (such as about 1g per day, then it may be time sliced about 100m),
b, after the nohup.out file segmentation, to ensure new log output will continue output to stop nohup.out
More than in the shell
current_date=`date -d "-1 day" "+%Y%m%d"` split -b 65535000 -d -a 4 nohup.out ./log/log_${current_date}_
As used herein, the split command, the specified file size nouhup slicing (65535000b M about 60 bar, can customize the size), and divided into the specified format (-d -a 4 is a suffix of from 0000 to four digital form), The final output format is log_20190918_0001
-d character suffix to suffix numbers, can be added later indicates the time from which numbers start
Specifies the length of the suffix -a
-b <byte>: specifies how many bytes each cut into a small file
-C <byte>: the parameter "-b" is similar, but will try to maintain the integrity of the cutting of each row
cat /dev/null > nohup.out
(This command will clear the moment nohup.out file, follow-up will continue to write the file), the log will be directed to / dev / null in
You can redirect the output using the same way, but replace the redirected file name
These are defined in a shell command file can be run regularly every day, every day so that the log is divided into several parts, investigation is also convenient, but if too large, then the log backlog. Timing can delete the history log, you can retain the last few days.
The final script is as follows:
#!/bin/bash path=$(cd `dirname $0`;pwd) cd $path echo $path current_date=`date -d "-1 day" "+%Y%m%d"` echo $current_date split -b 65535000 -d -a 4 /home/nohup.out /home/log/log_${current_date}_ cat /dev/null > nohup.out