Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this?

Somebody is bound to help you if you show some effort upfront.

What have you tried ?

Regards
Peasant.

1 Like

Thanks for the response. I am new to UNIX scripting. I am trying to put all the commands together and write a script.

#!/bin/sh
find /data  -type f -size +1G > $file
ps -ef | grep $file | awk '{print $2}' > $PID
 kill -9 $PID

Your specification is - even more than - a bit vague. Please indicate how frequently the file size should be checked. By using a loop / crontab entry? Report to whom / how? Are you sure the identified process may be killed?

Thank You for your reply. The script should be executed every few minutes and I can schedule it in crontab. It should report the PID, source of the file and also the job should be killed and the file should be deleted.

------ Post updated at 05:14 PM ------

so i tried and was able to get here. Any feedback or changes

#!/bin/sh
filename='bigfiles'
find /opt/karthik -type f -size +200M > $filename
count='cat bigfiles | wc -l'

#while read LINE; do echo "$LINE" |

while read -r line;do [ -n"$line" ] &&

pid= ps -ef | grep -v grep |  grep $(basename "$line") | awk '{print $2}' >> pid.txt
kill $(ps -ef | grep -v grep | grep $(basename "$line") | awk '{print $2}'); rm -- $line
done < $filename

------ Post updated at 05:42 PM ------

Instead of ps i want to replace with something which can find the process ID of the process creating the file. Any idea?