I cannot see the child java process from ruby process - ruby

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`

Related

How do I not show the processes that I can't kill with 'kill + [pid number]' command?

I was working on a project "make a task manager in linux" at school
I used ps -u [username] -o stat,cmd --sort=-pid | awk '{print $2$3$4}' command to get cmd names from the ps command
If I use this command, I see the part of the result like this :
awk{print$2$3$4}
ps-u[username]
when I try to terminate those process using the pid of each process, it won't terminate them because their PID doesn't exist.
How could I not show those awk{print$2$3$4} and ps-u[username] ???
I couldn't think of any idea
ps -u [username] -o stat,cmd --sort=-pid | awk '{print $2$3$4}'
You can't kill them because they were only alive while the commands were running, which was the same command you used to generate that output.
There's a few ways you can suppress these. I think the easiest would be to filter them out in your awk script.:
ps -u [username] -o stat,cmd --sort=-pid | awk '$2!="awk" && $2!="ps"{print $2$3$4}'
JNevill's solution excludes every running awk or ps process. I think it's better to exclude processes on tty. Also, you aren't getting complete commands with how you use awk. I (kind of) solved it using sed.
$ ps -u $USER -o stat,tty,cmd --sort=-pid | grep -v `ps -h -o tty $$` | sed -r 's/.* (.*)$/\1/'
You can test it with the following command. I opened man ps in another terminal.
$ ps -u $USER -o stat,tty,cmd --sort=-pid | grep -v `ps -h -o tty $$` | grep -E '(ps|grep)'
S+ pts/14 man ps
The downside is, besides excluding ps and grep, it excludes your application as well.

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 can I monitor the memory usage of a shell script at runtime?

I haz shell script like this:
while true;do
curl -s example.com | egrep -o 'sth'
sleep 60
done
How can I get the total memory usage of process bash and curl and egrep and sleep at runtime?
you can use the ps command:
ps aux | grep <scriptName>
ps aux | grep "curl -s example"
ps aux | grep "egrap -s 'sth'"
ps aux | grep "sleep 60"
the 3rd and 4th columns are cpu and mem (you can get them using awk)

Killing Process on Remote Host using Fabric

I am writing a script using Fabric which needs to terminate a process remotely.
(this means that the command ends up getting executed as /bin/bash command)
The current code I have is the following:
in a kill.sh file i have
/bin/kill $(ps -ef | grep multiserver.jar | grep -v bin/sh | grep -v /bin/bash | grep -v sh | grep python | grep -v /usr/bin/java | grep -v /usr/bin/python | grep -v sh | awk '{print $2}')
which I run in Fabric on my remote host using the following commands
local("scp " + "kill.sh " + user +"#" + server_address + ":" + directory)
run ("chmod u+x kill.sh")
run("./kill.sh")
However I get the following error message
out: Usage:
[] out: kill [options] <pid> [...]
Fatal error: run() received nonzero return code 1 while executing!
Requested: ./kill.sh
Executed: /bin/bash -l -c "cd ... && ./kill.sh"
Does anyone know what I am doing wrong?
While solving this issue with reading logs with fabric, I wrote command to kill remote processes:
with settings(warn_only=True):
sudo("kill -9 `ps aux | <pipeline of greps> | awk '{print $2}'`")
Hope this helps.

Getting PID of sshd

I am executing sshd in a bash script using
$ /usr/sbin/sshd
How do I get the process ID of this sshd that I executed?
sshd will typically write a PID file; by default this is at /var/run/sshd.pid. You can use this to find the process ID of the listening sshd process. You should be aware that sshd may fork several subprocesses as it works, so what you want really depends on what you intend to do with it.
Try this command:
ps aux | grep -e /usr/sbin/sshd | grep -v grep | tr -s " " | cut -d " " -f2
or
cat /var/run/sshd.pid

Resources