nohup runs Java tail to view logs
Run command in the background: nohup
-
nohup command
: nohup means no hang up. - If you are running a process and you feel that the process will not end when you log out of the account, you can use the nohup command.
- This command ignores all SIGHUP signals and can continue to run the corresponding process after you log out of your account/close the terminal.
The general format of this command is:
nohup yourcommand &
-
yourcommand
: Start the object command. It can be followed by various parameters required by this command. - & refers to running in the background, but when the user launches (hangs), the command will automatically exit. Nohup and & can achieve backend operation without hanging up.
Implement screen output recording to log files
nohup yourcommand 2>&1 &
- 0 – stdin (standard input),1 – stdout (standard output),2 – stderr (standard error) ;
- 2>&1 is to redirect standard error (2) to standard output (&1), and standard output (&1) is then redirected and input into the log file.
If you want to output the log to another file, you can add a file path parameter.
as follows:
nohup yourcommand > 2>&1 &
Among them is the file name that saves the output;
Run Java
nohup java -jar &
Command introduction
When you execute the nohup java -jar & command on the command line, nohup is a command used in Unix/Linux systems. It is used to run the program in the background and ignore the HUP (Hangup) signal to ensure that the program can continue to run when the user is logged out or the terminal is closed.
The nohup command redirects the program's output to a file named , by default,This file will be created in the current directory。
Therefore, when you execute nohup java -jar &, the output of the program (including the log) will be redirected to the file.
Add extra parameters
For example, we need to set the port information on the command line
nohup java -jar --=8080 &
Modify log output address
nohup java -jar --=8080 > /path/to/ &
> /path/to/ Partially redirects log output to the specified log file。Please /path/to/ Replace with the actual log file path you want to use。
By executing the above command, you will run the file in the background and output the logs to the specified log file.
Note that if the specified log file path does not exist, the system will automatically create the file.
Real-time monitoring log output content command: tail
1. The tail command can be used to view the content of the file. The parameter -f is often used to check the changing log files, such as viewing the log information output by Java applications in real time.
2. tail -f filename will display the last content in the filename file on the screen and refresh it continuously. As long as filename is updated, you can see the latest file content.
Parameter Description
-
-f
Loop reading -
-q
No processing information is displayed -
-v
Show detailed processing information -
-c
<Number> Number of bytes displayed -
-n
<Line Number> Displays the end of the file n lines of content -
--pid=PID
Used in combination with -f, which means that the process ID and PID end after it die -
-q
--quiet, --silent Never output the first part of the file name given -
-s
--sleep-interval=S is used in combination with -f, indicating that S seconds sleeping at each repetition interval
Common usage examples
Give an example Description
tail Show the current directory The end of the file 10 Line information tail -f Track the current directory name The growth of documents,according to ctrl + c Can exit tracking tail -c 10 Show the current directory The end of the file 10 Characters tail -f -n 50 Real-time display The end of the file 50 Message,The number of rows can be defined by yourself。 tail -f ----Refresh the latest logs in real time tail -100f --------Refresh the latest in real time100Log tail -100f | grep [Keywords] -------Find the latest one一百OK中与Keywords匹配的OK tail -100f | grep '2019-10-29 16:4[0-9]' ------Find the latest one100The time range in the row is2019-10-29 16:40-2019-10-29 16:49Rows in range tail -1000f | grep -A 5 [Keywords] ----------Check out the latest1000OK中与Keywords匹配的OK加上匹配OK后的5OK
Give an example Description
- The tail command is a good partner for the nohup command. With the -f parameter, you can monitor the added information to the log file in real time.
tail -f
- tail -f is equivalent to --follow=descriptor, tracking is performed according to the file descriptor. When the file is renamed or deleted, tracking is stopped.
- tail -F is equivalent to --follow=name --retry, tracking according to the file name and keeping retry, that is, after the file is deleted or renamed, if the same file name is created again, tracking will continue
- tailf is equivalent to tail -f -n 10. Unlike tail -f, if the file does not grow, it will not access disk files, so tailf is especially suitable for those who track log files on portable machines because it reduces disk access and can save power.
Monitor whether the program is running normally: ps
The ps command can also be used in conjunction with the nohup command to display the status of the current process. You can monitor whether the background program is running normally or has been hung.
ps -ef|grep yourcommand
parameters of grep
(Abbreviation from Globally search a Regular Expression and Print) is a powerful text search tool that can search text using regular expressions and print matching lines.
-
-a
or--text
: Do not ignore binary data. -
-A<Display number of rows>
or--after-context=<display number of rows>
: In addition to displaying the column that conforms to the template style, the content after the line is displayed. -
-b
or--byte-offset
: Before displaying the line that matches the style, the number of the first character of the line is marked. -
-B<Display number of rows>
or--before-context=<display number of rows>
: In addition to displaying the line that matches the style, the content before that line is displayed. -
-c
or--count
: Calculate the number of columns that match the style. -
-C<Display number of rows>
or--context=<display number of rows>
or-<Display number of rows>
: In addition to displaying the line that matches the style, the content before and after the line is displayed. -
-d <action>
or--directories=<action>
: This parameter must be used when specifying that the directory is to be found instead of a file, otherwise the grep instruction will return the information and stop the action. -
-e<Template Style>
or--regexp=<Template Style>
: Specify the string as the style to find the content of the file. -
-E
or--extended-regexp
: Use the style as an extended regular expression.
-f<Rule File>
or--file=<rule file>
: Specifies a rule file whose content contains one or more rule styles, allowing grep to find the content of the file that meets the rule conditions, and the format is one rule style per line.
-
-F
or--fixed-regexp
: Treats styles as a list of fixed strings. -
-G
or--basic-regexp
: Use styles as normal notation. -
-h
or--no-filename
: The file name to which the line belongs is not marked until the line that matches the style is displayed. -
-H
or--with-filename
: Before displaying the line that matches the style, indicate the file name to which the line belongs. -
-i
or--ignore-case
: Ignore the difference in upper and lower case of characters. -
-l
or--file-with-matches
: Lists the file name whose file content meets the specified style. -
-L
or--files-without-match
: Lists file names whose file content does not match the specified style. -
-n
or--line-number
: Before displaying the row that matches the style, the column number of the row is marked. -
-o
or--only-matching
: Only the matching PATTERN section is displayed. -
-q
or--quiet
or--silent
: No information is displayed. -
-r
or--recursive
: The effect of this parameter is the same as the specified "-d recurse" parameter. -
-s
or--no-messages
: No error message is displayed. -
-v
or--revert-match
: Shows all lines that do not contain matching text. -
-V
or--version
: Display version information. -
-w
or--word-regexp
: Only columns that match the full character are displayed. -
-x --line-regexp
: Only the columns that match all columns are displayed. -
-y
: The effect of this parameter is the same as the specified "-i" parameter.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.