Shell 脚本实现自动化循环删除进程
程序员文章站
2023-12-28 11:52:34
...
问题:
服务器中了木马,每隔几十秒就会自动启动一个脚本进程,占用CPU
解决方式:
写一个 shell 脚本,定时清理木马进程
# kill_macron.sh
#!/bin/bash
sleep_time=10
int=1
while(($int<1000))
do
echo "===== `date` ====="
pid=`ps -aux |grep "Macron"|grep -v "grep" |awk '{print $2}'`
wc=`ps -aux |grep "Macron"|grep -v "grep" |awk '{print $2}'|wc -l`
#echo "wc = " $wc
if [ $wc -ne "0" ];then
echo "kill -9 $pid"
kill -9 $pid
fi
sleep $sleep_time
done
再写启动进程脚本以及停止进程脚本:
# start.sh
# /bin/bash
nohup sh ./kill_macron.sh > kill_log.log &
tail -f ./kill_log.log
# stop.sh
# /bin/bash
pid=`ps -aux |grep "kill_macron"|grep -v "grep" |awk '{print $2}'`
wc=`ps -aux |grep "kill_macron"|grep -v "grep" |awk '{print $2}'|wc -l`
if [ $wc -ne "0" ];then
echo "kill -9 $pid"
kill -9 $pid
fi
ps -ef|grep "kill_macron"