Operating System - Linux
1753771 Members
4566 Online
108799 Solutions
New Discussion юеВ

Re: script to check and kill procs

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

script to check and kill procs

Hello,

I am looking for a small script that will fuser a bunch of files in a directory then the files with no process' writing I want to list / remove.

any ideas?

Thanks
hello
5 REPLIES 5
Muthukumar_5
Honored Contributor
Solution

Re: script to check and kill procs

Use can try as,

#!/bin/ksh
DIR="/tmp"

for file in `find $DIR -type f`
do
pid=$(fuser -u $file 2>/dev/null)
if [ -z $pid ]
then
echo "$file is not in use. Do you want to remote (Y/N)"
rm -i $file
fi
done

# END

hth.
Easy to suggest when don't know about the problem!
RAC_1
Honored Contributor

Re: script to check and kill procs

for i in $(< file_list)
do
pid=$(/usr/sbin/fuser -u ${i} 2>/dev/null)
if [[ ${pid} -eq "" ]];then
echo "File - ${i} can be killed"
else
echo "File $(i) is being accesses by ${pid}"
echo "Can't be removed"
done
There is no substitute to HARDWORK
Kent Ostby
Honored Contributor

Re: script to check and kill procs

I've used RAC's logic to find the files.

This script would create a script full of rm statements that you could then run.

touch /tmp/dascript
rm /tmp/dascript
touch /tmp/dascript
for i in $(< file_list)
do
pid=$(/usr/sbin/fuser -u ${i} 2>/dev/null)
if [[ ${pid} -eq "" ]];then
echo "rm " $i > /tmp/dascript
fi
done
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Kent Ostby
Honored Contributor

Re: script to check and kill procs

Oops .. an error in that. Here's the repost:

touch /tmp/dascript
rm /tmp/dascript
touch /tmp/dascript
for i in $(< file_list)
do
pid=$(/usr/sbin/fuser -u ${i} 2>/dev/null)
if [[ ${pid} -eq "" ]];then
echo "rm " $i >> /tmp/dascript
fi
done
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
lawrenzo_1
Super Advisor

Re: script to check and kill procs

ty very much
hello