0

I want to write a unix shell script to run a command 3 times in every 80 seconds and write the every sequence in a different line in a text file. And also if the all results are 10 or more in a line I want to kill the process:

for example:

pstack <pid> | grep -c 'abcd'

5

pstack <pid> | grep -c 'abcd'

5

pstack <pid> | grep -c 'abcd'

5

//Nothing to do.

//after 80 seconds again it runs:

pstack <pid> | grep -c 'abcd'

10 

pstack <pid> | grep -c 'abcd'

10

pstack <pid> | grep -c 'abcd'

10

kill -9 < PID>     // because all three outputs are bigger than 10   

also

the output file:

5 5 5 

10 10 10 

Note the if the output sequence is "10 10 11", "10 11 12" etc. then the process should be killed again. But if it is like "9 9 10" then no need to be killed.

JJJ
  • 32,902
  • 20
  • 89
  • 102
ephieste
  • 1
  • 2

1 Answers1

1

What are you trying to achieve?

Sounds like an extremely hacky way to monitor a process. Couldn't you simply employ:

ulimit -T 10    # the maximum number of threads

or a variation (man bash , /ulimitEnter)?

That way a program could even possibly be more graceful in shutting itself down.


Note: since you suggest using kill -9 without trying other signals, perhaps you imply that signals never get handled? In that case you can probably use ulimit -i (the maximum number of pending signals)

Snippet

#!/bin/bash

function dumpstack()
{
    pstack $(pgrep a.exe) | grep -c abcd
}

while sleep 1; do dumpstack; done | tee rawoutput.log |
    {
        trap "" INT
        count=0;
        while read stackframes; do 
            if [[ $stackframes -lt 10 ]]; then
                count=0
            else
                count=$(($count+1))
            fi

            if [[ $count -ge 3 ]]; then
                echo KILL -9 !
                            break
            fi
            echo "(debug frames:$stackframes, count:$count)"
        done
    } | tee cooked_output.log
sehe
  • 374,641
  • 47
  • 450
  • 633
  • The way I described is a must :( I am trying to count the number of specific lines in the pstack output and decide to kill or not to kill according to the last three results. – ephieste Jun 27 '11 at 14:44
  • I know what you are trying to do. I think it's hacky, and suggest other ideas. Feel free to ignore them :). If you don't want to deliberate, just code it – sehe Jun 27 '11 at 14:52
  • posting a snippet that does what you want assuming bash shell – sehe Jun 27 '11 at 15:06
  • Actually I have to add something to this code to check the pid of the "a.exe" everytime before running pstack. such as: pstack `ps -aef | grep 'a.exe' | grep -v grep | awk '{print $2}'`| grep - c "abcd" But there is something wrong with this query and I don't know how to add it inside the above... – ephieste Jun 27 '11 at 20:36
  • Also I have an idea it can check the sum of the last three output every time if it is equal or bigger than 30 it can run the kill... – ephieste Jun 27 '11 at 20:40
  • Yeah I thought of the sum. However, like you said `The way I described is a must :(`. The sum of 99 + 0 + 0 > 30, but doesn't satisfy your own criteria :) -- If you are on linux, `pstack $(pgrep a.exe)` will do what you want (perhaps use `pgrep a.exe|sort -n|head -n1` to get the first pid only) – sehe Jun 27 '11 at 20:49
  • Well, have you tried? `pgrep` works on my OpenSolaris `SunOS bbs2 5.11 snv_134 i86pc i386 i86pc`. It lives in `/usr/bin/pgrep` so I'm pretty sure it's not from blastwave or community software – sehe Jun 27 '11 at 21:08
  • pstack $(pgrep a.exe) | grep -c abcd is working...Also I am trying to keep the ouputs in a file... because I have to investigate the system. 10 10 10 is a risky sequence but there might be unexpected problems during a 9 9 9 sequence... – ephieste Jun 27 '11 at 21:14
  • ok, I jotted in a 'tee rawoutput.log |' just to demonstrate how to 'keep output in a file' – sehe Jun 27 '11 at 21:25
  • thank you very much I got the idea but unfortunately: "line 4: seq: command not found " :( – ephieste Jun 27 '11 at 21:27
  • Ok, simplified the answer to remove confusing bits. -- That's kind of funny, **obviously** you didn't that (obvious by virtue of a helpful comment sitting on the line directly above it <-- now removed) – sehe Jun 27 '11 at 21:41
  • @ephieste: Re: `ok I ignore...` No! That's not right. I edited the answer. Please don't cripple the script by ignoring errors. [You'll simply make it not work without seeing it](http://www.ostrichheadinsand.com/images/ostrich-head-in-sand.jpg?dur=125) – sehe Jun 27 '11 at 21:43