ps aux | grep different Suse versions..

Pixie22

Expert Member
Joined
Feb 11, 2007
Messages
2,016
Reaction score
0
Hi, for some reason, our test system and our live system have differing linux versions (live 7.3; test 8.2), don't ask me why..

This makes my life a little difficult, as my scripts need to work on both systems, taking discrepencies into account.

For example..

I am trying to find out if my script is already running an instance. If it is already running, I want the new instance to die.
I use the following linux command to count the occurences of the programs name in the running process list.

ps aux | grep -c prog.pl 2>/dev/null

On the Test system 8.2, It returns 2 occurences of the prog name. One for the current instance, and one for the ps aux | grep -c prog.pl 2>/dev/null itself.

On the Live system 7.3, it returns 3 occurences of the prog name, one for the current instance, one for the ps aux | grep -c prog.pl 2>/dev/null itself, and an extra grep command.

Any ideas as to how I can get this worknig on BOTH systems?

Thanks
 
You could create a temporary lock file in the script and check to see if it exists as a quick solution:

Code:
if [ -a /tmp/myscript.lock ]; then
    exit
else
    # create file
    touch /tmp/myscript.lock

    # other cmds...

    rm /tmp/myscript.lock
fi
 
I am trying to find out if my script is already running an instance. If it is already running, I want the new instance to die.
Quick and dirty (requires procps package):
Code:
kill -9 `pgrep prog.pl`

Mr Fancypants wants a script:
Code:
#!/bin/sh

if [ $# -ne 1 ]; then
    echo "Usage: `basename $0` <progname>"
    exit 1
else
    for PIDS in `pidof $1`
    do
        echo "kill -9 ${PIDS}"
    done
fi

When you're happy with the script remove the echo and quotes around the kill -9 ${PIDS} command.
If your distro doesn't have "pidof" you can revert to your "grep -c" method, see below.
If prog.pl has multiple PIDs, a more long-winded script will be needed.

On the Live system 7.3, it returns 3 occurences of the prog name, one for the current instance, one for the ps aux | grep -c prog.pl 2>/dev/null itself, and an extra grep command.
Because your `grep -c prog.pl` also matches.
Always do an inverted match to filter this out, i.e
Code:
ps aux | grep -v grep | grep -c prog.pl
 
Top
Sign up to the MyBroadband newsletter
X