I have a couple ruby scripts running on my machine and some other ruby processes. The only way I can differentiate them with top is by doing top -c (so I can see the command, otherwise everything is just 'ruby').
I want to be able to watch how many scripts are running so I can restart them if one fails.
I am thinking I can do this with top -c -n 1 | grep "script-name" but I can't figure out how to tail -f that or if that command is the best way to do it in the first place.
I think that top it's not the best choice here, because it's an interactive command and you can't really pipe its whole output (probably there is a way). One of the fair enough ways to do it would be using ps:
ps -e -o pid,cmd | grep "script-name"
If you want to periodically investigate this, you can also use watch:
watch 'ps -e -o pid,cmd | grep "script-name"'
In general, it's a bad practice to grep ps output, but I suppose in your case will work. If you only want the number of running processes that match against a pattern or you just want their PIDs, you'd better go with pgrep.
pgrep "script-name"
Related
I am using following Linux command to kill all vim process,
ps -ef | grep "gvim" | grep -v grep | awk '{print $2}' | xargs kill
Above command is working except it outputs:
Vim: Caught deadly signal TERM
Is there another clean way to kill it?
Well as long as you're fine with losing all unsaved changes and halting any background jobs mid-go, you can send a different signal from the kill command with the -s option. Signal 15, SIGTERM, is the default one; you can do kill -s 9 to murder the processes outright. Here is a good list of signals for reference.
So your final command would look like:
ps -ef | grep "gvim" | grep -v grep | awk '{print $2}' | xargs kill -s 9
This does not generate any warnings about deadly signals. It's also a pretty nasty way of doing things, and could lead to some gnarly behavior if you've got a non-trivial vim setup.
As #moopet said in the comments, you can still use SIGTERM if you redirect the command's output to /dev/null. This won't be any nicer than SIGKILL in that Vim won't automatically save your files, but it may give any background tasks it was doing some time to clean up.
Alternatively, if you're willing to change your setup, you can always run a vim as a server and issue save-and-close commands to it from the terminal. There is a great explanation over on the Vim SE site.
Is there way of counting the number of processes being run by a user in the unix/linux/os x terminal?
For instance, top -u taha lists my processes. I want to be able to count these.
This will show all of the users with their counts (I believe this would be close enough for you. :)
ps -u "$(echo $(w -h | cut -d ' ' -f1 | sort -u))" o user= | sort | uniq -c | sort -rn
You can use ps to output it and count the number using wc, as:
ps -u user | sed 1d | wc -l
You can also dump top output and grep it, something like:
top -u user -n1 | grep user | wc -l
I'm somewhat new to *nix, so perhaps I did not fully understand the context of your question, but here is a possible solution:
jobs | wc -l
The output of the above command is a count of all the processes reported by the jobs command. You can manipulate the parameters of the jobs command to change which processes get reported.
EDIT: Just FYI, this would only work if interested in commands originating from a particular shell. If you want more control in looking at system-wide processes you probably want to use ps as others have suggested. However, if you use wc to do your counting, make sure you take into account any extraneous white space jobs, ps or top may have generated as that will affect the output of wc.
How can I list all Java processes in bash?
I need an command line. I know there is command ps but I don't know what parameters I need to use.
try:
ps aux | grep java
and see how you get on
Recent Java comes with Java Virtual Machine Process Status Tool "jps"
http://download.oracle.com/javase/1.5.0/docs/tooldocs/share/jps.html
For example,
[nsushkin#fulton support]$ jps -m
2120 Main --userdir /home/nsushkin/.netbeans/7.0 --branding nb
26546 charles.jar
17600 Jps -m
jps -lV
is most useful. Prints just pid and qualified main class name:
2472 com.intellij.idea.Main
11111 sun.tools.jps.Jps
9030 play.server.Server
2752 org.jetbrains.idea.maven.server.RemoteMavenServer
Starting from Java 7, the simplest way and less error prone is to simply use the command jcmd that is part of the JDK such that it will work the same way on all OS.
Example:
> jcmd
5485 sun.tools.jcmd.JCmd
2125 MyProgram
jcmd allows to send diagnostic command requests to a running Java
Virtual Machine (JVM).
More details about how to use jcmd.
See also the jcmd Utility
You can use single command pgrep as well (doesn't require you to use pipes and multiple commands):
pgrep -fl java
For better output format check this command:
ps -fC java
This will return all the running java processes in linux environment. Then you can kill the process using the process ID.
ps -e|grep java
ps aux | grep java
or
$ ps -fea|grep -i java
pgrep -l java
ps -ef | grep java
If I want simply list java processes, use:
ps -A | grep java
ps axuwww | grep java | grep -v grep
The above will
show you all processes with long lines (arg: www)
filter (grep) only lines what contain the word java, and
filter out the line "grep java" :)
(btw, this example is not the effective one, but simple to remember) ;)
you can pipe the above to another commands, for example:
ps axuwww | grep java | grep -v grep | sed '.....' | while read something
do
something_another $something
done
etc...
When I want to know if a certain Java class is getting executed, I use the following command line:
ps ww -f -C java | grep "fully.qualified.name.of.class"
From the OS side view, the process's command name is "java". The "ww" option widens the colum's maximum characters, so it's possible to grep the FQN of the related class.
jps & jcmd wasn't showing me any results when I tried it using using openjdk-1.8 on redhat linux. But even if it did it only shows processes under the current user which doesn't work in my case. Using the ps|grep is what I ended up doing but the class path for some java apps can be extremely long which makes results illegible so I used sed to remove it. This is a bit rough still but removes everything except: PID, User, java-class/jar, args.
ps -o pid,user,cmd -C java | sed -e 's/\([0-9]\+ *[^ ]*\) *[^ ]* *\([^$]*\)/\1 \2/' -e 's/-c[^ ]* [^ ]* \|-[^ ]* //g'
Results look something like:
PID USER CMD
11251 userb org.apache.zookeeper.server.quorum.QuorumPeerMain ../config/zookeeper.properties
19574 userb com.intellij.idea.Main
28807 root org.apache.nifi.bootstrap.RunNiFi run
28829 root org.apache.nifi.NiFi
An alternative on windows to list all processes is:
WMIC path win32_process where "Caption='java.exe'" get ProcessId,Commandline
But that is going to need some parsing to make it more legible.
There's a lot of ways of doing this. You can use java.lang.ProcessBuilder and "pgrep" to get the process id (PID) with something like: pgrep -fl java | awk {'print $1'}. Or, if you are running under Linux, you can query the /proc directory.
I know, this seems horrible, and non portable, and even poorly implemented, I agree. But because Java actually runs in a VM, for some absurd reason that I can't really figure out after more then 15 years working the JDK, is why it isn't possible to see things outside the JVM space, it's really ridiculous with you think about it. You can do everything else, even fork and join child processes (those were an horrible way of multitasking when the world didn't know about threads or pthreads, what a hell! what's going in on with Java?! :).
This will give an immense discussion I know, but anyways, there's a very good API that I already used in my projects and it's stable enough (it's OSS so you still need to stress test every version you use before really trusting the API): https://github.com/jezhumble/javasysmon
JavaDoc: http://jezhumble.github.io/javasysmon/, search for the class com.jezhumble.javasysmon.OsProcess, she will do the trick. Hope it helped, best of luck.
ps -eaf | grep [j]ava
It's better since it will only show you the active processes not including this command that also got java string the [] does the trick
I use this (good on Debian 8):
alias psj='ps --no-headers -ww -C java -o pid,user,start_time,command'
The following commands will return only Java ProcessIDs. These commands are very useful especially whenever you want to feed another process by these return values (java PIDs).
sudo netstat -nlpt | awk '/java/ {print $7}' | tr '/java' ' '
sudo netstat -nlpt | awk '/java/ {print $7}' | sed 's/\/java/ /g'
But if you remove the latest pipe, you will be noticed these are java process
sudo netstat -nlpt | awk '/java/ {print $7}'
sudo netstat -nlpt | awk '/java/ {print $7}'
To know the list of java running on the linux machine.
ps -e | grep java
I am trying to use the ps -o command to get just specific info about processes matching a certain name. However, I am having some issues on this, when I try to use this even to just get all processes, like so, it just returns a subset of what a normal ps -ef would return (it doesn't return nearly the same number of results so its not returning all running processes)
ps -ef -o pid,time,comm
I want to try something like this (below) but incorporate the ps -o to just get specific info from it (just the PID)
ps -ef |grep `whoami`| grep firefox-bin
Any advice is appreciated as to how to do this properly, thanks
This will get you the PID of a process by name:
pidof name
Which you can then plug back in to ps for more detail:
ps -p $(pidof name)
This is a bit old, but I guess what you want is: ps -o pid -C PROCESS_NAME, for example:
ps -o pid -C bash
EDIT: Dependening on the sort of output you expect, pgrep would be more elegant. This, in my knowledge, is Linux specific and result in similar output as above. For example:
pgrep bash
ps -fC PROCESSNAME
ps and grep is a dangerous combination -- grep tries to match everything on each line (thus the all too common: grep -v grep hack). ps -C doesn't use grep, it uses the process table for an exact match. Thus, you'll get an accurate list with: ps -fC sh rather finding every process with sh somewhere on the line.
Sometimes you need to grep the process by name - in that case:
ps aux | grep simple-scan
Example output:
simple-scan 1090 0.0 0.1 4248 1432 ? S Jun11 0:00
Sorry, much late to the party, but I'll add here that if you wanted to capture processes with names identical to your search string, you could do
pgrep -x PROCESS_NAME
-x Require an exact match of the process name, or argument list if -f is given.
The default is to match any substring.
This is extremely useful if your original process created child processes (possibly zombie when you query) which prefix the original process' name in their own name and you are trying to exclude them from your results. There are many UNIX daemons which do this. My go-to example is ninja-dev-sync.
I have the following bash script, that lists the current number of httpd processes, and if it is over 60, it should email me. This works 80% of the time but for some reason sometimes it emails me anyways when it is not over 60. Any ideas?
#!/bin/bash
lines=`ps -ef|grep httpd| wc -l`
if [ "$lines" -gt "60" ]
then
mailx -s "Over 60 httpd processes" me#me.com < /dev/null
fi
There is a delay between checking and emailing. In that time, some httpd processes might finish, or start, or both. So, the number of processes can be different.
You are including the grep process in the processes (most of the time, it could happen that the ps finishes before grep starts). An easy way to avoid that is to change your command to ps -ef | grep [h]ttpd. This will make sure that grep doesn't match grep [h]ttpd.
On linux, you have pgrep, which might be better suited for your purposes.
grep ... | wc -l can usually be replaced with grep -c ....
If you want to limit the number of httpd requests, I am sure you can set it in apache configuration files.
You've probably thought of this, but ...
At time t0, there are 61.
At time t1, when you read the email, there are 58.
Try including the value of $lines in the email and you'll see.
Or try using /proc/*/cmdline, it might be more reliable.
grep httpd finds all processes that include httpd in their name, including possibly grep httpd itself, and perhaps other ones.
"ps -ef|grep httpd" doesn't find just httpd processes, does it? It finds processes whose full (-f) listing in ps includes the string "httpd".
This probably doesn't solve your issue but you could simplify things by using pgrep instead.
you can do it this way too, reducing the use of grep and wc to just one awk.
ps -eo args|awk '!/awk/&&/httpd/{++c}
END{
if (c>60){
cmd="mailx -s \047Over 60\047 root"
cmd | getline
}
}'