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

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}')

Related

I cannot see the child java process from ruby process

I run from the first irb console:
fork { system 'webdriver-manager start --standalone' }
run from the second irb console:
`ps aux | grep -ie '\-Dwebdriver' | grep -v grep`
So I receive nothing. Though I see the process from terminal:
$ ps aux | grep -ie '\-Dwebdriver' | grep -v grep
... 24762 2.5 0.6 4752104 49180 pts/2 Sl+ 15:44 0:00 java -jar /home/.../.nvm/versions/node/v5.3.0/lib/node_modules/protractor/selenium/selenium-server-standalone-2.48.2.jar -Dwebdriver.chrome.driver=/home/.../.nvm/versions/node/v5.3.0/lib/node_modules/protractor/selenium/chromedriver
Why I cannot see the java process from ruby process?
Thank you.
ps aux will truncate the output to the width of your terminal. I bet if you were to expand your terminal where you run your second irb session, it would show. Use ww so it doesn't truncate.
`ps auxww | grep -ie '\-Dwebdriver' | grep -v grep`

(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.

not able to kill snmpd process in MAC OSX

Trying to Kill snmpd process i MAC OSX not able to do, after every Kill it restart again Automatically, not sure who is starting the process again and again, using MAC OSX operation system
bash$ ps ax |grep snmpd
22214 ?? Ss 0:00.10 /usr/libexec/snmpd -f
22224 s001 R+ 0:00.00 grep snmpd
Kiling the Process_Id
bash$ sudo kill -9 22214
after killing the prcoess again snmpd process is Up
bash$ ps ax |grep snmpd
22231 ?? Ss 0:00.08 /usr/libexec/snmpd -f
22234 s001 S+ 0:00.00 grep snmpd
I tried this so many times not able to Kill the snmpd process,
After doing some research Work i got the answer, iN MAC the snmpd started using launchctl unloading it has resolved the Issue
sudo launchctl unload -w /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist
after executing this command the snmpd process is get stopped

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

Active Processes running except the current process

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 [].)

Resources