0

I have a simple script which just uses awk to extract some information from output of ps. When I run this on the terminal, it works fine. But, when I run this script with watch:

watch bench_run.sh

there is no output at all.

The script is as follows

#!/bin/bash

bench_run() {
    local awk_cmd='
        {
            time=$10
            bench=$46

            start=match(bench, /throughput/)
            start+=(RLENGTH+1)
            end=match(bench, /base/)
            printf ("%s %s\n", time, substr(bench, start, end-start-1))
        }
    '

    ps aux | grep $USER | grep simulator | awk "$awk_cmd"
}

bench_run

What could be the reason for watch not printing any output?

1

Try replacing ps aux by ps auxww to get the full width output. ps is probably testing your input or tty and deciding to act differently inside watch.

  • @Hashken: I think this is the correct (and most succinct) answer. Adding the ww to the option list makes sure you get the full command line printed out; otherwise, output gets truncated to your current window width. Or as @cas suggests, use ps -o to control the output in a way that make it more awk friendly. – Otheus Nov 3 '15 at 11:06
0

This is the start of an improved version. So far, I've changed it to:

  • get rid of the greps (awk can do regexp matching)
  • make better use of ps options
  • got rid of the strange awk_cmd local variable
  • quote $1 and $46 (which is now $37 with 9 unwanted fields removed from ps output
#!/bin/bash

bench_run() {

    ps -u "$USER" -o time,args | awk "/simulator/ {
            time=\$1
            bench=\$37

            start=match(bench, /throughput/)
            start+=(RLENGTH+1)
            end=match(bench, /base/)
            printf ("%s %s\n", time, substr(bench, start, end-start-1))
            }
    "
}

bench_run

Here's a version that uses sed

#! /bin/sh
watch 'ps -u "$USER" -o time,args | 
       sed -n -e "/[s]imulator/ s/\([^ ]*\) .*\(throughput.*\)base.*/\1 \2/ p"'

I've used ' single-quotes around the watch command, so I've used " double-quotes around the sed command that's inside it.

If simulator is the full name of the process, you could use:

#! /bin/sh
watch 'ps -o time,args -C simulator | 
       sed -n -e "s/\([^ ]*\) .*\(throughput.*\)base.*/\1 \2/ p"'
  • could you add some more information about what exactly you are trying to do. Is 'simulator' the exact full name of the process? or just an argument to the process name? what are you trying to do with the start and end and substr() stuff? i'm starting to think that this may be better done with sed than awk. Is there any particular reason why you've embedded it in a function? for re-use somewhere else? – cas Oct 30 '15 at 12:25
  • Does ps -u "$USER" -o time,args | sed -n -e '/[s]imulator/ s/\([^ ]*\) .*\(throughput.*\)base.*/\1 \2/ p' give you the output you're after? – cas Oct 30 '15 at 12:35
  • simulator is the name of the process – Hashken Nov 3 '15 at 6:20
  • The above version with sed works :) But, how do I use this with watch? My original script also work fine when run alone. But, when given to watch it gives no output – Hashken Nov 3 '15 at 6:20
  • @Hashken, i've added a version that uses sed to my answer. – cas Nov 3 '15 at 6:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.