Active Processes running except the current process - shell

If i write ps -ef, then it returns all the processes running currently. If i type ps -ef | grep xxx, then it returns all the processes running with substring xxx. but ps -ef | grep xxx is also a current process, so it returns grep xxx in the list too.
I just want to eliminate grep xxx from the list.
Can anyone help me with this.
Thanks

Try doing this :
pgrep -fl xxx
Another solution
ps -ef | grep '[x]xx'
this is a simple regex trick to avoid repetition
pgrep is packaged in procps, on Debian :
$ LANG=C apt-cache show procps
Package: procps
Priority: important
Section: admin
Installed-Size: 760
Maintainer: Craig Small <csmall#debian.org>
Replaces: bsdutils (<< 2.9x-1), watch
Provides: watch
Depends: libc6 (>= 2.3.4), libncurses5 (>= 5.7+20100313), libncursesw5 (>= 5.7+20100313), lsb-base (>= 3.0-10), initscripts
Recommends: psmisc
Conflicts: libproc-dev (<< 1:1.2.6-2), pgrep (<< 3.3-5), procps-nonfree, w-bassman (<< 1.0-3), watch
Size: 249178
Description: /proc file system utilities
This package provides command line and full screen utilities for browsing
procfs, a "pseudo" file system dynamically generated by the kernel to
provide information about the status of entries in its process table
(such as whether the process is running, stopped, or a "zombie").
.
It contains free, kill, pkill, pgrep, pmap, ps, pwdx, skill, slabtop,
snice, sysctl, tload, top, uptime, vmstat, w, and watch.
Homepage: http://procps.sf.net/
Tag: admin::monitoring, interface::commandline, interface::text-mode, role::program, scope::utility, uitoolkit::ncurses, use::monitor, works-with::software:running

Instead of saying
ps -ef | grep xxx
say
ps -ef | grep [x]xx
and you wouldn't see grep xxx in the output. (Essentially put the first character of the desired word as a character class [].)

Related

How can I grep a count of available updates from yum check-updates

I want to check if updates are available, and if so, perform some steps before I install the updates. I run yum check-updates to see a list of updates available for installed packages, but I'd like to grep this and get a count that I can use for some logic in a bash script. So ideally, I would like to grep that output of check-updates and return 0 if there are no updates, or if five updates are available then I would like the grep to return 5.
How can I grep this to return the count?
I like a simpler approach.
"yum -q" reduces the output from yum so it only displays a list of packages. Combine this with "wc -l" to count the number of lines output.
So to get a count of packages requiring updates I would run
sudo yum -q check-update | wc -l
Are you aware of grep -c? I've just created some nonsense file, giving following result:
Prompt> grep "AA" test.txt
1A01 TCCTTGAAAG
TCAACAAGAA
TCGCAAA
TTTAAAGTCGT
GGCGGAATCAATAC
GATGGAATATGCGCC
If I use grep -c, this is the result:
Prompt> grep -c "AA" test.txt
6
In case this does not answer your question completely, please edit your question and add some more information, just to show what you are looking for.
Also, please be aware that adding | wc -l behind every UNIX command reads the amount of results of that command.
This combination of awk and grep gives the count of available updates for installed packages:
yum check-updates | awk 'p;/^$/{p=1}' | grep -c "\."
This was based on the info in How to get just a list of yum updates
The -q for quiet is great but you may also want to grep out any blank lines and also the trailing Loaded plugins message. This works nicely for an accurate count:
yum check-update -q|egrep -v "Loaded plugins: langpacks, product-id, subscription-manager|^$"|wc -l

Bash redirect stderr to file after for loop

I have the following script that checks for installed/uninstalled packages:
#!/bin/bash
DEPENDENCIES="build-essential pkg-config qt4-qmake libqt4-dev libavformat-dev libavcodec-dev"
for dep in $DEPENDENCIES; do
dpkg -l $dep | grep "$dep"
done | sort
the result is:
dpkg-query: no packages found matching libavformat-dev
dpkg-query: no packages found matching libavcodec-dev
ii build-essential 12.1ubuntu2 amd64 Informational list of build-essential packages
ii pkg-config 0.29.1-0ubuntu1 amd64 manage compile and link flags for libraries
un libqt4-dev (no description available)
un qt4-qmake (no description available)
which is what I expect. I would then like to redirect stdout and stderr to a file depend.out. So I modify the last line of the script to: done | sort &> depend.out. But the contents of depend.out are:
ii build-essential 12.1ubuntu2 amd64 Informational list of build-essential packages
ii pkg-config 0.29.1-0ubuntu1 amd64 manage compile and link flags for libraries
un libqt4-dev <none> <none> (no description available)
un qt4-qmake <none> <none> (no description available)
Why are the lines in bold (uninstalled packages) missing even if I use the redirection operator &>?
most probably these lines were written to your stderr therefore they weren't redirected to the pipe (instead written on the tty)
if you want stderr to be processed by the pipe as well you need to redirect it to stderr manually before piping (as pipe only acts on stdout)
try this one:
#!/bin/bash
DEPENDENCIES="build-essential pkg-config qt4-qmake libqt4-dev libavformat-dev libavcodec-dev"
for dep in $DEPENDENCIES; do
dpkg -l $dep 2>&1 | grep "$dep"
done | sort
To redirect stderr to stdout, use:
command 2>&1
Demonstration:
ls unexisting-path 2>&1 | cat > /dev/null
Here, ls will produce an error output. This output is redirected to stdout, so it gets caught by the pipe | and sent to cat, which outputs it to stdout too. To prove it, > /dev/null is added, and as expected, nothing is displayed.

(Mac OS X) Why is this process unkillable and its pid varies over time?

Today when I tried to kill all the processes related to docker, I noticed something really funny:
➜ ~ ps aux | grep docker
Caesar 73944 0.0 0.0 2423372 220 s000 R+ 6:49PM 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn docker
➜ ~ kill 73944
kill: kill 73944 failed: no such process
➜ ~ ps aux | grep docker
Caesar 74064 0.0 0.0 2432788 572 s000 R+ 6:50PM 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn docker
➜ ~
I used ps aux | grep docker to find the process related to docker, although I am not sure if it is really a docker process. The funny thing is that: when I kill it using pid, I couldn't do it, as you can see from the screenshot. Also, I noticed that the pid changed at the second ps command.
I know the problem may look stupid, however, I couldn't find anything on Google because I didn't know how to describe it. Maybe you can help me with that. Thanks ; )
Update
Thanks for the comments under this question. I noticed that the pid belongs to the grep process, and no wonder why the pids differ each time. Thank y'all for the help!
You're seeing the grep process, not the docker process. Whenever you use grep to filter the output of ps you'll run into this issue. To avoid listing the grep process, the canonical solution is to put square brackets around the first character in the target process name:
ps aux | grep '[d]ocker'
Since the search string contains square brackets (which don't effectively alter the regular expression) it will no longer be a match for the regular expression itself when found in the name of the grep process.

Trying to kill process by shell scripting

I want to kill process through shell scripting it is giving me error. Here is what i tried so far:
When i try to kill memcache it give me error like "kill: No such process" , i used below command:
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
or if try like below:
kill -9 $(pidof memcache)
i get error like below:": arguments must be process or job IDs"
When i run directly on command prompt process is running:
ring#ubuntu:~/parasol$ ps aux | grep memcache
memcache 873 0.0 0.0 323220 1188 ? Sl 22:56 0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
ring 1714 0.0 0.0 9384 920 pts/0 S+ 23:45 0:00 grep --color=auto memcache
I reff to https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-name
AND
Shell script to capture Process ID and kill it if exist
My Ubuntu Version:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"
Acutally
ps -ef | grep memcache
will give two lines..
so you can go this way
ps -ef | grep memcache | grep -v "grep" | awk '{print $2}' | xargs kill;
This can get you exact one PID
if I has to do I would break it in 2 lines in start
#!/bin/bash
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
echo $PID
#to check PID is right
kill -9 $PID
save it in scrip files say test.sh
then on terminal
chmod +x test.sh
then
./test.sh
You can use these commands:
pkill memcached
or
pgrep memcached | xargs kill -9
or
killall memcached.
pgrep, pkill - look up or signal processes based on name and other attributes.
Your first command might be killing itself before having chance to kill the target processes.
For a reliable way, run pkill /usr/bin/memcached or pkill -9 /usr/bin/memcached although the latter is a bad practice.
For the first command, you see that there are two processes that matches the pattern you grep for. The actual memcached process and your grep process. That is probably the reason for the error of the first command line.
Try narrowing the search down, for example by grepping for e.g. "^memcache.*/usr/bin/memcached".
The problem with the second error is that you're calling pidof with the username instead of the process name, so the command is essentially kill -9 without any process id. Try instead e.g. pidof memcached to get the process id of the correct process.
ps -ef | grep "memcache" | awk '{print $2}' | xargs kill;
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print $2}'`
#...
First, you could use cut instead of awk in this case. No need to use a tank to kill a fly ;)
Then, why is it necessary to brutally kill memcache? You have a daemon to stop it :
/etc/init.d/memcached stop
service memcached stop

How to kill fastcgi-mono-server4.exe from a rake file?

I'm working on automated deployment using Rake of a mono asp.net website to ubuntu server with nginx.
As far as I've discovered fastcgi-mono-server4.exe can't be stopped gracefully and must be terminated. I currently do that manually in htop. How do I do that from rake or the shell?
I've only a few hours of experience with rake & ruby and a few weeks of linux but made a lot of progress already, however somethings are eluding me even potentially obvious ones like this.
To find the running mono process ids in Linux:
ps ax | grep mono
This will give you something like:
user 1452 0.0 0.0 9396 876 pts/4 S+ 17:33 0:00 grep mono
user 2810 98.2 16.1 967424 330432 ? Sl Mar30 7866:50 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.logappconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
The 2nd column contains the process id which you can kill with:
sudo kill -9 2810
Excluding grep from ps output using a character class
Thanks to #Yevgeniy comment, you can exclude grep from the ps output by using a grep character class as explained in this question, e.g:
ps aux | grep [m]ono
Which will exclude the grep process from the ps output and give you something like:
2810 ? Sl 7861:07 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
This now means if you only have one process of mono running you can now kill it with this 1-liner:
sudo kill -9 $(ps aux | grep '[m]ono' | awk '{print $2}')

Resources