前言: 下面操作都是基于 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