shell腳本如何查詢進(jìn)程并殺死
shell腳本查詢進(jìn)程并殺死
說明:
工作中經(jīng)常需要寫一個定時腳本,需要找到一個進(jìn)程,然后殺死,并定時重新啟動這個進(jìn)程。
具體腳本如下:(ngsp代表是什么進(jìn)程)
#!/bin/bash
ID=`ps -ef | grep ngsp | grep -v grep | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "kill $id"
done或者有些朋友看了,不明白,我這里就舉一個httpd的例子吧
1. 安裝一個apache服務(wù)
yum -y install httpd systemctl start httpd

網(wǎng)頁訪問:IP:80 比如1.117.92.32:80 看到網(wǎng)頁說明httpd部署成功了。

查看httpd的服務(wù):

如下是腳本殺死進(jìn)程和啟動腳本
[root@VM-4-11-centos ~]# ls
kill_httpd.sh start_httpd.sh
[root@VM-4-11-centos ~]# cat kill_httpd.sh
#!/bin/bash
ID=`ps -ef | grep httpd | grep -v grep | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "kill $id"
done
[root@VM-4-11-centos ~]# cat start_httpd.sh
#!/bin/bash
systemctl start httpd
[root@VM-4-11-centos ~]# 設(shè)置定時啟動腳本:crontab -e進(jìn)行編輯
[root@VM-4-11-centos ~]# crontab -l #查看定時任務(wù),我的腳本在 /root/路徑下,我這是每天10點50運(yùn)行殺死進(jìn)程,然后每天10點52重新啟動httpd服務(wù) */5 * * * * flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &' 50 10 * * * /root/kill_httpd.sh 52 10 * * * /root/start_httpd.sh [root@VM-4-11-centos ~]#
到此這篇關(guān)于shell腳本查詢進(jìn)程并殺死的文章就介紹到這了,更多相關(guān)shell查詢進(jìn)程并殺死內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用命令行將json數(shù)據(jù)導(dǎo)出到csv(一行命令搞定)
這篇文章主要為大家介紹了使用命令行將json數(shù)據(jù)導(dǎo)出到csv,一行命令搞定的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Shell調(diào)用curl實現(xiàn)IP歸屬地查詢的腳本
這篇文章主要介紹了Shell調(diào)用curl實現(xiàn)IP歸屬地查詢,文中給大家提到了查詢IP歸屬地的shell腳本,在批量查找數(shù)據(jù)的時候經(jīng)常會遇到,今天給大家分享出來,需要的朋友可以參考下2021-07-07
linux shell之通過標(biāo)識測試文件系統(tǒng)屬性的方法示例
今天小編就為大家分享一篇關(guān)于linux shell之通過標(biāo)識測試文件系統(tǒng)屬性的方法示例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04

