Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
now I need to check this time.. if it is 4 hours less than sysdate and time ..those process should be killed automatically
Please suggest some ideas
Well, it looks like you've got the time, and you know the system time, so an idea would be to write a script to compare the two, and kill it.
Hate to be vague, but you haven't said what language you want to use, or any real details. And I assume you want processes OLDER than 4 hours, not newer. Otherwise, nothing would ever be able to start......
#!/bin/bash
# Get a list of PIDs and their start times and pair them using a
# comma
PID_LIST=$(ps -ef | awk '{print $2 "," $5 }')
# Save the current time as seconds since the epoch
current_time=$(date +"%s")
# Calculate four hours before the current time
four_hours_ago=$(expr $current_time - $(expr 4 \* 60 \* 60))
# For each (PID,start time) pair
for item in $PID_LIST ;
do
# If this is not the first line
if ! [[ $item =~ "PID" ]] ; then
# Convert the pair into an array
pair=(${item/,/ })
# Pull out the PID
pid=${pair[0]}
# Pull out the start time
start_time=${pair[1]}
# Get the start time as seconds since the epoch
start_secs=$(date -d $start_time +"%s")
# If the start time is greater than or equal to four hours
# before now
if (( $start_secs >= $four_hours_ago )) ; then
# Do some useful work with the information
echo "Killing $pid that started at $start_time" ;
fi ;
fi ;
done
Last edited by David1357; 12-11-2008 at 04:06 PM.
Reason: Improved syntax for findng the first line. Used array to remove calls to sed and awk.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.