public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: David Haller <gentoo@dhaller.de>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] HOWTO: Freezing/unfreezing (groups of) processes
Date: Sat, 6 Feb 2021 02:08:57 +0100	[thread overview]
Message-ID: <20210206010857.dbct3qhhjaemtr6w@grusum.endjinn.de> (raw)
In-Reply-To: <YB2Xe1kGCwDwWeY4@waltdnes.org>

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 <waltdnes@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


  parent reply	other threads:[~2021-02-06  1:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-05  7:45 [gentoo-user] HOWTO: Freezing/unfreezing (groups of) processes Walter Dnes
2021-02-05  8:46 ` Andrew Udvare
2021-02-05 19:00   ` Walter Dnes
2021-02-05 18:24     ` Walter Dnes
2021-02-05 22:42       ` Matt Connell (Gmail)
2021-02-06  7:54         ` Walter Dnes
2021-02-05 11:55 ` Rich Freeman
2021-02-05 19:07   ` Walter Dnes
2021-02-05 14:39     ` Rich Freeman
2021-02-05 16:23     ` Neil Bothwick
2021-02-06  1:08     ` David Haller [this message]
2021-02-06  4:46     ` Paul Colquhoun
2021-02-05 12:12 ` Ramon Fischer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210206010857.dbct3qhhjaemtr6w@grusum.endjinn.de \
    --to=gentoo@dhaller.de \
    --cc=gentoo-user@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox