1. Several ways to run jar packages in Linux
- Method 1: java -jar xxx.jar
The most commonly used command to start the jar package, features: the current ssh window is locked, you can press CTRL + C to interrupt the program running, or close the window directly, and the program exits
- Method 2: java -jar xxx.jar &
& stands for running in the background, and ctrl+c
the program will continue to run after
- Method 3: nohup java -jar xxx.jar &
nohup means no hang up without hanging up, close the SSH client connection, the program will not stop running
By default, all the output of the job is redirected to the nohup.out file, how to redirect the output to the specified file?
- Method 4: nohup java -jar xxx.jar >aaa.log &
command >out.file is to redirect the commandd output to the out.flie file, that is, the output content is not printed to the screen, but output to the out.file file
- Method 5: nohup java -jar spring-boot-demo.jar > springboot.log 2>&1 &
- 方式六:nohup java -jar spring-boot-demo.jar > /dev/null 2>&1 &
Do not output logs
Two, nohup and &
Use &
the background runner:
- The result will be output to the terminal
- use
Ctrl + C
, program immunity - close
session
, program close
Run nohup
the program using:
- The results are output by default to
nohup.out
- use
Ctrl + C
, program closes - off
session
, program immune
nohup
Frequent use and cooperation online on weekdays &
to start the program
三、> /dev/null 2>&1
- Standard
>
redirector, allowing us to create an empty file of 0KB. It is usually used to redirect the output of a command to a new file. When redirection notation is used without a command, it creates a file. - It can
/dev/null
be regarded黑洞
as equivalent to a write-only file. Everything written to it is lost forever, and attempts to read from it get nothing. That is, all generated logs will be discarded 2>&1
The symbol>&
is a whole that means redirecting standard error 2 to standard output 1, if so2>1
, it means outputting standard error to file 1 instead of redirecting to standard output stream
First understand what 1 and 2 represent in Linux
When Linux executes a program, three streams are automatically opened
:0
standard input stream (default is keyboard) 1
: standard output stream (default is screen) 2
: standard error stream (default is screen)
name |
the code |
operator |
expressed in java |
File descriptors in Linux |
---|---|---|---|---|
standard input (stdin) |
0 |
< 或 << |
System.in |
/dev/stdin -> /proc/self/fd/0 -> /dev/pts/0 |
standard output (stdout) |
1 |
>, >>, 1> 或 1>> |
System.out |
/dev/stdout -> /proc/self/fd/1 -> /dev/pts/0 |
Standard error output (stderr) |
2 |
2> or 2>> |
System.err |
/dev/stderr -> /proc/self/fd/2 -> /dev/pts/0 |
As can be seen from the above table, the commonly used echo 'hello' > a.log
can be written asecho 'hello' 1> a.log
Why should 2>&1 be placed at the end? For the following shell command, nohup java -jar app.jar >log 2>&1 &
we might as well understand both 1 and 2 as pointers, and then look at the above statement as follows:
Originally 1——>screen (1 points to the screen) After executing >log, 1——>log (1 points to log) After executing 2>&1, 2——>1 (2 points to 1, and 1 points to log, So 2 also points to log)
Let’s analyze it again nohup java -jar app.jar 2>&1 >log &
Originally 1——>screen (1 points to the screen) After executing 2>&1, 2——>1 (2 points to 1, and 1 points to the screen, so 2 also points to the screen) After executing >log , 1—–>log (1 points to log, 2 still points to the screen) So this is not the result we want.
It is too troublesome to write ">log 2>&1" every time, can you abbreviate it? can be abbreviated as &>log
or>&log
Abbreviatednohup java -jar app.jar 2>&1 >log &
as:nohup java -jar app.jar &>log &