• Ridiculous Online Programming "Advice"

    From c186282@c186282@nnada.net to comp.os.linux.misc on Sat Jan 10 20:14:49 2026
    From Newsgroup: comp.os.linux.misc

    Have a Python app that's a bit heavy on system
    resources. Alas it's sometimes hard to SSH or
    VNC into the box without evoking another instance.

    SO, checked out a few 'advice' sites. There was
    advice ... ten to twenty line long bits where
    you had to get follow and parse-out process tables
    and such. Long and complicated.

    Then it came to me, Linux/Unix already have
    something that'll DO all that.

    So, my ONE-line fix :

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    That's it. Might not work in 101% of all possible
    situations but for pretty much anything it'll do
    just fine.

    Feed it "sys.argv[0]" or whatever you want - it'll
    tell you how many are running. Best results if you
    evoke the pgm using a full path, but you don't
    HAVE to. You can look for OTHER pgms too of course.

    COULD add a "grep" in there (and it adds its own
    evocation line) but all grep does is pretty much
    what 'count(ss)' is doing anyway, so why the waste ?

    If you like my fix, I declare it to be public domain.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Sun Jan 11 07:31:34 2026
    From Newsgroup: comp.os.linux.misc

    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    $ AmRunning brave
    32


    Before Linux distros mostly switched to bash I used tcsh. You could do a
    lot more with alias without having to write a function.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From c186282@c186282@nnada.net to comp.os.linux.misc on Sun Jan 11 02:45:59 2026
    From Newsgroup: comp.os.linux.misc

    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed
    or desired.

    $ AmRunning brave
    32


    Before Linux distros mostly switched to bash I used tcsh. You could do a
    lot more with alias without having to write a function.

    Perhaps ... but few use tcsh anymore.

    Anyway, put this one-liner into my apps, works
    just perfectly.

    "if AmRunning(sys.argv[0]) > 1 :
    sys.exit()
    "

    Right at the top of the program.

    Anyway, the top-ranked "advice" all aimed
    at re-inventing the wheel. Ridiculous !

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Nuno Silva@nunojsilva@invalid.invalid to comp.os.linux.misc on Sun Jan 11 10:32:08 2026
    From Newsgroup: comp.os.linux.misc

    On 2026-01-11, rbowman wrote:

    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    These two produce an off-by-one, right?

    (Assuming .count() counts lines, otherwise it's off-by-at-least-one in
    that one?)

    $ AmRunning brave
    32


    Before Linux distros mostly switched to bash I used tcsh. You could do a lot more with alias without having to write a function.
    --
    Nuno Silva
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.os.linux.misc on Sun Jan 11 20:31:13 2026
    From Newsgroup: comp.os.linux.misc

    c186282 <c186282@nnada.net> wrote:
    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed
    or desired.

    Your .count() is implicity a "grep" in that to count it has to see if
    "ss" exists on the line. So the function is still there, even if it is
    hidden behind the abstraction.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Sun Jan 11 22:31:23 2026
    From Newsgroup: comp.os.linux.misc

    On Sun, 11 Jan 2026 02:45:59 -0500, c186282 wrote:

    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed or desired.

    $ ps aux | wc
    419 5866 71885

    ps ax | wc
    418 3347 55870

    From 'man ps'

    To see every process on the system using BSD syntax:
    ps ax
    ps axu


    I suppose you think count(ss) isn't doing a sort of grep as it tests every element in the list for ss.

    I should thank you. It did inspire me to add

    Find() {
    find . -name $1 | xargs grep $2
    }

    to my .bashrc. I use it a lot and was always going to get roundtoit.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Sun Jan 11 22:51:57 2026
    From Newsgroup: comp.os.linux.misc

    On Sun, 11 Jan 2026 10:32:08 +0000, Nuno Silva wrote:

    These two produce an off-by-one, right?

    The bash version does since it also counts 'grep --color=auto watchdogd'
    along with the watchdogd process itself.

    improved version:

    AmRunning() {
    ps aux | grep -v "grep" | grep $1 | wc -l
    }


    $ AmRunning firefox
    11

    That was an unexpected result since Firefox wasn't running. Further investigation revealed Tor was showing its roots

    tor-browser/Browser/firefox.real

    Kill Tor and

    $ AmRunning firefox
    0



    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From c186282@c186282@nnada.net to comp.os.linux.misc on Sun Jan 11 18:12:00 2026
    From Newsgroup: comp.os.linux.misc

    On 1/11/26 15:31, Rich wrote:
    c186282 <c186282@nnada.net> wrote:
    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed
    or desired.

    Your .count() is implicity a "grep" in that to count it has to see if
    "ss" exists on the line. So the function is still there, even if it is hidden behind the abstraction.

    Exactly, ergo there's no point in effectively
    grepping twice. 'Count()' also nicely adds up
    the number of matching instances.

    This is a Python solution - indeed Linux Python.
    Variants ought to be doable, not sure how compactly,
    in a number of other langs. Winders has 'tasklist'
    but I don't remember if it's as to-the-point as 'ps'.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From c186282@c186282@nnada.net to comp.os.linux.misc on Sun Jan 11 19:31:48 2026
    From Newsgroup: comp.os.linux.misc

    On 1/11/26 17:51, rbowman wrote:
    On Sun, 11 Jan 2026 10:32:08 +0000, Nuno Silva wrote:

    These two produce an off-by-one, right?

    The bash version does since it also counts 'grep --color=auto watchdogd' along with the watchdogd process itself.

    improved version:

    AmRunning() {
    ps aux | grep -v "grep" | grep $1 | wc -l
    }


    $ AmRunning firefox
    11

    That was an unexpected result since Firefox wasn't running. Further investigation revealed Tor was showing its roots

    tor-browser/Browser/firefox.real

    Kill Tor and

    $ AmRunning firefox
    0

    Works well ... but you can't run it inside Python,
    you'd have to make a shell script. If there are
    more than 255 hits then there might be issues
    sending a simple return value. Indeed this creates
    a text rep of a number ...

    If I have the time I'll try comparing the speed
    of the two solutions - 100 iterations maybe.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Nuno Silva@nunojsilva@invalid.invalid to comp.os.linux.misc on Sun Jan 11 23:57:22 2026
    From Newsgroup: comp.os.linux.misc

    On 2026-01-11, rbowman wrote:

    On Sun, 11 Jan 2026 10:32:08 +0000, Nuno Silva wrote:

    These two produce an off-by-one, right?

    The bash version does since it also counts 'grep --color=auto watchdogd' along with the watchdogd process itself.

    Yeah, and just that one. The other is, of course, not affected because
    it doesn't invoke grep.

    (I wish I could go one week without a post where lack of coffee leads to
    some sort of mistake...)

    improved version:

    AmRunning() {
    ps aux | grep -v "grep" | grep $1 | wc -l
    }

    Would it be preferrable to just subtract one?

    I think another approach is to add [ and ] around one letter of what
    you're filtering for, but that'd require modifying the string, and
    shares one issue with "grep -v grep": it doesn't work as intended if the
    name you are grepping for is grep.

    (But then we also have to consider words being something other than
    argv[0]. 'ps axo comm', perhaps?; Is that 'ps -eo comm' in POSIX
    syntax?)

    $ AmRunning firefox
    11

    That was an unexpected result since Firefox wasn't running. Further investigation revealed Tor was showing its roots

    tor-browser/Browser/firefox.real

    Kill Tor and

    $ AmRunning firefox
    0



    --
    Nuno Silva
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Mon Jan 12 05:24:35 2026
    From Newsgroup: comp.os.linux.misc

    On Sun, 11 Jan 2026 18:12:00 -0500, c186282 wrote:


    This is a Python solution - indeed Linux Python. Variants ought to be
    doable, not sure how compactly, in a number of other langs. Winders
    has 'tasklist' but I don't remember if it's as to-the-point as 'ps'.

    No. I wrote a Windows version of ps so support would have a tool
    compatible with the Unix/Linus ps.

    First you load NtQueryInformationProcess from the native NT API dll, and then call EnumProcesses().

    https://learn.microsoft.com/en-us/windows/win32/api/psapi/nf-psapi- enumprocesses

    Then you iterate the list calling OpenProcess() on each.

    https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf- processthreadsapi-openprocess

    Then you make several calls to ReadProcessMemory(), GetProcessTimes(), OpenProcessToken(), and GetTokenInformation() to get the usual stuff
    reported by ps. Along the way you do a little math since you're dealing
    with 64 bit integers of 100 nanoseconds.

    If you're a peon (User) you can't do that but you can open the Registry HKEY_PERFORMANCE_DATA and get some info.

    Ain't nothing easy in Windows. There is also a version that uses CreateToolhelp32Snapshot().

    https://learn.microsoft.com/en-us/windows/win32/api/tlhelp32/nf-tlhelp32- createtoolhelp32snapshot




    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.os.linux.misc on Mon Jan 12 05:57:42 2026
    From Newsgroup: comp.os.linux.misc

    c186282 <c186282@nnada.net> wrote:
    On 1/11/26 15:31, Rich wrote:
    c186282 <c186282@nnada.net> wrote:
    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count >>>>
    .bashrc
    AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed
    or desired.

    Your .count() is implicity a "grep" in that to count it has to see if
    "ss" exists on the line. So the function is still there, even if it is
    hidden behind the abstraction.

    Exactly, ergo there's no point in effectively
    grepping twice. 'Count()' also nicely adds up
    the number of matching instances.

    You miss the point (as usual). The grep is required for the shell
    script version because wc does not include any implicit 'grepping'.

    And the effect of the 'grep' is still present, just not as explicitly,
    in the python version.

    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Rich@rich@example.invalid to comp.os.linux.misc on Mon Jan 12 06:02:07 2026
    From Newsgroup: comp.os.linux.misc

    rbowman <bowman@montana.com> wrote:
    On Sun, 11 Jan 2026 02:45:59 -0500, c186282 wrote:

    On 1/11/26 02:31, rbowman wrote:
    On Sat, 10 Jan 2026 20:14:49 -0500, c186282 wrote:

    def AmRunning(ss) :
    return os.popen("ps ax").read().count(ss) # get, unwrap, count

    .bashrc AmRunning() {
    ps aux | grep $1 | wc -l
    }

    "aux" creates a much larger text.

    And, for this purpose, no 'grep' is needed or desired.

    $ ps aux | wc
    419 5866 71885

    ps ax | wc
    418 3347 55870

    From 'man ps'

    To see every process on the system using BSD syntax:
    ps ax
    ps axu


    I suppose you think count(ss) isn't doing a sort of grep as it tests
    every element in the list for ss.

    I also pointed out that fact out as well. The python count(ss) is a
    combined grep [1] and wc. I'm slightly undecided if c186282 did not
    realize that a grep is present, just hidden.

    [1] Although it may be a less powerful grep if it only supports
    substring match (likely) instead of regular expressions.

    I should thank you. It did inspire me to add

    Find() {
    find . -name $1 | xargs grep $2
    }

    to my .bashrc. I use it a lot and was always going to get roundtoit.

    You may want to quote the $1 expansion, or the find will complain the
    first time you do 'Find "something with spaces"'
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Mon Jan 12 07:05:17 2026
    From Newsgroup: comp.os.linux.misc

    On Sun, 11 Jan 2026 19:31:48 -0500, c186282 wrote:

    Works well ... but you can't run it inside Python,
    you'd have to make a shell script. If there are more than 255 hits
    then there might be issues sending a simple return value. Indeed this
    creates a text rep of a number ...

    If I have the time I'll try comparing the speed of the two solutions
    - 100 iterations maybe.

    I've never needed to run it in a Python script. I was just curious if a
    bash shell function would get the job done. There are a few other things I should create functions for rather than manually typing the commands like mounting the music files on my nfs server.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Mon Jan 12 08:05:04 2026
    From Newsgroup: comp.os.linux.misc

    On Mon, 12 Jan 2026 06:02:07 -0000 (UTC), Rich wrote:


    You may want to quote the $1 expansion, or the find will complain the
    first time you do 'Find "something with spaces"'

    $2 was the problem for my usage.

    $ Find *.cpp "cout << buf"
    ./winutils/tail.cpp: cout << buf << endl; ./winutils/tail.cpp: cout << buf << endl; ./winutils/tail.cpp: cout << buf <<
    endl;
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From rbowman@bowman@montana.com to comp.os.linux.misc on Mon Jan 12 08:07:23 2026
    From Newsgroup: comp.os.linux.misc

    On Sun, 11 Jan 2026 23:57:22 +0000, Nuno Silva wrote:

    I think another approach is to add [ and ] around one letter of what
    you're filtering for, but that'd require modifying the string, and
    shares one issue with "grep -v grep": it doesn't work as intended if the
    name you are grepping for is grep.

    I'll take a chance I'm somehow looking for a running grep process.
    --- Synchronet 3.21a-Linux NewsLink 1.2
  • From c186282@c186282@nnada.net to comp.os.linux.misc on Mon Jan 12 17:42:25 2026
    From Newsgroup: comp.os.linux.misc

    On 1/12/26 02:05, rbowman wrote:
    On Sun, 11 Jan 2026 19:31:48 -0500, c186282 wrote:

    Works well ... but you can't run it inside Python,
    you'd have to make a shell script. If there are more than 255 hits
    then there might be issues sending a simple return value. Indeed this
    creates a text rep of a number ...

    If I have the time I'll try comparing the speed of the two solutions
    - 100 iterations maybe.

    I've never needed to run it in a Python script. I was just curious if a
    bash shell function would get the job done. There are a few other things I should create functions for rather than manually typing the commands like mounting the music files on my nfs server.

    Yes, the Bash script seems to work well. You could
    put it at the top of a shell script that also starts
    yer program, assuming the co-instance number is zero.

    ODD bit about 'ps' in this role ... if you open up
    several terminals (lxterminal in my case) it only
    sees ONE. If you run something from one of those
    terminals 'ps' sees THAT, but not the terminal app
    itself. Weird.

    --- Synchronet 3.21a-Linux NewsLink 1.2