The previous few articles shared the implementation and principles of asynchronous interfaces, and basically required a look at the source code to help you understand them. To be honest, it might be ok to read the source code, but it can be a bit boring to read the source code when you don’t know much about it at first, especially if you don’t really want to understand it. Only when we really want to understand this thing, we will be willing to spend energy on research, then we will not be so boring, but feel interesting in the process of exploration.

Then I begin to share a little bit easier, but less boring and very useful, it is everyday we will use a lot of orders, some of that we use more frequently, and it is the necessary commands, these orders we need to master, which is beneficial to improve the efficiency of our daily work.

First, we need to know all the commands are designed to help us to complete one or more things, also is in the computer system, we want to do, all need through the corresponding command to tell the computer our demand, if a command, often use, also means that the command often do we need to do, The following commands are elicited based on common requirements.

Background start SpringBoot project:

nohup java -jar usercenter.jar --spring.profiles.active=development > usercenter.log 2>&1 &
Copy the code

As a background developer, we often need to start the project program in daily life, but we usually start it locally through IDEA. If the UserCenter project needs to be packaged and deployed to a server for development and testing, then we need to package userCenter at this time. Then copy it to the server and connect to the server terminal through SSH. Assume that the server is already installed with Java runtime environment. Now we can start the jar package we just copied. Normal foreground startup commands are:

java -jar usercenter.jar --spring.profiles.active=development
Copy the code

But this approach is to start at the front desk, the log output directly to the terminal, this for one-time start-up project that is good, good start after the test the shut down directly to the end disconnect the server connection is good, but if you want to keep it continue to run after we quit the terminal is not line, because of the above in this way we exit the terminal also stop running the application. In this case, it is necessary to keep the program running in the background by launching it in the background, the nohup command.

Nohup is no hang up. It is used to run commands in the background without interrupting the running of the program. The nohup command, by default (when not redirected), outputs a file named nohup. Out to the current directory. If the nohup.

Nohup Command [Arg…] [&] Parameter description: Command: indicates the Command to be executed. Arg: Some parameters that can specify the output file. & : Allows commands to be executed in the background and continue to be executed after the terminal exits.

This is the introduction to the nohup command in the rookie tutorial. By default, we output a file named nohup. Out to the current directory. If we do not redirect the output of the nohup command to another file, it will be output to a file named nohup. If the nohup.out file in the current directory is not writable, the output is redirected to the $HOME/nohup.out file.

That is, a nohup.out file will be generated in the current directory if executed as follows.

nohup java -jar usercenter.jar --spring.profiles.active=development &
Copy the code

However, if we need to start multiple projects in the current directory, the project startup logs will be output to the nohup.out file, which is not good for us to view the logs. In this case, we may need to do log redirection to separate the output of different projects. This also helps us to distinguish the log output of different items by file name when we log in to the server again.

So we used the following command:

nohup java -jar usercenter.jar --spring.profiles.active=development > usercenter.log 2>&1 &
Copy the code

To better understand the command above, let’s first understand the concept of terminal input/output:

0< -- stdin (standard input), short < 1> -- stdout (standard output), short > 2> -- stderr (standard error)Copy the code

For this command, the java-jar part in the middle is naturally the project startup command to execute. Nohup and the & at the end indicate that the java-jar command is executed in the background without hanging up, even if the command continues to execute after the terminal exits. > usercenter.log redirects the standard output of the command to the usercenter.log file, and 2>&1 redirects the standard error output to the standard output.

Of course, if we don’t care about the output of the command execution and don’t want the default generated nohup.out file, we can redirect the output to /dev/null, which is a special file that accepts and discards all input written to it. This is usually useful for situations where a large number of log outputs are likely to be generated, preventing log files from taking up a large amount of space.

Wechat public number: Rookiedev, Java background development, inspirational lifelong learning, adhere to the output of original dry goods, you can choose to pay attention to me now, or look at the historical article and then pay attention to it. Long press the QR code to pay attention, we work together to become better!