Kill a particular process if it's over an hour hold

I meant old not hold :slight_smile:

I need to kill a process if it's over an hour old and then send an e-mail of the list that was killed.....?

I need to kill ps -ef | grep stashd | grep ' older than an hour?'

#! /bin/bash
if test ps -ef | grep <stashd> (Is over an hour old)???? >>stashd_old.txt

stashd_old=`cat stashd_old.txt`
for i in $stashd_old;
do
kill -9 <stashd>
echo "Found <process_name> older than 1 hour, and killed process name | mail -s "killed <process_name `date`" Jay
fi

done;
etc this stashd process if it's over an hour old?

This will tell you the processes older than an hour....

#!/bin/sh

first()
{
        echo $1
}

second()
{
        echo $2
}

third()
{
        echo $3
}

ps -ef -o "pid etime comm" | while read PID ETIME COMM
do
        case "$ETIME" in
        *:* )
                DAYS=0
                HOURS=0
                MINUTES=0
                SECONDS=0

                case "$ETIME" in
                *-* )
                        X=`echo $ETIME | sed y/-/\ /`
                        DAYS=`first $X`
                        ETIME=`second $X`
                        ;;
                * )
                        ;;
                esac

                X=`echo $ETIME | sed y/:/\ /`

                case "$ETIME" in
                *:*:* )
                        HOURS=`first $X`
                        MINUTES=`second $X`
                        SECONDS=`third $X`
                        ;;
                *:* )
                        MINUTES=`first $X`
                        SECONDS=`second $X`
                        ;;
                *)
                        ;;
                esac

                HOURS=`echo $HOURS + \( $DAYS \* 24 \) | bc`
                MINUTES=`echo $MINUTES + \( 60 \* $HOURS \) | bc`
                SECONDS=`echo $SECONDS + \( 60 \* $MINUTES \) | bc`

                if test "$SECONDS" -gt "3600"
                then
                        echo $PID $COMM
                fi
                ;;
        * )
                ;;
        esac
done

I put your script in hourold.sh and greped stashd what's teletype syntax error on line 1 mean?

user@host:/export/home/user/--> hourold.sh |grep 'stashd'
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
21931 /opt/ft/st414/bin/agents/stashd
222 /opt/ft/st414/bin/agents/stashd
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
syntax error on line 1, teletype
user@host:/export/home/user/-->

It means that "bc" is getting rubbish. I tested this on Solaris 9.

I suggest you add a line that says

echo DAYS=$DAYS, HOURS=$HOURS, MINUTES=$MINUTES, SECONDS=$SECONDS

prior to the maths with bc.

and

echo $ETIME

after the "do".

I've just tested this on Solaris 10 and it works fine.

Have you copied and pasted exactly as I posted it?

SunOS hostname 5.8 Generic_117350-45

Yep triple checked still get the syntax messages. I'm getting the rest fine as well but it's throwing those messages in every so often?

I don't have a copy of Solaris 8 running here.

Can I suggest you debug it as I suggested?

Alternatively, post the result of

ps -ef -o "pid etime comm"

I did this it scrolls off the page and seems to work. I guess the problem I have is when I pipe the script out put through a grep to get the stashd process only?

But even when I do this

ps -ef -o "pid etime comm"

It comes out fine.

20983 31:15 /opt/ft/st414/bin/httpd
25370 9:21 /opt/ft/st414/bin/agents/stashd
13569 03:30:43 /opt/ft/st414/bin/httpd
28178 2-07:58:05 /usr/local/ctsa/control-sa/exe/Solaris-6/./p_ctscd
1394 7:09 /opt/ft/st414/bin/agents/stashd
14551 07:51:55 /opt/ft/st414/bin/agentd
20617 4:15 /opt/ft/st414/bin/agents/stashd
21487 20:46 /opt/ft/st414/bin/agents/stashd
27012 8:35 /opt/ft/st414/bin/agents/stashd
21264 4:00 /opt/ft/st414/bin/agents/stashd
3123 17:02 /opt/ft/st414/bin/agents/stashd

ps -ef -o "pid etime comm" |grep stashd

15684 13:40 /opt/ft/st414/bin/agents/stashd
4402 0:52 /opt/ft/st414/bin/agents/stashd
15085 46:48 /opt/ft/st414/bin/agents/stashd
29392 2:35 /opt/ft/st414/bin/agents/stashd
213 53:29 /opt/ft/st414/bin/agents/stashd
24358 4:04 /opt/ft/st414/bin/agents/stashd
29983 19:05 /opt/ft/st414/bin/agents/stashd
17786 5:59 /opt/ft/st414/bin/agents/stashd
26796 20:06 /opt/ft/st414/bin/agents/stashd
4951 38:29 /opt/ft/st414/bin/agents/stashd
4824 0:40 /opt/ft/st414/bin/agents/stashd
27276 9:28 /opt/ft/st414/bin/agents/stashd

There is a lot more output but didn't want to put all of it on the screen.

You will have to put in the debug lines as I suggested, those ETIME formats look okay.

I found this post.

syntax error on line 1, teletype - The UNIX Forums

But it doesn't say how to fix it.