when i use ps -ef |grep i get the current running programs
if below shown are the currently running programs.How can i stop a program using the name of the program
user 8587 8577 30 12:06 pts/9 00:03:07 python3 program1.py
user 8588 8579 30 12:06 pts/9 00:03:08 python3 program2.py
eg. If i want to stop program1.py then how can i stop the process using the program name "program1.py"
.If any suggestions on killing the program with python will be great
By using psutil is fairly easy
import psutil
proc = [p for p in psutil.process_iter() if 'program.py' in p.cmdline()]
proc[0].kill()
To find out the process from the process name filter through the process list with psutil like in Cross-platform way to get PIDs by process name in python
Try doing this with the process name:
pkill -f "Process name"
For eg. If you want to kill the process "program1.py", type in:
pkill -f "program1.py"
Let me know if it helps!
Assuming you have pkill utility installed, you can just use:
pkill program1.py
If you don't, using more common Linux commands:
kill $(ps -ef | grep program1.py | awk '{print $2}')
If you insist on using Python for that, see How to terminate process from Python using pid?
grep the program and combine add pipe send the output in another command.
1. see program ps -ef.
2.search program grep program.
3. remove the grep that you search because is appear in the search process grep -v grep.
4.separate the process to kill with awk awk '{ print $2 }'
5. apply cmd on the previous input xarks kill -9
ps -ef | grep progam | grep -v grep | awk '{ print $2 }' | xargs kill -9
see here for more:
about pipe , awk, xargs
with python you can use os:
template = "ps -ef | grep {program} | grep -v grep | awk '{{ print $2 }}' | xargs kill -9"
import os
os.system(template.format(program="work.py"))
Related
I have running python file "cepusender/main.py" (and another python files). How can I restart/kill only main.py file?
Here's a way (there are many):
ps -ef | grep 'cepusender/main.py' | grep -v grep | awk '{print $2}' | xargs kill
ps is the process snapshot command. -e prints every process on the system, and -f prints the full-format listing, which, germanely, includes the command line arguments of each process.
grep prints lines matching a pattern. We first grep for your file, which will match both the python process and the grep process. We then grep -v (invert match) for grep, paring output down to just the python process.
Output now looks like the following:
user 77864 68024 0 13:53 pts/4 00:00:00 python file.py
Next, we use awk to pull out just the second column of the output, which is the process ID or PID.
Finally we use xargs to pass the PID to kill, which asks the python process to shutdown gracefully.
kill is the command to send signals to processes.
You can use kill -9 PID to kill your python process, where 9 is the number for SIGKILL and PID is the python Process ID.
I'm trying to kill a specific process with a command which works well in the shell but not from python subprocess
import subprocess
subprocess.call(["kill", "$(ps | grep process | awk '{ print $1}' | head -n1)"], shell=False)
A work around would be to put this command into a shell script and run the shell script.
Is it possible from python subprocess directly ?
import subprocess
subprocess.Popen("kill $(ps | grep ncat | awk '{print $1}' | head -n1)", shell=True)
In your example, you don't create a subprocess from bash, but at the same time you use $(...) which is a bash instruction. To be more precise, you create a kill process and pass it argument $(...) which is not precomputed.
The above example creates a bash process, and then tells it to interpret kill $(...). Bash converts $(...) to a value, and then it runs kill VALUE.
I'm a brand new noob in python universe, so don't judge me too fast :-)
I'm trying to force a python script to reload or restart at the beggining of a bash script.
I've tried :
pkill -f myscript.py
and
killall myscript.py
and others...
Actually, I would like to make run the same script that call .wav files after having changed those .wav files... If I don't reload the script or restart it, it keeps playing the old files.
Maybe, there is other solutions.
Here is the script I want to reload (it's a button script playing music for my daughter)
#!/usr/bin/env python3
import pygame
from gpiozero import LED, Button
from signal import pause
pygame.init()
button_sounds = {Button(2): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/1.wav"),
Button(3): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/2.wav"),
Button(4): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/3.wav"),
Button(17): pygame.mixer.Sound("/home/pi/gpio-music-box/samples/4.wav")}
for button, sound in button_sounds.items():
button.when_pressed = sound.play
pause()
And here is my bash script :
#!/bin/bash
***HERE THE COMMAND I NEED !***
rm -r /home/pi/gpio-music-box/samples/*
cp -r /home/pi/gpio-music-box/comptines/* /home/pi/gpio-music-box/samples/
/home/pi/gpio-music-box/music.py
Thank you very much, and scuze my english, I'm french :-)
Andy
try this
#!/bin/bash
pid=$(ps auxwww | grep nameOfScript.py | grep -v grep | awk '{print $2}')
kill -9 $pid
rm -r /home/pi/gpio-music-box/samples/*
cp -r /home/pi/gpio-music-box/comptines/* /home/pi/gpio-music-box/samples/
nohup /home/pi/gpio-music-box/music.py &
Have a nice day
Firstly, you can reduce a lot of the "noise" from ps by using output formatting. You can then stop the need for using both grep and awk by using awk to do the searching also.
ps -eo "%p %a" | awk '/nameOfScript.py/ && $1 != PROCINFO["pid"] { print "kill -9 "$1 }'
This forces ps to only print the pid (%p) and the full command (%a). The output is then piped to awk where is searches for lines with the name of the script contained. It discounts any entries with the current process id of awk and then uses this to print a kill command with the relevant process id.
Once you have verified that the kill command displays as expected, you can use awk's system function to actually run the command through:
ps -eo "%p %a" | awk '/prometheous-things.py/ && $1 != PROCINFO["pid"] { system("kill -9 "$1) }'
I have a process running in the background, a python one, with ps -ef I can see filename from running command : UID PID PPID ... python ./filename.py
How can I know where the file is located
pwdx < PID > gives full directory the process is running from.
So, the full script would be
ps -ef | grep 'your process' | awk '{print $2}' | xargs pwdx
Though, you can simplify this into
pgrep 'your process' | awk '{print $1}' | xargs pwdx
I closed iPython explicitly when a script was running. The script didn't stop and is still running in the background, creating some output files. How can I stop the script?
You can kill the ipython process by this command
ps aux | grep ipython | grep -v "grep ipython" | awk '{print $2}' | xargs kill -9