Linux上重启服务的正确命令

简介: 在开发环境下,我们经常需要部署代码,重启服务,所以会把命令写在脚本中,方便使用。我们可能这么写#!/bin/bashps -ef | grep backend-api-1.

在开发环境下,我们经常需要部署代码,重启服务,所以会把命令写在脚本中,方便使用。

我们可能这么写

#!/bin/bash

ps -ef | grep backend-api-1.0 | grep -v "\-\-color" |awk '{print $2}' |xargs kill -9
sleep 1
nohup java -jar backend-api-1.0.jar >> backend-api.log 2>&1 &
tail -f backend-api.log

这个脚本看似没问题,可执行时,会出现kill: 向 PID 发送信号失败: 没有那个进程的错误,但是服务仍然能正常重启,也就是ps -ef | grep backend-api-1.0 | grep -v "\-\-color"这个命令并没有把当前执行进程过滤掉。

正确写法是:取项目名中的两个不同的单词,用两次grep,而不是用grep -v,如下

#!/bin/bash

ps -ef | grep backend | grep api |awk '{print $2}' |xargs kill -9
sleep 1
nohup java -jar backend-api-1.0.jar >> backend-api.log 2>&1 &
tail -f backend-api.log
目录
相关文章
|
1天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
190 85
|
6天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
4天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
2天前
|
机器学习/深度学习 存储 Linux
linux中强大且常用命令:find、xargs、grep
linux中强大且常用命令:find、xargs、grep
24 9
|
2天前
|
SQL 移动开发 Linux
linux下find、grep命令详解
linux下find、grep命令详解
35 8
|
8天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
9天前
|
存储 Linux 编译器
linux中vim介绍以及常用命令大全
linux中vim介绍以及常用命令大全
31 8
|
5天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘
|
7天前
|
设计模式 Java Linux
Linux的20个常用命令
Linux的23个常用命令
Linux的20个常用命令
|
9天前
|
Linux
linux关机重启
linux关机重启 登录注销 shutdown -h now 立刻关机 shutdown -h 1 一分钟后重启 shutdown -r now 重启计算机 halt 关机 reboot 重启计算机 sync 内存数据同步到磁盘