Kill command does the job but gives error - linux

I'm using shell script to stop a python service and to do that I'm executing the below command.
ps -ef | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9
After executing I'm getting below error:
kill: sending signal to 487225 failed: Operation not permitted
But, when I check the terminal where the process is running, it terminates.
/home/saakhan/Documents/scripts/cluster-helpers.sh: line 75: 526839 Killed python3 src/service.py
Now, in my shell script I'm checking for the error if any, while stopping the service. So, it calls that error function which displays "Failed to stop the Service" even though the service is stopped successfully.
Do I need to change the command for this ?
Note: I have to use 'kill' only and not 'pkill

As #Bodo pointed out, there was more than one PIDs and only one of them got killed while the other showed error as it wasn't started by the same user.
I changed the grep command to filter the process started by the same user and then killed it, so it worked.
Update:
I used this command to successfully run my code and terminate the app as needed.
ps -u | grep service.py | grep -v grep | awk '{print $2}' | xargs kill -9

Related

Killing multiple Mac OS processes dynamically in one command? [duplicate]

This question already has answers here:
How to kill all processes with the same name using OS X Terminal
(6 answers)
Closed 4 years ago.
I have this issue where sometimes .Net Core crashes when I run the debugger in VS Code, and I'm unable to restart the debugger. Even if I quit out of VS Code and go back in, the processes are still open. Rather than restart the machine, I can kill each process manually, but sometimes there are a whole bunch of them, and it gets pretty tedious. For example:
$ ps -eaf | grep dotnet | grep -v grep
16528 ?? 0:02.65 /usr/local/share/dotnet/dotnet /Users/ceti-alpha-v/Documents/NGRM/application/NGRM.Web/bin/Debug/netcoreapp2.0/NGRM.Web.dll
16530 ?? 0:02.75 /usr/local/share/dotnet/dotnet /Users/ceti-alpha-v/Documents/NGRM/application/NGRM.Web/bin/Debug/netcoreapp2.0/NGRM.Web.dll
16532 ?? 0:02.85 /usr/local/share/dotnet/dotnet /usr/local/share/dotnet/sdk/2.1.403/Roslyn/bincore/VBCSCompiler.dll
$ kill 16528 16530 16532
I'd like to delete the processes automatically with a single command, but I'm not sure how to pipe each PID to kill.
You can use xargs like this
ps -eaf | grep dotnet | grep -v "grep" | awk '{print $2}' | xargs kill
or if you want to just kill all dotnet processes
killall dotnet
You can use command Substitution
kill $(ps -eaf | grep dotnet | grep -v grep | awk '{ print $2 }')

Bash script works locally but not on a server

This Bash script is called from a SlackBot using a Python script that uses the paramiko library. This works perfectly when I run the script locally from the bot. It runs the application and then kills the process after the allotted time. But when I run this same script on a server from the SlackBot, it doesn't kill the process. Any suggestions?? The script name is "runslack.sh" which is what is called with the grep command below.
#!/bin/bash
slack-export-viewer -z "file.zip"
sleep 10
ps aux | grep runslack.sh | awk '{print $2}' | xargs kill
Please try this and let me know if this helps you
ps -ef | grep runslack.sh | grep -v grep | awk '{print $2}' |xargs -i kill -9 {}
and i hope your user has sufficient permission to execute/kill this action on your server environment.

.bashrc saves previous process id and does not update in alias commands

I have made an alias in .bashrc to kill my python service.py & process
alias servicestop="kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}')"
Whenever I run first time servicestop command it will kill the process.
but again whenever I start process python service.py &, and execute command servicestop it gives an error.
After research, I found following things.
when I run first time python service.py & process. its process id was 512.
and, command servicestop kill that process(512).
Now when I run Second time process python service.py &. its process id was 546.(definitely it will be different).
When I run command servicestop. it will give following error:
-bash: kill: (512) - No such process
That means $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}') will return the previous pid, which is already killed.
Now please suggest the solution if any possible.
so whenever I want to run servicestop command, I have to run source .bashrc command first, then run servicestop command to make it work.
Please remove the servicestop alias from your .bashrc and add :
servicestop(){
kill $(ps -ef | grep -w service.py | grep -v grep | awk '{print $2}');
}
In a way, functions in .bashrc are "aliases 2.0" : simply better
Better : same function; but with the name of script to kill as parameter :
servicestop(){
kill $(ps -ef | grep -w $1 | grep -v servicestop | awk '{print $2}');
}
Use it like that :
servicestop service.py
servicestop otherSuperService.py

how can I kill a process in a shell script

My bash script has:
ps aux | grep foo.jar | grep -v grep | awk '{print $2}' | xargs kill
However, I get the following when running:
usage: kill [ -s signal | -p ] [ -a ] pid ...
kill -l [ signal ]
Any ideas, how to fix this line?
In general, your command is correct. If a foo.jar process is running, its PID will be passed to kill and (should) terminate.
Since you're getting kill's usage as output, it means you're actually calling kill with no arguments (try just running kill on its own, you'll see the same message). That means that there's no output in the pipeline actually reaching xargs, which in turn means foo.jar is not running.
Try running ps aux | grep foo.jar | grep -v grep and see if you're actually seeing results.
As much as you may enjoy a half dozen pipes in your commands, you may want to look at the pkill command!
DESCRIPTION
The pkill command searches the process table on the running system and signals all processes that match the criteria
given on the command line.
i.e.
pkill foo.jar
Untested and a guess at best (be careful)
kill -9 $(ps -aux | grep foo.jar | grep -v grep | awk '{print $2}')
I re-iterate UNTESTED as I'm not at work and have no access to putty or Unix.
My theory is to send the kill -9 command and get the process id from a sub shell command call.

puppet exec command issue

Below is my puppet code ,It is killing the process in agent but showing the error code
exec { "restart_process":
command => "ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print $2}' | xargs kill -9",
path => ['/usr/bin', '/sbin', '/bin', '/usr/sbin'],
subscribe => File["/root/Backup/deploy"],
refreshonly => true,
}
Error code:
Info: Computing checksum on file /root/Backup/deploy/nrpe.sh
Info: /Stage[main]/Myfile/File[/root/Backup/deploy/nrpe.sh]: Filebucketed /root/Backup/deploy/nrpe.sh to puppet with sum 136c6013774f5dfb84943718a70b36e0
Notice: /Stage[main]/Myfile/File[/root/Backup/deploy/nrpe.sh]/content: content
changed '{md5}136c6013774f5dfb84943718a70b36e0' to '{md5}c4fc9099405a108923ad18f7e2db13c8'
Info: /root/Backup/deploy: Scheduling refresh of Exec[restart_process]
Error: /Stage[main]/Myfile/Exec[restart_process]: Failed to call refresh: ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print }' | xargs kill -9 returned instead of one of [0]
Error: /Stage[main]/Myfile/Exec[restart_process]: ps -ef | grep nrpe | grep -v pts/1 | /bin/awk '{print }' | xargs kill -9 returned instead of one of [0]
Notice: Finished catalog run in 1.56 seconds
Can any body help me
#maxd is right that you need to escape the $ in the command when you specify it to Puppet. This is because Puppet performs its own variable interpolation on quotation-mark-quoted strings, and this will replace the $2 with the relevant replacement string -- probably an empty string -- before your command even reaches the catalog, and long before any attempt is made to run it.
I don't think that's your fundamental problem, however. Your command is a shell pipeline, but the default Exec provider for your system is almost certainly posix, and that provider will not do what you want with the command string you provided. You probably want to add
provider => 'shell'
to your resource, to have Puppet use the shell to execute it instead of trying to execute it directly as a (single) command.

Resources