I need to kill the processes older then then one day
Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
for proc in $(find /proc -maxdepth 1 -type d -name "[0-9]*" -mmin +1440); do
pid=$(basename ${proc})
user=$(stat -c %U ${proc})
if [ "${user}" = "root" ]; then
continue
else
kill ${pid}
fi
done
With this you can find all processes which are one or more days older and then you can check if they belong or not to use root, if they don't you can kill them.
And using this little idea you can do whatever you want or need.
Actually this script is not a totally good idea, coz it will kill all processes which are not started with root user, and in most cases there will be some processes running with different user then root which you shouldn't kill.
Code:
for proc in $(find /proc -maxdepth 1 -type d -name "[0-9]*" -mmin +1440); do
pid=$(basename ${proc})
uid=$(stat -c %u ${proc})
if [ ${user} -qe 500 ]; then
kill ${pid}
fi
done
So this new example will kill all processes which belong to users with a id bigger or equal to 500, which shouldn't be any "special" system user.
Use ALL-CAPS variable names for readability, use "while" loop instead, speed up by excluding root-owned processes, speed up by avoiding basename and stat, derive value of 500 from /etc/login.defs, check shell, it's not "-qe" but "-ge" BTW and allow process to end before killing it:
to obtain the PID, elapsed time (age), and command line for each SSH process. The elapsed time is MM:SS if less than a hour, HH:MM:SS if less than a day, and DD-HH:MM:SS if a full day or longer.
Then, you need to ignore the process which matches command /usr/sbin/sshd, because that is the listening server, not an SSH connection. You also need to ignore the processes that have not run long enough yet, and send a termination signal (TERM or KILL) to the processes that have run long enough.
Once again, I'd use awk for this. Using awk in a Bash script:
Code:
export LC_ALL=C
export LANG=C
PIDS=$(ps -o pid= -o etime= -o cmd= -C sshd | awk '#
# Remove leading whitespace if necessary.
($1 ~ /^[\t\v\f ]*$/) {
sub(/^[\t\v\f ]*/, "", $0)
}
# Skip the listening SSH server process.
($3 == "/usr/sbin/sshd") { next }
# Parse the time.
{
# To calculate the elapsed time in seconds, split it into fields.
timetemp = $2
gsub(/[^0-9]+/, " ", timetemp)
n = split(timetemp, timepart)
# Compute the number of seconds based on n fields in timepart.
if (n >= 4)
time = timepart[4] + 60*timepart[3] + 60*60*timepart[2] + 24*60*60*timepart[1]
else if (n == 3)
time = timepart[3] + 60*timepart[2] + 60*60*timepart[1]
else
time = timepart[2] + 60*timepart[1]
# Ignore if less than a hour, 3600 seconds.
if (time < 3600)
next
# print the PID.
print $1
}')
# Kill the processes that are old enough.
[ -n "$PIDS" ] && kill -KILL $PIDS
Note that AIX ps might use different flags. Also, I have not tested the code, so there might be typos or bugs lurking in there; you need to carefully check it first.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.