LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 12-11-2008, 10:31 AM   #1
kais1
Member
 
Registered: Dec 2008
Posts: 61

Rep: Reputation: 17
command help


Hai all,

am grepping all the process running with local=no and printing the time of the process by


ps -ef | grep -i LOCAL=NO | awk '{print $5}'

which displays like

Dec
16:02:25
13:55:05
Dec
18:18:21
18:19:35
18:21:38
17:44:28
21:24:22
18:23:18
18:02:29
18:24:10
17:08:38


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

kai
 
Old 12-11-2008, 11:24 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,336

Rep: Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896Reputation: 3896
Quote:
Originally Posted by kais1 View Post
Hai all,

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......
 
Old 12-11-2008, 01:55 PM   #3
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Try this script:

Code:
#!/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 05:06 PM. Reason: Improved syntax for findng the first line. Used array to remove calls to sed and awk.
 
Old 12-11-2008, 02:23 PM   #4
kais1
Member
 
Registered: Dec 2008
Posts: 61

Original Poster
Rep: Reputation: 17
Thanks a lot for the script..


I only changed the following parameter:


PID_LIST=$(ps -ef | grep -i local=no| awk '{print $2 "," $5 }')


when I try to run, am getting the error :

bash-3.00$ sh new.sh
new.sh: syntax error at line 4: `PID_LIST=$' unexpected


Please suggest
\


Kai
 
Old 12-11-2008, 03:03 PM   #5
David1357
Senior Member
 
Registered: Aug 2007
Location: South Carolina, U.S.A.
Distribution: Ubuntu, Fedora Core, Red Hat, SUSE, Gentoo, DSL, coLinux, uClinux
Posts: 1,302
Blog Entries: 1

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by kais1 View Post
bash-3.00$ sh new.sh
new.sh: syntax error at line 4: `PID_LIST=$' unexpected
I do not think "sh" supports all the features used in that script. You need to use "bash".
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Turning a Ruby on Rails Ubuntu command into a Redhat command Mangalaiii Red Hat 1 12-02-2008 04:37 PM
My error in mkisofs command to make a command line iso mg92865 Linux - Newbie 1 06-20-2008 12:43 PM
Translating windows pscp command to linux scp command help robward Linux - General 2 01-17-2008 07:02 AM
startx command in FC7 reverts me back to the command prompt sriram87 Linux - Newbie 5 01-04-2008 12:18 AM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 08:14 PM


All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration