shell脚本,检测任务状态为停止,就重启
时间: 2023-10-17 08:16:36 浏览: 35
可以使用以下脚本实现:
```bash
#!/bin/bash
# 检查任务状态
task_status=$(systemctl is-active your-service-name)
# 如果任务状态为停止,重启任务
if [ "$task_status" = "inactive" ]; then
systemctl restart your-service-name
fi
```
你需要将 `your-service-name` 替换为你要检测和重启的任务名称。这个脚本会先检查任务的状态,如果状态为停止(`inactive`),就会执行重启操作。
相关问题
shell脚本检测nginx的运行状态
可以使用如下的Shell脚本检测nginx的运行状态:
```
#!/bin/bash
nginx_status=$(systemctl status nginx | grep Active | awk '{print $2}')
if [ "$nginx_status" == "(running)" ]; then
echo "Nginx is running"
else
echo "Nginx is not running"
fi
```
这个脚本通过systemctl命令获取nginx的状态,然后使用grep和awk命令提取出状态信息,并且判断nginx是否在运行。如果nginx正在运行,输出"Nginx is running",否则输出"Nginx is not running"。
jar、war包启动、停止、重启shell脚本
启动、停止、重启jar、war包可以通过以下的shell脚本实现:
启动脚本:
```
#!/bin/bash
# 启动jar包
nohup java -jar /path/to/your/project.jar > /dev/null 2>&1 &
echo "项目已启动"
```
将`/path/to/your/project.jar`替换为你的jar包所在的路径,然后将脚本保存为`start.sh`。
执行`./start.sh`即可启动jar包。
停止脚本:
```
#!/bin/bash
# 停止jar包
pid=$(ps -ef | grep /path/to/your/project.jar | grep -v grep | awk '{print $2}')
if [ -n "$pid" ]; then
kill -9 $pid
echo "项目已停止"
else
echo "项目未启动"
fi
```
将`/path/to/your/project.jar`替换为你的jar包所在的路径,然后将脚本保存为`stop.sh`。
执行`./stop.sh`即可停止jar包。
重启脚本:
```
#!/bin/bash
# 重启jar包
./stop.sh
./start.sh
```
将`stop.sh`和`start.sh`保存在同一目录下,然后将脚本保存为`restart.sh`。
执行`./restart.sh`即可重启jar包。
相关推荐
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)