未分类
定时清理垃圾进程
自动清除脚本: 具体是检测cron文件如果有修改并有此进程便进行清理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#!/bin/bash (rpm -qa | grep inotify-tools > /dev/null 2>&1) || yum install -y inotify-tools cron="/var/spool/cron/root" log="/data/scripts/monitor.log" inotifywait -mqr -e modify --timefmt '%Y-%m-%d %H:%M:%S' --format '%w %f %e %T' $cron | \ while read file; do if grep 'systemd-service.sh' $file; then file_name=$(echo $file | awk '{print $6}') rm -rf "$file_name" sed -i 's/*.systemd-service.sh.*//g' $file echo '----'$(date)'----' >> $log #process_info=$(ps -eo pid,ppid,%mem,%cpu,cmd --sort=-%cpu | head -n2) process_info=$(ps -eo pid,ppid,%mem,%cpu,comm --sort=-%cpu | head -n2) echo "$process_info" >> $log pid=$(echo "$process_info"|grep -v 'PID'|awk '{print $1}') kill -9 $pid echo "---- killed $pid ----" >> $log fi done |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
cat > /etc/systemd/system/monitor.service <<EOF [Unit] Description=Monit cron Service After=network.target Wants=network.target [Service] Type=simple ExecStart=/data/scripts/monitor.sh Restart=on-failure [Install] WantedBy=multi-user.target EOF |
1 2 |
systemctl daemon-reload systemctl start monitor |