Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Create a pidof command to find PID numbers easily' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Create a pidof command to find PID numbers easily
Authored by: adamjacobmuller on Jun 19, '03 01:22:50PM

<<author of the hint >>
this is a small bug in this hint, i'm not sure if it will create a problem however i am being preemptive.....

when you run pidof on a process name that has more than one process my pidof would return the pids delimited with \n's
this is not a good idea as the standard pidof would return the pids delimited by a space.
i fixed this by adding a "tr '\n' ' '"
then an echo on the end just to wrap the output

if that's confusing than dont worry
here's the revised code
<code>
#!/bin/sh
ps axc|awk "{if (\$5==\"$1\") print \$1}"|tr '\n' ' '
echo
</code>

also someone suggested that one could just killall <processname>
that's good but sometimes you might want to do
ps -p `pidof processname`
or something like that
or perhaps your just old and you are used to doing
kill -3 `pidof processname`
killall is good, pidof is good too. it's all about choice. that's what open source is all about.



[ Reply to This | # ]
Create a pidof command to find PID numbers easily
Authored by: ClarkGoble on Jun 19, '03 04:23:50PM

It's also useful if you've accidentally started too many processes of a command and just wish to kill one or two of them. Killall isn't that helpful there.



[ Reply to This | # ]
Create a pidof command to find PID numbers easily
Authored by: idle on Jun 19, '03 06:23:19PM

it's always the simplest things that are the best. After having used various unices over the last 10 years, I've always ps|grep'd.

sigh. it makes me feel like a newbie all over again.



[ Reply to This | # ]
Create a pidof command to find PID numbers easily
Authored by: adamjacobmuller on Jun 19, '03 08:54:52PM

not really, piping ps|grep is safe. I have been using linux for a decade and posix be dammed i have to re-learn commands all over again!
one of my favorite features is ps -C in linux this shows you all the proccess with the the specified name in darwin this has some obscure function that messed me up for two weeks i couldnt understand why it didn't work!!!!

so for you who is used to simply ps|grepping that's sometimes slower but better! becuse whether you move from darwin, to FreeBSD, to irix or linux or whatever i can assure you that ps and grep will still be present and will function in generally the same way (axuw commands generally are standardized at least)

I want to state so there is no confusion that this, someone who has used various unixes for a decade, love OSX any less! i love my little powerbook (12'). and i love the killer os that powers it. and critics be dammed even if microsoft cant make a decent os I happen to think that word is decent. and gimp will never live up to adobe photoshop. and ps2pdf never did as good a job as adobe acrobat! but at least i could type an entire document without the computer crashing, transform that 120mb tiff file, convert that 80page graphics intensive file to pdf without the computer crashing or locking up. but it was never the same. I missed that tactile response that windows gave me. I missed things looking good in linux. I reaized that things looking good took up too much processor power and i used the stripped down window manager now with my new powerbook i have the best of every world. Plus wireless networking that kicks butt! I will never abandon linux but at least I will never have to touch my mothers windows notebook again to get on that "supported browsers only" site.

got a wee bit off topic there... :-)



[ Reply to This | # ]
making pidof case insensitive?
Authored by: madgnome on Jun 20, '03 08:54:20AM

Hi, not to appear too lazy or stupid, but is there a way to make pidof case insensitive? I hate having to remember if something like itunes is iTunes or Itunes or iTuNeS or whatever.

I've always been a 'ps | grep -i' kind of person.

---
--Jeff



[ Reply to This | # ]
making pidof case insensitive?
Authored by: adamjacobmuller on Jun 21, '03 06:29:23PM

#!/bin/sh
ps axc|tr '[:upper:]' '[:lower:]'|awk "{if (\$5==\"`echo $1|\
tr '[:upper:]' '[:lower:]'`\") print \$1}"|tr '\n' ' '
echo


this makes all the output of ps axc lowercase then makes all your input lowercase so that it always matches regardless of case, this can be good this can be bad... i wonder if this is how the real pidof works? <<ssh's to his linux box to check>>
it's not, so it's probably not the best idea to do this, then again in most enviroments it would be unusual to have an uppercase letter... anywhere in the filename of a program.... so perhaps this isnt so bad.... it could just lead to false-positives.



[ Reply to This | # ]
making pidof case insensitive?
Authored by: dpwk on Jun 25, '03 06:16:23PM

My solution, short and reasonably sweet (I call it pidf):


#!/bin/sh
ps axc | grep -i $1 | awk "{ print \$1}";



[ Reply to This | # ]