def AmRunning(ss) :
return os.popen("ps ax").read().count(ss) # get, unwrap, count
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.
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.--
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.
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.
These two produce an off-by-one, right?
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.
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
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
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'.
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) :.bashrc
return os.popen("ps ax").read().count(ss) # get, unwrap, count >>>>
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.
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.
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.
You may want to quote the $1 expansion, or the find will complain the
first time you do 'Find "something with spaces"'
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.
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.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (1 / 5) |
| Uptime: | 20:57:16 |
| Calls: | 742 |
| Files: | 1,218 |
| D/L today: |
6 files (8,794K bytes) |
| Messages: | 185,811 |
| Posted today: | 1 |