How to restart specific running python file Ubuntu

Question:

I have running python file "cepusender/main.py" (and another python files). How can I restart/kill only main.py file?

Answers:

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.

Answered By: dsenese

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.
Answered By: thisisrandy
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.