Get pid of last started instance of a certain process - linux

I have several instances of a certain process running and I want to determine the process id of the one that has been started last.
So far I came to this code:
ps -aef | grep myProcess | grep -v grep | awk -F" " '{print $2}' |
while read line; do
echo $line
done
This gets me all process ids of myProcess. Somehow I need to compare now the running times of this pids and find out the one with the smallest running time. But I don't know how to do that...

An easier way would be to use pgrep with its -n, --newest switch.
Select only the newest (most recently started) of the matching
processes.
Alternatively, if you don't want to use pgrep, you can use ps and sort by start time:
ps -ef kbsdstart

Use pgrep. It has a -n (newest) option for that. So just try
pgrep -n myProcess

Related

Getting User from processid when multiple user processes exist

I'm trying to tweak a bash script to pull back PID's of the individual application accounts when there are multiple applications running as a masterId. This used to run under individual user accounts, but recent changes have forced the applications to all run under a combined "masterId", but still maintain a unique application Id that I can grep against.
Normally
pgrep -u "appId"
would give me a single PID. Now I have to run:
pgrep -u "masterId"
it returns all of the PID's (each one is it's own application).
1234
2345
3456
I'm trying to come up with a command to bring me back just the PID of the appAccount(n) so I can pipe it into other useful commands. I can do a double grep (which is closer to what I want):
ps aux | grep -i "masterId" | grep -i "appAccount(n)"
and that will get me the entire single process information, but I just want the PID to do something like:
ps aux | grep -i "masterId" | grep -i "appAccount(n)" | xargs sudo -u appAccount(n) kill -9
How do I modify the initial above command to get just the PID? Is there a better way to do this?
pgrep --euid "masterId" --list-full | awk '/appAccount(n)/ {print $1}'
Output the full process command line, then select the one with the desired account and print the first field (pid).

Getting specific PID from CentOS Journalctl

I'm writing a bash script that will print on the screen all the latest logs from a service that has already died (or still lives, both situations must work). I know its name and don't have to guess.
I'm having difficulty getting the latest PID for a process that has already died from journalctl. I'm not talking about this:
journalctl | grep "<processname>"
This will give me all the logs that include processname in their text.
I've also tried:
journalctl | pgrep -f "<processname>"
This command gave me a list of numbers which supposedly should include the pid of my process. It was not there.
These ideas came from searching for previous questions. I haven't found a question that answers specifically what I asked.
How can I extract the latest PID from journalctl for a specific process?
I figured this out.
First, you must be printing your PID in your logs. It doesn't appear there automatically. Then, you can use grep -E and awk to grab exactly the expression you want from your log:
Var=$(journalctl --since "24 hours ago" | grep -E "\[([0-9]+)\]" | tail -n 1 | awk '{print $5}' | awk -F"[][{}]" '{print $2}'
This one-liner script takes the logs from the last 24 hours, grep with -E to use an expression, tail -n 1 to grab the last most updated line from those results and then, using awk to delimit the line and grab the exact expression you need from it.

kill multiple process at once works only manually not from inside script

I'm trying to kill multiple process at the same time.
Im using this simple for loop to kill the process by PID number.
for i in $(ps -ejH | grep omn_bdxtrc|awk '{print $1}'); do kill ${i}; done
The loop works fine if I enter it manually in the terminal.
but if I want to us it from inside an file (*.sh) it returns this output.
/functions.sh: line 231: kill: 25211
25698
27930
8477
5018
16383
13488
2403
10963 18796: arguments must be process or job IDs
have tried multiple ways that works manually, but not from the file.
Any ideas why this is happening?
Thanks in advance.
It looks like the PIDs are being passed as a single argument delimited by line breaks, which kill does not seem to like.
I would simplify the approach by removing the loop completely and just passing the PIDs to kill via xargs:
ps -ejH | grep omn_bdxtrc | awk '{print $1}' | xargs kill
Alternatively (if you don't have or don't want to use xargs for some reason), you can keep your current loop and just sanitize the output from awk by changing all possible line breaks to spaces using tr:
for i in $(ps -ejH | grep omn_bdxtrc | awk '{print $1}' | tr '\n' ' '); do kill ${i}; done
But this is not that elegant.
Probably the most elegant solution would be to use killall, assuming you know the exact name of the process:
killall omn_bdxtrc
Or if you don't know the exact name and need to match a part of it:
killall --regexp '.*omn_bdxtrc.*'

Shell script to kill process with highest PID

I would like to kill a specific java-process via a shell script.
I know that kill -9 $(pidof java) will kill it, yet it will also kill all other java processes. I know that the java-process I would like to kill is the one that was started as the last one.
So if I use pidof java, I will retrieve a set of PIDs, how can I assign that to a list (or sth similar) and the get the last element of it?
Thanks
I am kind of a newbie for Shell scripts, but here is my solution:
kill -9 `ps aux | grep [j]ava | awk '{ print $2 }' | sort -nr | head -n 1`
ps aux prints the current processes
grep [j]ava finds the processes containing "java" inside, except the current grep command
awk { print $2 } retrieves the second column of ps, which is the pid
sort -nr sorts the output by descending order (n is for numerical)
head -n 1 gets the first line of the sorted pids, which is the highest

How to give arguments to kill via pipe [duplicate]

This question already has answers here:
How to pass command output as multiple arguments to another command
(5 answers)
Closed 1 year ago.
I need to search for a certain process and kill that process. I wrote a command like this:
ps -e | grep dmn | awk '{print $1}' | kill
Where the process name is dmn. But it is not working. How can I find processes by name and kill them.
kill $(ps -e | grep dmn | awk '{print $1}')
In case there are multiple processes that you want to remove you can use this:
ps -efw | grep dmn | grep -v grep | awk '{print $2}' | xargs kill
Note: You need to remove grep process itself from the output, that's why grep -v grep is used.
You could use
pkill dmn
if your system has the pkill command.
Just adding on others, but I like using awk's regex features capacity:
kill $(ps | awk '/dmn/{print $1}')
If you have the pidof command on your system ( I know shells such as ZSH come with this by default, unless I'm mistaken), you could do something like.
kill -9 $(pidof dmn)
You might not need pipe for this, if you have pidof command and know the image name, I did it like this:
kill $(pidof synergyc)
$() I understand this as it converts that output to a variable that kill can use, essentially like pipe would do. Shorter and easier to understand than some other options but also maybe less flexible and more direct.
for procid in $(ps -aux | grep "some search" | awk '{print $2}'); do kill -9 $procid; done
hello friends .. we can do it using for loop .
"Some search" is here any process name you want to search, for example "java" so let say count of java process is 200+ so killing one by one will be too typical .
so you can use above command.
Thanks.
You can also use killall:
killall dmn
Use pgrep with -f option.
kill $(pgrep -f dmn)

Resources