把进程放入后台运行

以下以一个 java 应用做示例

java -jar target/iep.jar &:把命令放入后台运行,应用的输出会不断的打印到当前终端,关闭终端后应用会停止运行

  1. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar &
  2. [1] 5564
  3. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# ls
  4. 404.html lib logs README.md ruoyi.iml sql target
  5. index.html LICENSE pom.xml restart.sh ry.sh src uploadPath
  6. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# 19:54:57.776 [main] INFO c.r.RuoYiApplication - [logStarting,50] - Starting RuoYiApplication v4.4.0 on iZwz91sneajs2ji49vo4wxZ with PID 5564 (/www/wwwroot/iep-test.ca163.com/target/iep.jar started by root in /www/wwwroot/iep-test.ca163.com)
  7. 19:54:57.779 [main] DEBUG c.r.RuoYiApplication - [logStarting,53] - Running with Spring Boot v2.1.17.RELEASE, Spring v5.1.18.RELEASE
  8. 19:54:57.779 [main] INFO c.r.RuoYiApplication - [logStartupProfileInfo,652] - The following profiles are active: druid
  9. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# 19:55:01.606 [main] WARN n.s.e.s.d.DiskStorageFactory - [<init>,172] - The index for data file /tmp/shiro-active%0053ession%0043ache.data is out of date, probably due to an unclean shutdown. Deleting index file /tmp/shiro-active%0053ession%0043ache.index
  10. 19:55:01.629 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache' does not yet exist. Creating now.
  11. 19:55:01.629 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]
  12. 19:55:03.372 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]
  13. 19:55:03.711 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [sys-userCache]
  14. 19:55:04.235 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Initializing ProtocolHandler ["http-nio-8880"]
  15. 19:55:04.255 [main] INFO o.a.c.c.StandardService - [log,173] - Starting service [Tomcat]
  16. 19:55:04.255 [main] INFO o.a.c.c.StandardEngine - [log,173] - Starting Servlet engine: [Apache Tomcat/9.0.38]
  17. 19:55:04.428 [main] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring embedded WebApplicationContext
  18. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# 19:55:05.260 [main] INFO o.q.i.StdSchedulerFactory - [instantiate,1220] - Using default implementation for ThreadExecutor
  19. 19:55:05.285 [main] INFO o.q.c.SchedulerSignalerImpl - [<init>,61] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
  20. 19:55:05.286 [main] INFO o.q.c.QuartzScheduler - [<init>,229] - Quartz Scheduler v.2.3.2 created.
  21. 19:55:06.087 [main] INFO c.a.d.p.DruidDataSource - [init,994] - {dataSource-1} inited
  22. 19:55:06.103 [main] INFO o.q.c.QuartzScheduler - [initialize,294] - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'RuoyiScheduler' with instanceId 'iZwz91sneajs2ji49vo4wxZ1603022105264'
  23. Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.
  24. ....

java -jar target/iep.jar:输入此命令后按下 ctrl + z,应用会在后台暂停

查看后台工作

jobs -l:查看后台运行的工作,+ 代表最近一个放入后台的工作,- 代表倒数第二个放入后台的工作

  1. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar
  2. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar
  3. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar
  4. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l
  5. [1] 6476 停止 java -jar target/iep.jar
  6. [2]- 6718 停止 java -jar target/iep.jar
  7. [3]+ 6756 停止 java -jar target/iep.jar

恢复后台工作

fg 工作号:将后台暂停的工作恢复到前台执行,恢复到前台执行意味着关闭终端程序将终于,注意工作号不是进程号

  1. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l
  2. [1] 6476 停止 java -jar target/iep.jar
  3. [2]- 6718 停止 java -jar target/iep.jar
  4. [3]+ 6756 停止 java -jar target/iep.jar
  5. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# fg 3
  6. java -jar target/iep.jar
  7. 20:05:52.375 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache' does not yet exist. Creating now.
  8. 20:05:52.378 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]
  9. 20:05:54.132 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]
  10. ....

bg 工作号:将后台暂停的工作恢复到后台执行,关闭终端后应用会停止运行

  1. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l
  2. [1]- 6476 停止 java -jar target/iep.jar
  3. [2]+ 6718 停止 java -jar target/iep.jar
  4. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# bg 1
  5. [1]- java -jar target/iep.jar &
  6. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# 20:08:38.450 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache' does not yet exist. Creating now.
  7. 20:08:38.452 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]
  8. 20:08:40.101 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]
  9. ....

nohup 命令

nohup 应用启动命令 > 输出日志路径 &:让应用在后台执行,关闭终端也不会停止运行
后台运行应用程序的正确操作:使用 nohup 在后台运行,使用 tail 查看输出日志

  1. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# nohup java -jar target/iep.jar > nohup.log &
  2. [1] 9008
  3. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# nohup: 忽略输入重定向错误到标准输出端
  4. [root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# tail -f nohup.log
  5. 20:20:16.131 [main] INFO c.r.RuoYiApplication - [logStarting,50] - Starting RuoYiApplication v4.4.0 on iZwz91sneajs2ji49vo4wxZ with PID 9008 (/www/wwwroot/iep-test.ca163.com/target/iep.jar started by root in /www/wwwroot/iep-test.ca163.com)
  6. 20:20:16.135 [main] DEBUG c.r.RuoYiApplication - [logStarting,53] - Running with Spring Boot v2.1.17.RELEASE, Spring v5.1.18.RELEASE
  7. 20:20:16.136 [main] INFO c.r.RuoYiApplication - [logStartupProfileInfo,652] - The following profiles are active: druid
  8. 20:20:20.252 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,158] - Cache with name 'com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache' does not yet exist. Creating now.
  9. 20:20:20.254 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]
  10. 20:20:21.796 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]