Linux(CentOS7)后台运行程序关掉窗口不被杀掉

前言: 下面操作都是基于 CentOS7 操作的



后台运行命令

  • nohup 执行脚本或者执行命令 > 日志的输出路径 &

注意:日志文件不需要提前创建(自动创建),但是目录一定是存在的目录才行(不会自动创建)

脚本执行

# 创建 app.jar.sh 文件并写入命令
echo "java -jar app.jar" >> app.jar.sh

# 执行脚本
nohup ./app.jar.sh > app.jar.log &

# 如果报没有权限就加上 bash
[root@VM-0-5-centos temp]# nohup: ignoring input and redirecting stderr to stdout
nohup: failed to run command ‘./app.jar.sh’: Permission denied

[1]+  Exit 126                nohup ./app.jar.sh > app.sh.log

# 加上bash执行脚本, 看到输出pid就成功了, 然后按回车就行
nohup bash ./app.jar.sh > app.sh.logs &

命令执行

# 输出日志到当前目录的 app.jar.log 文件 (日志文件不需要提前创建),执行完之后直接按回车就行
nohup java -jar app.jar > app.jar.log &
# 把日志输出到 home 目录下的 app.jar.log
nohup java -jar app.jar > /home/app.jar.log &


###################执行过程###################
[root@VM-0-5-centos temp]# nohup java -jar app.jar > app.jar.log &
[1] 1690 # 进程id
[root@VM-0-5-centos temp]# nohup: ignoring input and redirecting stderr to stdout

# 查看所有的java进程,1690就是刚才后台运行的java服务
[root@VM-0-5-centos temp]# ps -aux|grep java
root      1690  100 11.4 2530680 214908 pts/0  Sl   11:05   0:16 java -jar app.jar
root      1769  0.0  0.0 112708   976 pts/0    S+   11:06   0:00 grep --color=auto java

# 杀掉刚才启动的java后台进程
[root@VM-0-5-centos temp]# kill 1690

监控产生的日志文件

# tail -f 日志文件
tail -f app.jar.log

查看所有后台进程

# 查看所有进程
ps -aux

# 查看指定进程
ps -aux|grep xxx

# 查看端口是否被占用
netstat -anp|grep 端口号
netstat -anp|grep 7078

结束进程

kill pid

# 结束进程id为1690的进程
kill 1690

根据进程id查看程序启动路径

命令:ls -l /proc/进程id

# 查看进程
[root@localhost ~]# ps -ef|grep nginx
root      7622 31541  0 15:24 pts/1    00:00:00 grep --color=auto nginx
nobody   13138 13609  0 5▒▒07 ?        00:00:56 nginx: worker process
root     13609     1  0 5▒▒06 ?        00:00:00 nginx: master process ./nginx

# 根据进程id查看进程信息
[root@localhost ~]#ls -l /proc/13609

在这里插入图片描述

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值