개발/linux

[linux] 리눅스 백그라운드 프로세스 명령 실행 ( & )

mabb 2023. 8. 15. 16:33
반응형

다음과 같이 Java 바이트 코드를 실행하는 스크립트에서 마지막의 &(ampersand) 기호의 의미가 무엇인지 알아보았다.
( jvm 옵션들도 정리해야겠다 ) 

java
-Xms512 m
-Xmx4096 m
-verbose:gc
-XX:+UseParallelGC
-XX:+UseParallelOldGC
-XX:MaxGCPauseMillis=20
-XX:InitiatingHeapOccupancyPercent=50
-XX:NewRatio=2
-XX:SurvivorRatio=8
-XX:G1 ReservePercent=10
-XX:+CMSClassUnloadingEnabled
-XX:+CMSPermGenSweepingEnabled
-Djava.security.egd=file:/dev/./urandom
package.Test &

 

java 명령어의 시놉시스를 보고 &을 명령행 인자로 제공하는 것인가 하였으나 직접 테스트해보니 &은 명령행 인자로 들어가지 않았다.

man java

java(1)
Basic Tools java(1)

NAME
           java - Launches a Java application.

SYNOPSIS
                     java [options] classname [args]
                     java [options] -jar filename [args]
           options
                     Command-line options separated by spaces. See Options.
           classname
                     The name of the class to be launched.
           filename
                     The name of the Java Archive (JAR) file to be called. Used only with the -jar option.
           args
                     The arguments passed to the main() method separated by spaces.

DESCRIPTION

           The java command starts a Java application. It does this by starting the Java Runtime Environment (JRE),              loading the specified class, and calling that class's main() method. The method must be declared public                  and  static, it must not return any value, and it must accept a String array as a parameter. The method                    declaration has the following form:

                     public static void main(String[] args)

           The java command can be used to launch a JavaFX application by loading a class that either has a main()             method or that extends javafx.application.Application. In the latter case, the launcher constructs an instance            of the Application class, calls its init() method, and then calls the start(javafx.stage.Stage) method. By                      default, the first argument that is not an option of the java command is the fully qualified name of the class              to be called. If the -jar option is specified, its argument is the name of the JAR file containing class and                    resource files for the application. The startup class must be indicated by the Main-Class manifest header in            its source code.


                     .............

 

phind에 질문해보았다. 명령어의 마지막에 &(ampersand)를 붙일 경우 해당 명령어는 Background 프로세스로 실행되고 즉시 프롬프트를 실행할 수 있다고 한다.  &는 Java 명령어의 옵션이 아니고 Linux 명령어였다.

Q. java Test & what is &
A.

The & symbol at the end of a command in Unix-like operating systems,
including Linux, is used to run the preceding command in the background.
When a command is run in the background, the shell does not wait for the command to finish, and immediately proceeds to the next line or command prompt.

Here is a simple example: If you have a Java program named Test, and you run the command java Test &, the Test program will start running in the background. This means you can continue to use your terminal for other commands while Test is still running howtogeek.com.
To understand this better, let's break it down:

java Test: This part of the command is straightforward. java is the command to start the Java Virtual Machine (JVM), and Test is the name of the Java class you want to run.

&: This is the key part we're focusing on. When you put an ampersand (&) at the end of a command, it tells the shell to run the command in the background.

The shell will start the command as a background process and then immediately return you to the command prompt. You'll be able to continue entering other commands while the background process runs.
When you start a process in the background, the shell will print a process ID that you can use to refer to the process in other commands. For example, you can use the kill command with this process ID to stop the process if needed

 

while(true) 같은 무한 반복문이 포함된 Java 바이트코드를 백그라운드로 실행하면 정말로 명령 프롬프트가 즉시 반환되는지 Test를 해본다.

 

mobaXterm 으로 사내 서버를 활용하여 테스트해본다. Test.java를 작성한다.

 

컴파일
무한 반복문인데 명령어 프롬프트가 작동한다.

 

PID를 반환한다. 백그라운드에서 실행되기 때문에 관리에 용이하도록 PID를 반환하는 것으로 보인다.

 

프로세스가 실행되고 있다.

kill -9 151274

프로세스를 종료시켰다.

명령어를 백그라운드로 실행하지 않으면 원래 다음과 같이 된다. Ctrl + c로 실행을 취소해주어야 한다.

java Test

 

반응형