Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.os.linux.misc > #80867 > unrolled thread
| Started by | c186282 <c186282@nnada.net> |
|---|---|
| First post | 2026-01-10 20:14 -0500 |
| Last post | 2026-01-12 08:07 +0000 |
| Articles | 17 — 4 participants |
Back to article view | Back to comp.os.linux.misc
Ridiculous Online Programming "Advice" c186282 <c186282@nnada.net> - 2026-01-10 20:14 -0500
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-11 07:31 +0000
Re: Ridiculous Online Programming "Advice" c186282 <c186282@nnada.net> - 2026-01-11 02:45 -0500
Re: Ridiculous Online Programming "Advice" Rich <rich@example.invalid> - 2026-01-11 20:31 +0000
Re: Ridiculous Online Programming "Advice" c186282 <c186282@nnada.net> - 2026-01-11 18:12 -0500
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-12 05:24 +0000
Re: Ridiculous Online Programming "Advice" Rich <rich@example.invalid> - 2026-01-12 05:57 +0000
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-11 22:31 +0000
Re: Ridiculous Online Programming "Advice" Rich <rich@example.invalid> - 2026-01-12 06:02 +0000
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-12 08:05 +0000
Re: Ridiculous Online Programming "Advice" Nuno Silva <nunojsilva@invalid.invalid> - 2026-01-11 10:32 +0000
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-11 22:51 +0000
Re: Ridiculous Online Programming "Advice" c186282 <c186282@nnada.net> - 2026-01-11 19:31 -0500
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-12 07:05 +0000
Re: Ridiculous Online Programming "Advice" c186282 <c186282@nnada.net> - 2026-01-12 17:42 -0500
Re: Ridiculous Online Programming "Advice" Nuno Silva <nunojsilva@invalid.invalid> - 2026-01-11 23:57 +0000
Re: Ridiculous Online Programming "Advice" rbowman <bowman@montana.com> - 2026-01-12 08:07 +0000
| From | c186282 <c186282@nnada.net> |
|---|---|
| Date | 2026-01-10 20:14 -0500 |
| Subject | Ridiculous Online Programming "Advice" |
| Message-ID | <546dnQ79vYkSZP_0nZ2dnZfqnPSdnZ2d@giganews.com> |
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.
[toc] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-11 07:31 +0000 |
| Message-ID | <msh1ulFqhrqU1@mid.individual.net> |
| In reply to | #80867 |
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.
[toc] | [prev] | [next] | [standalone]
| From | c186282 <c186282@nnada.net> |
|---|---|
| Date | 2026-01-11 02:45 -0500 |
| Message-ID | <7y2dnbcmBsiqyP70nZ2dnZfqn_udnZ2d@giganews.com> |
| In reply to | #80892 |
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 !
[toc] | [prev] | [next] | [standalone]
| From | Rich <rich@example.invalid> |
|---|---|
| Date | 2026-01-11 20:31 +0000 |
| Message-ID | <10k11ah$6725$1@dont-email.me> |
| In reply to | #80894 |
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.
[toc] | [prev] | [next] | [standalone]
| From | c186282 <c186282@nnada.net> |
|---|---|
| Date | 2026-01-11 18:12 -0500 |
| Message-ID | <A4SdnYq6693as_n0nZ2dnZfqnPWdnZ2d@giganews.com> |
| In reply to | #80935 |
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'.
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-12 05:24 +0000 |
| Message-ID | <msjesiF80p4U2@mid.individual.net> |
| In reply to | #80954 |
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
[toc] | [prev] | [next] | [standalone]
| From | Rich <rich@example.invalid> |
|---|---|
| Date | 2026-01-12 05:57 +0000 |
| Message-ID | <10k22gm$24t9m$1@dont-email.me> |
| In reply to | #80954 |
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.
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-11 22:31 +0000 |
| Message-ID | <msimlqF4ju7U1@mid.individual.net> |
| In reply to | #80894 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Rich <rich@example.invalid> |
|---|---|
| Date | 2026-01-12 06:02 +0000 |
| Message-ID | <10k22ov$24t9m$2@dont-email.me> |
| In reply to | #80948 |
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"'
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-12 08:05 +0000 |
| Message-ID | <msjo9gF9runU1@mid.individual.net> |
| In reply to | #80970 |
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;
[toc] | [prev] | [next] | [standalone]
| From | Nuno Silva <nunojsilva@invalid.invalid> |
|---|---|
| Date | 2026-01-11 10:32 +0000 |
| Message-ID | <10jvu79$3ql4q$1@dont-email.me> |
| In reply to | #80892 |
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
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-11 22:51 +0000 |
| Message-ID | <msinsdF4ju7U2@mid.individual.net> |
| In reply to | #80896 |
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
[toc] | [prev] | [next] | [standalone]
| From | c186282 <c186282@nnada.net> |
|---|---|
| Date | 2026-01-11 19:31 -0500 |
| Message-ID | <SymdnYi5Detp3fn0nZ2dnZfqn_QAAAAA@giganews.com> |
| In reply to | #80949 |
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.
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-12 07:05 +0000 |
| Message-ID | <msjkpdF990eU1@mid.individual.net> |
| In reply to | #80955 |
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.
[toc] | [prev] | [next] | [standalone]
| From | c186282 <c186282@nnada.net> |
|---|---|
| Date | 2026-01-12 17:42 -0500 |
| Message-ID | <SOKcnTkSSfVI5fj0nZ2dnZfqnPqdnZ2d@giganews.com> |
| In reply to | #80971 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Nuno Silva <nunojsilva@invalid.invalid> |
|---|---|
| Date | 2026-01-11 23:57 +0000 |
| Message-ID | <10k1dd2$9he4$1@dont-email.me> |
| In reply to | #80949 |
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
[toc] | [prev] | [next] | [standalone]
| From | rbowman <bowman@montana.com> |
|---|---|
| Date | 2026-01-12 08:07 +0000 |
| Message-ID | <msjodrF9runU2@mid.individual.net> |
| In reply to | #80964 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.os.linux.misc
csiph-web