How to list and kill threads/processes in Perl without using external command(s)? - multithreading

I need to kill some processes and threads (if exist) in Perl application, but I don't want to use external command(s) such as ps, grep, awk, cut, uniq or kill.
My current code is:
my $appName = $0;
$appName =~ s/^.*\/([^\/]*)$/$1/;
$_ = qx(kill -9 `ps -eLao pid,command | grep '$appName\[ 0-9\]*\$' |
grep -v grep | awk '\$1 != $$' | cut -d' ' -f1 | uniq` 2>&1);
I am using VPS, so my memory is limited. The code above sometimes returns undef, as system cannot allocate memory for call of external command(s). I am looking for alternative solution without using external command(s).

Use internal kill commands like kill and the /proc fs or some modules like Win32::Process::List or anything in the Proc:: namespace.

Related

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.*'

How to turn off auto add escape character feature in Linux

I am trying to pass a command to a remote server using ssh. while my commands have some characters like ", $, ', \ which often requires a backslash as a escape character except ' (single quote), but the system is automatically taking an escape character \ before the single codes while execution. Can some one help me how to turn off this.
OS : RHEL
my Code :
ssh -q $server "ps -ef | grep mongo | grep conf | awk '{print \$(NF-2)}'
While execution, the code becomes
ssh -q $server "ps -ef | grep mongo | grep conf | awk \'{print $(NF-2)}\'"
I need to turn off this feature
Your analysis isn't really correct. Anyhow, there is no particular reason to run Awk remotely, or grep at all here (because Awk does all of that nicely and efficiently with a very minor refactoring):
ssh -q "$server" ps -ef |
# This runs locally instead
awk '/mongo/ && /conf/ {print $(NF-2)}'

Is it possible to find which process is using OPENSSL in linux?

Suppose, one process is running and accessing OPENSSL shared library to perform some operation. Is there any way to find the pid of this process ?
Is there any way to find on which core this process is running ?
If possible, does it require any special privilege like sudo etc?
OS- Debian/Ubuntu
Depending on what exactly you want, something like this might do:
lsof | grep /usr/lib64/libcrypto.so | awk '{print $1, $2}' | sort -u
This essentially:
uses lsof to list all open files on the system
searches for the OpenSSL library path (which also catches versioned names like libcrypto.so.1.0)
selects the process name and PID
removes any duplicate entries
Note that this will also output processes using previous instances of the shared library file that were e.g. updated to a new version and then deleted. It also has the minor issue of outputting duplicates when a process has multiple threads with different names.
And yes, this may indeed require elevated privileges, depending on the permissions on your /proc directory.
If you really do need the processor core(s), you could try something like this (credit to dkaz):
lsof | grep /usr/lib64/libcrypto.so | awk '{print $2}' |
xargs -r ps -L --no-headers -o pid,psr,comm -p | sort -u
Adding the lwp variable to the ps command would also show the thread IDs:
lsof | grep /usr/lib64/libcrypto.so | awk '{print $2}' |
xargs -r ps -L --no-headers -o pid,lwp,psr,comm -p
PS: The what-core-are-the-users-of-this-library-on requirement still sounds a bit unusual. It might be more useful if you mentioned the problem that you are trying to solve in broader terms.
thkala is almost right. The problem is that the answer is half, since it doesn't give the core.
I would run that:
$ lsof | grep /usr/lib64/libcrypto.so |awk '{print $2}' | xargs ps -o pid,psr,comm -p

Get pid of last started instance of a certain process

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

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