nohup

  1. nohup 指令 - 背景執行指令
    1. Run process on backgrund
    2. See process log
    3. Kill background process

nohup 指令 - 背景執行指令

於背景執行Linux命令直到完成後才會結束, 不會影響使用者的正常工作。

nohup = “no hangup”

$ nohup your-command &

nohup 預設會將指令的輸出放到 nohup.out 的檔案,如果需要自訂重新導向的檔案, 可以用下面方法重新導向到檔案

$ nohup your-domain > nohup.log 2>&1 &

這樣就會將指令的輸出放到 nohup.log 內。


Run process on backgrund

$ nohup mvn spring-boot:run &
[1] 84534
appending output to nohup.out
$ jobs -l
[1]  + 84534 running    nohup mvn spring-boot:run

$ nohup java -jar project.jar –server.port=port &

$ ls
./                    .gitignore            .mvn/                 mvnw*                 nohup.out             springbootdocker.iml  target/
../                   .idea/                HELP.md               mvnw.cmd              pom.xml               src/

See process log

$ cat nohup.out
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< pri.anc:springbootdocker >----------------------
[INFO] Building springbootdocker 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) > test-compile @ springbootdocker >>>
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:resources (default-resources) @ springbootdocker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ springbootdocker ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.1.0:testResources (default-testResources) @ springbootdocker ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/alan/Documents/springbootdocker/src/test/resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ springbootdocker ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] <<< spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) < test-compile @ springbootdocker <<<
[INFO]
[INFO]
[INFO] --- spring-boot-maven-plugin:2.1.3.RELEASE:run (default-cli) @ springbootdocker ---
[INFO] Attaching agents: []

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2019-03-31 02:59:17.996  INFO 84554 --- [  restartedMain] p.a.s.SpringbootdockerApplication        : Starting SpringbootdockerApplication on smartcaredeMacBook-Air.local with PID 84554 (/Users/alan/Documents/springbootdocker/target/classes started by alan in /Users/alan/Documents/springbootdocker)
2019-03-31 02:59:18.004  INFO 84554 --- [  restartedMain] p.a.s.SpringbootdockerApplication        : No active profile set, falling back to default profiles: default
2019-03-31 02:59:18.252  INFO 84554 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2019-03-31 02:59:18.253  INFO 84554 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2019-03-31 02:59:22.233  INFO 84554 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.hateoas.config.HateoasConfiguration' of type [org.springframework.hateoas.config.HateoasConfiguration$$EnhancerBySpringCGLIB$$458abca2] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-03-31 02:59:24.009  INFO 84554 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-03-31 02:59:24.158  INFO 84554 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-03-31 02:59:24.161  INFO 84554 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2019-03-31 02:59:24.205  INFO 84554 --- [  restartedMain] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/alan/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-03-31 02:59:24.648  INFO 84554 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-31 02:59:24.650  INFO 84554 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 6396 ms
2019-03-31 02:59:26.342  INFO 84554 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-03-31 02:59:27.159  INFO 84554 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2019-03-31 02:59:27.376  INFO 84554 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-03-31 02:59:27.389  INFO 84554 --- [  restartedMain] p.a.s.SpringbootdockerApplication        : Started SpringbootdockerApplication in 11.186 seconds (JVM running for 12.608)

Kill background process

$ jobs -l
[1]  + 84534 running    nohup mvn spring-boot:run
$ kill -9 84534
[1]  + 84534 killed     nohup mvn spring-boot:run
$ jobs -l