Detailed explanation of the solution to the problem of nohup log output being too large under Linux

Detailed explanation of the solution to the problem of nohup log output being too large under Linux

Recently, I ran a spark streaming program in a hadoop test cluster, and then used nohup ./execute.sh & to run the program in the background. After just a few days, the log file was over G. If there is a problem and I want to view the log file, it is obviously a troublesome thing to open the file, so I tried to reduce the file size by:

1. Explanation of nohup command:

a. Syntax: nohup [command] [args] [&]

b. Description: The nohup command runs the command specified by the Command parameter and any related Arg parameters, ignoring all hangup signals. Use the nohup command to run the program in the background after logging out. To run the nohup command in the background, add & (the symbol for "and") to the end of the command. If you do not specify redirection, the log is output to the nohup.out file in the current directory by default.

Generally submit like: nohup ./execute.sh & so that the log or output is in the current running directory.nohup.out

Redirection: nohup ./execute.sh > /home/xxx/log.log 2>&1 & : This way the log will be redirected to the specified directory

2. Split nohup.out and prevent it from growing indefinitely

The general submission command I use here is: nohup ./execute.sh &, so there will be a nohup.out file in the current directory. At this time, you can find a way to regularly split nohup.out into multiple small files, but at the same time make sure that nohup.out does not grow indefinitely (generally, the program cannot be interrupted):

a. Every day (set the time as needed), split the log of the previous day regularly (for example, if it is about 1g per day, you can split it into about 100m each time),

b. After splitting, the nohup.out file will be saved to ensure that the new output log will continue to be output to nohup.out

The above in the shell

current_date=`date -d "-1 day" "+%Y%m%d"`

split -b 65535000 -d -a 4 nohup.out ./log/log_${current_date}_ The split command is used here to split the nouhup file according to the specified size (65535000b is about 60 MB, the size can be customized), and divided into the specified format (-d -a 4 with a 4-digit suffix starting from 0000, for details, you can Baidu split command usage), the final output format is log_20160610_0001

cat /dev/null > nohup.out (This command will instantly clear the nohup.out file, and will continue to write to the file later), directing the log to /dev/null

The same can be done using redirected output, except that the redirected file name is used instead.

Define these commands in a shell file and run them regularly every day. This way, the daily logs will be divided into several parts, which is convenient for troubleshooting and if the log backlog is too large. You can delete historical logs regularly and keep only the last few days.

The overall code is as follows:

this_path=$(cd `dirname $0`;pwd)
 
cd $this_path
echo $this_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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Linux nohup to run programs in the background and view them (nohup and &)
  • Solve the problem of python nohup linux background running output
  • PHP daemon process plus Linux command nohup to implement task execution once per second
  • How to use & and nohup in the background of Linux
  • Linux nohup and tail-f usage
  • Nohup and background running process viewing and termination in Linux

<<:  JavaScript timer to achieve seamless scrolling of pictures

>>:  Mysql cannot select non-aggregate columns

Recommend

How complicated is the priority of CSS styles?

Last night, I was looking at an interview question...

Vue.js uses Element-ui to implement the navigation menu

This article shares the specific code for impleme...

Detailed explanation of Json format

Table of contents A JSON is built on two structur...

15 Linux Command Aliases That Will Save You Time

Preface In the process of managing and maintainin...

Specific operations of MYSQL scheduled clearing of backup data

1|0 Background Due to project requirements, each ...

How to configure ssh/sftp and set permissions under Linux operating system

Compared with FTP, SSH-based sftp service has bet...

A practical record of encountering XSS attack in a VUE project

Table of contents Preface Discover the cause Cust...

Implementation of Nginx load balancing/SSL configuration

What is load balancing? When a domain name points...

jQuery implements ad display and hide animation

We often see ads appear after a few seconds and t...

A detailed introduction to setting up Jenkins on Tencent Cloud Server

Table of contents 1. Connect to Tencent Cloud Ser...

Open the Windows server port (take port 8080 as an example)

What is a Port? The ports we usually refer to are...

Introduction to CSS BEM Naming Standard (Recommended)

1 What is BEM Naming Standard Bem is the abbrevia...

How to use the Clipboard API in JS

Table of contents 1. Document.execCommand() metho...