4 ms·
Serious question: what would be a better way to do this?
by thaumaturgy 9y ago
Serious question: what would be a better way to do this?
- desdiv 9y agoperl -e 'print `pgrep SomeDaemon `'
- deleted 9y ago[deleted]
- CaptSpify 9y agoI would just recommend pkill: https://en.wikipedia.org/wiki/Pkill https://en.wikipedia.org/wiki/Pkill
- stephen82 9y agoI tend to use the following command: kill -HUP $(pidof SomeDaemon) and if it insists on running, I use: sudo kill -9 $(pidof SomeDaemon) That's it, really.
- jsmthrowaway 9y agopgrep is also useful if you're not trying to kill. They're both from Solaris, and if you have one, you have the other. pgrep | xargs can also be very useful on occasion, depending on what you're doing (mainly debugging).
- vidoc 9y ago> xargs can also be very useful on occasion yes, and xargs has a particularly useful argument -P that allows to do stuff in parallel and probably deserves more love :P
- fanf2 9y agoTop tip! `pkill -v` is not verbose mode! Remember pkill is next to pgrep...
- kodfodrasz 9y agoPowerShell?
- rikkus 9y agoIn a script: Get-Process | Where-Object { $_.ProcessName -eq 'SomeDaemon' } | Stop-Process Or at the command line: ps | ? { $_.ProcessName -eq 'SomeDaemon' } | kill
- deleted 9y ago[deleted]
- emmelaich 9y agoAs per other users, you can use pkill but it's not entirely portable in meaning between OSs. This software had to run on just about every possible Unix out there. Solaris, HP-UX, AIX, many others ... My main point is that using perl and grep, (multiple thereof!) is nuts when you can do it all in perl. Also crazy was using -ef (all processes) when the process of interest was using a known user. So ps -u <user> would be more appropriate. Doing as much in Perl as possible, for portability, you'd do something like ps -u <user>| perl -ane 'm/[S]omeDaemon/ && kill "SIGTERM", $F[1]' -a means autosplit into fields, $F[0], F[1], etc so the pid is $F[1]. See `perldoc -f kill` for the Perl function kill. You can also do the `ps` from within Perl but I don't think you'd be gaining much in readability or portability.
- OskarS 9y agoIf it's something you need to do on a semi-regular basis, using a pidfile seems to me to be the best solution. Instead of trying to be clever about pipelines, just write the pid(s) to a file and use that.
- vidoc 9y agoI often roll with 'pgrep' part of pkill package to fetch the pids of either the ucomm or longname (-l) of the process. pgrep -l 'reg.*ex" | xargs -L1 do-stuff or just pkill if the idea is just to send a signal. To gather particular process metrics, ps can be invoked with process ids (-p), with full control of its output (-o), grep is rarely needed.
- lkirk 9y agoA pid file is the best IMO
- dozzie 9y agodo_something_with_pids $(ps -C SomeDaemon ho pid) Though it's GNU procps.