Gentoo Archives: gentoo-user

From: David Haller <gentoo@×××××××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] HOWTO: Freezing/unfreezing (groups of) processes
Date: Sat, 06 Feb 2021 01:09:18
Message-Id: 20210206010857.dbct3qhhjaemtr6w@grusum.endjinn.de
In Reply to: Re: [gentoo-user] HOWTO: Freezing/unfreezing (groups of) processes by Walter Dnes
1 Hello,
2
3 On Fri, 05 Feb 2021, Walter Dnes wrote:
4 >On Fri, Feb 05, 2021 at 06:55:12AM -0500, Rich Freeman wrote
5 >> On Fri, Feb 5, 2021 at 2:45 AM Walter Dnes <waltdnes@××××××××.org> wrote:
6 >> > So far, so good, but running "ps -ef | grep whatever" and then
7 >> > typing the kill -SIGSTOP/SIGCONT command on the correct pid is grunt
8 >> > work, subject to typos.
9
10 It's much easier to use the '-o' option of ps, i.e.:
11
12 $ ps -eo pid,cmd
13
14 That gives you a much easier format to work with. There's a lot more
15 fields to use, e.g. tname or tty, args or cmd, comm, and many more see
16 'man ps' under "STANDARD FORMAT SPECIFIERS".
17
18 > My reading of the "killall" man page is that it works on command
19 >names. For my script, "pstop palemoon" stops all instances of Pale
20 >Moon. But my script greps the entire line, so "pstop slashdot" will
21 >stop the process...
22 >
23 >/home/waltdnes/pm/palemoon/palemoon -new-instance -p slasdot
24 >
25 > Does "killall" have that ability to stop a process based on any
26 >parameters in the command line?
27
28 The following script does:
29
30 ==== ~/bin/pstop && ln -s pstop ~/bin/pcont ====
31 #!/usr/bin/gawk -f
32 BEGINFILE { if( FILENAME != "" ) { exit(0); } }
33 BEGIN {
34 ### determine if were run as pstop or pcont
35 cmdlinefile = "/proc/" PROCINFO["pid"] "/cmdline" ;
36 getline cmdline < cmdlinefile;
37 n = split(cmdline, argv, "\0");
38 IAM=argv[3];
39 if( IAM ~ /pstop$/) { SIG="STOP"; } else { SIG="CONT"; };
40
41 ### now to work ...
42 printf("%s-ing pids: ", SIG);
43 bcmd = sprintf("kill -%s ", SIG);
44 pscmd = "ps -eo pid,cmd";
45
46 # IGNORECASE=1 ### uncomment for case insensitive matching
47 while ( pscmd | getline ) {
48 if( $1 != PROCINFO["pid"] ) { ### ignore ourself
49 p = $1; $1 = ""; ### save pid to p; prune pid from $0
50 for(i=1; i < (ARGC); i++) {
51 if( $0 ~ ARGV[i] ) {
52 printf("%s ", p);
53 cmd = bcmd p;
54 system(cmd);
55 }
56 }
57 }
58 }
59 }
60 END { printf("\n"); }
61 ====
62
63 Arguments can be any number of (quoted where neccessary) regular
64 expressions described under 'Regular Expressions' in 'man gawk'
65 (basically Extended POSIX REs as in egrep, see 'man 7 regex').
66
67 Example use:
68
69 $ pstop palemoon firefox slashdot 'chrom(e|ium)'
70
71 (and the same for pcont)
72
73 HTH,
74 -dnh
75
76 --
77 Love your enemies: they'll go crazy trying to figure out what you're up
78 to. -- BSD fortune file