Hello,

On Fri, 05 Feb 2021, Walter Dnes wrote:
>On Fri, Feb 05, 2021 at 06:55:12AM -0500, Rich Freeman wrote
>> On Fri, Feb 5, 2021 at 2:45 AM Walter Dnes <waltd...@waltdnes.org> wrote:
>> >   So far, so good, but running "ps -ef | grep whatever" and then
>> > typing the kill -SIGSTOP/SIGCONT command on the correct pid is grunt
>> > work, subject to typos.

It's much easier to use the '-o' option of ps, i.e.:

    $ ps -eo pid,cmd

That gives you a much easier format to work with. There's a lot more
fields to use, e.g. tname or tty, args or cmd, comm, and many more see
'man ps' under "STANDARD FORMAT SPECIFIERS".

>  My reading of the "killall" man page is that it works on command
>names.  For my script, "pstop palemoon" stops all instances of Pale
>Moon.  But my script greps the entire line, so "pstop slashdot" will
>stop the process...
>
>/home/waltdnes/pm/palemoon/palemoon -new-instance -p slasdot
>
>  Does "killall" have that ability to stop a process based on any
>parameters in the command line?

The following script does:

==== ~/bin/pstop && ln -s pstop ~/bin/pcont ====
#!/usr/bin/gawk -f
BEGINFILE { if( FILENAME != "" ) { exit(0); } }
BEGIN {
    ### determine if were run as pstop or pcont
    cmdlinefile = "/proc/" PROCINFO["pid"] "/cmdline" ;
    getline cmdline < cmdlinefile;
    n = split(cmdline, argv, "\0");
    IAM=argv[3];
    if( IAM ~ /pstop$/) { SIG="STOP"; } else { SIG="CONT"; };

    ### now to work ...
    printf("%s-ing pids: ", SIG); 
    bcmd = sprintf("kill -%s ", SIG);
    pscmd = "ps -eo pid,cmd";

    # IGNORECASE=1 ### uncomment for case insensitive matching
    while ( pscmd | getline ) {
        if( $1 != PROCINFO["pid"] ) { ### ignore ourself
            p = $1; $1 = ""; ### save pid to p; prune pid from $0
            for(i=1; i < (ARGC); i++) {
                if( $0 ~ ARGV[i] ) {
                    printf("%s ", p);
                    cmd = bcmd p;
                    system(cmd);
                }
            }
        }
    }
}
END { printf("\n"); }
====

Arguments can be any number of (quoted where neccessary) regular
expressions described under 'Regular Expressions' in 'man gawk'
(basically Extended POSIX REs as in egrep, see 'man 7 regex').

Example use:

$ pstop palemoon firefox slashdot 'chrom(e|ium)'

(and the same for pcont)

HTH,
-dnh

-- 
Love your enemies: they'll go crazy trying to figure out what you're up
to.                                                 -- BSD fortune file

Reply via email to