![]() ![]() Let’s see the basic working of the ps -ef command by executing the script: ps -ef The output shows a list of processes. It seems you are using a version of ps that a) does not accept -ef and b) does not print a useful error message. The first command, ps (using the -ef options for a full listing of all processes), finds all of the processes running on the server. ![]() In this case, 'pgrep' won't be reliable (if usable at all) for identifying the exact process you need. The ps -ef command displays a list of processes, with each process occupying one line. Imagine a machine where several Tomcat instances are running with processes like: tomcat 154304 1 8 10:43 ? 00:00:59 /usr/lib/jvm/jre/bin/java .file=/somewhere/conf/logging.properties =true =file:/dev/./urandom =2048 = .SecurityListener.UMASK=0027 -Xms512M -Xmx1024M -server -XX:+UseParallelGC = -classpath. This make the solution more reliable than the one that 'pgrep' offers returning just a PID *) # other UNIX flavors get a minimalist version.įor a 'fnord' both the regular expression nord or pgrep usage may work.īut for a generic use, the regex is by far more flexible, allowing to get more information on the output in case you need to refine the search. In case grep returns no lines (grep return code 1), I abort the script if I get 1 line I invoke A () or B () if more than 1 line. Also, the head command can easily print the first n lines. ![]() when grepping for anything and always enclose in to avoid shell misinterpretation. ![]() & echo "$ts: not running, restarting.The simplest shell-agnostic way to do this would be to store it in a variable first: PS_OUTPUT="$(ps aux)" echo "$PS_OUTPUT" |grep fnordįrom my rc files, I have a case-insensitive version that takes grep's options: psl() We know that ps -ef grep won’t work just because grep cannot output the first line. Command and output ps -efgrep tgu root 1884 29789 0 15:52:42. If you have a newer kernel it should support /proc/pid/smaps which gives you some detailed information on each processes memory usage. Using grep with the ps aux command requires us. 9 Answers Sorted by: 8 Obtaining memory usage through ps is pretty unreliable. Putting it all together your script could be reworked like this: #!/bin/bash To list all the processes, we use the ps command with the BSD aux switch (notice there is no hyphen prefix). program is named foobar and you want your $1 to be just foo you can add the -f switch to pgrep: ] & return 1 || return 0 If you're looking for a partial match, i.e. Share Improve this answer Follow answered at 17:35 Dominique 16. But you can enable it for other commands as. You can achieve almost everything in PROCESS_NUM with this one-liner: & return 1 || return 0 4 Answers Sorted by: 2 You can use the command pgrep, where also the switch -w can be used: pgrep -w 'rv' The final result is the process ID of the rv process. On bash, fuzzy completion is enabled only for a predefined set of commands ( complete grep fzf to see the list). ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |