把进程放入后台运行
以下以一个 java 应用做示例
java -jar target/iep.jar &:把命令放入后台运行,应用的输出会不断的打印到当前终端,关闭终端后应用会停止运行
[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar &[1] 5564[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# ls404.html lib logs README.md ruoyi.iml sql targetindex.html LICENSE pom.xml restart.sh ry.sh src uploadPath[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)19:54:57.779 [main] DEBUG c.r.RuoYiApplication - [logStarting,53] - Running with Spring Boot v2.1.17.RELEASE, Spring v5.1.18.RELEASE19:54:57.779 [main] INFO c.r.RuoYiApplication - [logStartupProfileInfo,652] - The following profiles are active: druid[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.index19: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.19:55:01.629 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]19:55:03.372 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]19:55:03.711 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [sys-userCache]19:55:04.235 [main] INFO o.a.c.h.Http11NioProtocol - [log,173] - Initializing ProtocolHandler ["http-nio-8880"]19:55:04.255 [main] INFO o.a.c.c.StandardService - [log,173] - Starting service [Tomcat]19:55:04.255 [main] INFO o.a.c.c.StandardEngine - [log,173] - Starting Servlet engine: [Apache Tomcat/9.0.38]19:55:04.428 [main] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring embedded WebApplicationContext[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# 19:55:05.260 [main] INFO o.q.i.StdSchedulerFactory - [instantiate,1220] - Using default implementation for ThreadExecutor19:55:05.285 [main] INFO o.q.c.SchedulerSignalerImpl - [<init>,61] - Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl19:55:05.286 [main] INFO o.q.c.QuartzScheduler - [<init>,229] - Quartz Scheduler v.2.3.2 created.19:55:06.087 [main] INFO c.a.d.p.DruidDataSource - [init,994] - {dataSource-1} inited19:55:06.103 [main] INFO o.q.c.QuartzScheduler - [initialize,294] - Scheduler meta-data: Quartz Scheduler (v2.3.2) 'RuoyiScheduler' with instanceId 'iZwz91sneajs2ji49vo4wxZ1603022105264'Scheduler class: 'org.quartz.core.QuartzScheduler' - running locally.....
java -jar target/iep.jar:输入此命令后按下 ctrl + z,应用会在后台暂停
查看后台工作
jobs -l:查看后台运行的工作,+ 代表最近一个放入后台的工作,- 代表倒数第二个放入后台的工作
[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# java -jar target/iep.jar[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l[1] 6476 停止 java -jar target/iep.jar[2]- 6718 停止 java -jar target/iep.jar[3]+ 6756 停止 java -jar target/iep.jar
恢复后台工作
fg 工作号:将后台暂停的工作恢复到前台执行,恢复到前台执行意味着关闭终端程序将终于,注意工作号不是进程号
[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l[1] 6476 停止 java -jar target/iep.jar[2]- 6718 停止 java -jar target/iep.jar[3]+ 6756 停止 java -jar target/iep.jar[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# fg 3java -jar target/iep.jar20: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.20:05:52.378 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]20:05:54.132 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]....
bg 工作号:将后台暂停的工作恢复到后台执行,关闭终端后应用会停止运行
[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# jobs -l[1]- 6476 停止 java -jar target/iep.jar[2]+ 6718 停止 java -jar target/iep.jar[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# bg 1[1]- java -jar target/iep.jar &[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.20:08:38.452 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]20:08:40.101 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]....
nohup 命令
nohup 应用启动命令 > 输出日志路径 &:让应用在后台执行,关闭终端也不会停止运行
后台运行应用程序的正确操作:使用 nohup 在后台运行,使用 tail 查看输出日志
[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# nohup java -jar target/iep.jar > nohup.log &[1] 9008[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# nohup: 忽略输入重定向错误到标准输出端[root@iZwz91sneajs2ji49vo4wxZ iep-test.ca163.com]# tail -f nohup.log20: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)20:20:16.135 [main] DEBUG c.r.RuoYiApplication - [logStarting,53] - Running with Spring Boot v2.1.17.RELEASE, Spring v5.1.18.RELEASE20:20:16.136 [main] INFO c.r.RuoYiApplication - [logStartupProfileInfo,652] - The following profiles are active: druid20: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.20:20:20.254 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,165] - Added EhCache named [com.ruoyi.framework.shiro.realm.UserRealm.authorizationCache]20:20:21.796 [main] INFO o.a.s.c.e.EhCacheManager - [getCache,169] - Using existing EHCache named [loginRecordCache]
