How to get PID of current rake task? - ruby

I'm putting in a reaper line into a rake task to kill some additionally spawned ruby tasks as they somehow creep up on occasion.
system "ps aux | grep 'namespace:taskname' | grep ruby | grep -v grep | awk '{print $2}' | xargs kill -9; echo 'Reaped old namespace:taskname processes.'"
I'd like to add grep -v $PID_OF_CURRENT_TASK in that just to be sure I don't kill the current task that's running as well.
How do I get that PID?

You get the current PID in Ruby with Process.pid

Related

How to use xargs to pass arguments as sudo to given program in terminal shell

I want kill a series process like follow:(rg is a grep alternative)
ps -aux | rg "some_procs" | awk '{print $2}' | xargs kill -9
but It raise an error message show I don't have permission. I also can't add sudo neither after xargs or behind xargs.(seems it will cause xargs assign argus to sudo rather kill).
How do I use sudo in this pipe link to kill those processes?
ps -aux | rg "mytmp" | awk '{print $2}' | xargs sudo kill -9
this command worked.
In addition to
the answer,
if you are using GNU/Linux box, you could have use
pkill so
sudo pkill some_procs
Also note that, otherwise, your can pass many PIDs to kill OK. You don't need all ps default fields, but only PID and name to filter. Last, you can skip the rg part because you can do it in awk directly.
sudo kill -9 $(ps -o pid,args | awk '$2 ~ /some_procs/ {$1}')

Killing a running sh script using pid

I have made an Auto Clicker and was wondering how i would kill it using
kill [pid]
My auto Clicker works like this:
while true [1]; do
xdotool click --repeat 10000 --delay 150 1
done
code I have used to try and terminate running proccess:
ps -ef | grep AutoClicker | grep -v grep | xargs kill -9
I found this code on another post, however i have had no luck with it.
pkill -f AutoClicker or kill $(pgrep -f AutoClicker)
If you run your code:
ps -ef | grep AutoClicker | grep -v grep | xargs kill -9
you should get an error message from kill.
kill expects a list of process ids not usernames, times, and random strings.
You need to filter out everything except the pids with something like:
ps -ef | grep AutoClicker | grep -v grep | awk '{print $2}' | xargs kill
Instead of searching for the process ID, use your own "PID file". I'll demonstrate with sleep in place of xdotool.
echo $$ > /tmp/my.pid
while true [1]; do
echo "clicking"
sleep 5
done
# rm -f /tmp/my.pid
I can run this script in another terminal window or the background. The important thing is that /tmp/my.pid contains the process ID of this running script. You can stop it with:
kill $(</tmp/my.pid)
This interrupts the while-loop. Need to figure out how to remove the PID file...

Kill command won't work correctly in bash script

I was running an ubuntu console, when I type the following command, all the processes would be perfectly killed.
kill -9 $(ps -ef | grep 'job1/' | grep -v grep| awk '{print $2}')
But when I was trying to use crontab to call a script routinely, things went wrong.
#!/bin/bash
pid=$(ps -ef | grep 'job1/' | grep -v grep | awk '{print $2}')
echo $pid
kill -9 $pid
# the following commands were never executed
sleep 5
/data/job1/tomcat8/bin/startup.sh
The result was just like this:
15432 15438
Killed
It seems to just killed the job, but won't execute the following commands. Any idea?
If you are going to make a script that kills things by PID then you need to be very careful that you kill the right things.
You already have grep -v grep to avoid killing the grep itself, but it seems that you have not put in anything to protect against the script killing itself. Since you know your own PID you could grep -v that, but what if you are 123 and one of the things you want to kill is 1234? Probably safer to go by script name.

ps aux auto close app

I'm trying to setup a task to kill certain server processes when the server gets into a weird state such as when it fails to boot one process, but another process gets keeps running and so not everything boots up. This is mainly a task for development so you can do jake killall to kill all processes associated with this project.
I'm having trouble figuring out how to get the pid after doing: ps aux | grep [p]rocess\ name | {HOW DO I GET THE PID NOW?} and then after getting the ID how do I pass that to kill -9 {PID HERE}
The PID is the second column, so you can do
ps aux | grep [p]rocess\ name | awk '{print $2}'
All together,
my_pid=$(ps aux | grep [p]rocess\ name | awk '{print $2}')
kill -9 $my_pid
You could also you killall <program> or pkill <program> or pgrep <program>

Redirecting the output of ps command,getting process id and killing that process using shell script

I want to write a shell script to find the running process for a given user and kill the process by getting the respective process ID.
Its like
ps -ef | grep dinesh
After this, i am getting the output as the following
dinesh 19985 19890 0 11:35 pts/552 00:00:00 grep dinesh
Here 19985 is the process ID. I want to kill that process.
How can i achieve this using script?
I have to parse the ps command output and get the process ID
Thanks in advance.
kill `ps -ef | grep dinesh | awk '{ print $2 }'`
What if there is more than one process defined by the string 'dinesh'? What about the grep process itself? This is a more complete answer
ps -ef | grep dinesh | grep -v grep | awk '{print $2}' | xargs kill -9

Resources