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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to report file size, pid and also kill the process
# 1  
Old 07-30-2018
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?
# 2  
Old 07-30-2018
Somebody is bound to help you if you show some effort upfront.

What have you tried ?

Regards
Peasant.
This User Gave Thanks to Peasant For This Post:
# 3  
Old 07-30-2018
Thanks for the response. I am new to UNIX scripting. I am trying to put all the commands together and write a script.

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



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-30-2018 at 03:46 PM.. Reason: Added CODE tags.
# 4  
Old 07-30-2018
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?
# 5  
Old 07-30-2018
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

Code:
#!/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?

Last edited by Don Cragun; 07-30-2018 at 07:00 PM.. Reason: Add CODE tags again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

By pass a process in a Shell Script on file size

I wish to by pass a process if the file is over a certain size? not sure this makes sense current bit of the script below #if we are bypAssing the OCR if ; then echo Bypassing HOTFOLDER OCR HOT_FOLDER_DIR=$BATCH_POST_OCR_DIR; potential change below? would this work would I need... (1 Reply)
Discussion started by: worky
1 Replies

2. Shell Programming and Scripting

Kill nohup process with changing PID

Hi there! I have a tricky problem concerning a nohup process: I started a python2.7 script which loops over a function. At the end it restarts the function. Due to a mistake I'm now having a never ending nohup process that I have to kill. I started the program execution with: >>nohup... (4 Replies)
Discussion started by: Lydia
4 Replies

3. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

4. Red Hat

How to Force KILL State -D Process/PID?

Hi Expert, I am not able to kill certain user process as root. I have tried using: pkill -u uname skill KILL -u uname kill -9 PID *** I have not using killall yet, since this server has more than 100 users online atm. PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND... (1 Reply)
Discussion started by: regmaster
1 Replies

5. Shell Programming and Scripting

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (4 Replies)
Discussion started by: ninadgac
4 Replies

6. UNIX for Dummies Questions & Answers

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (1 Reply)
Discussion started by: ninadgac
1 Replies

7. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

8. Shell Programming and Scripting

grab PID of a process and kill it in a script

#!/bin/sh who echo "\r" echo Enter the terminal ID of the user in use: echo "\r" read TERM_ID echo "\r" ps -t $TERM_ID | grep sh echo "\r" echo Enter the process number to end: echo "\r" read PID echo "\r" kill -9 $PID What this code does is ultimately grab the PID of a users sh... (6 Replies)
Discussion started by: psytropic
6 Replies

9. Shell Programming and Scripting

How to kill a process and its childs given pid and userid

I need to write a shell script which would take 2 arguments pid , userid. Then it should kill all the child process under it. If a child process is not killed then it should wait for 1 minute and should kill. can anybody give me the idea to write it? (0 Replies)
Discussion started by: nani_g
0 Replies

10. UNIX for Dummies Questions & Answers

Script to kill all child process for a given PID

Is there any build in command in unix to kill all the child process for a given process ID ? If any one has script or command, please let me know. Thanks Sanjay (4 Replies)
Discussion started by: sanjay92
4 Replies
Login or Register to Ask a Question