How to find/kill a specific python program - linux

There are two different python programs running in this VM
one is a background job who monitors a folder and then 'does stuff' (with several workers)
10835 ? Sl 0:03 python main.py
10844 ? Sl 34:02 python main.py
10845 ? S 33:43 python main.py
the second one is started via script
20056 pts/1 S+ 0:00 /bin/bash ./exp.sh
20069 pts/1 S+ 0:00 /bin/bash ./exp.sh
20087 pts/1 S+ 0:10 python /home/path/second.py
i have tried numerous things to find a way to kill only the main program (i want to build a cron watchdog), but non succeded
first one i want to find only the hanging 'python main.py' process (accompanied by [defunct]), but i cant even find just this process alone.
the upper ones are from ps -ax (so they both are running currently)
pgrep 'python' returns all PIDs, also from second.py which i dont want: (not usefull, so is pkill therefore)
pgrep 'python'
10835
10844
10845
20087
pgrep 'python main.py' always returns empty, so does pgrep 'main.py;
the only thing which works
ps ax | grep 'python main.py'
but this one also returns its own PID, grepping 'ps' isn't a prefered solution afair. when main.py hangs, it shows "python main.py [defunct]". a
ps ax | grep 'python main.py [defunct]'
is useless as test as it always returns true. pgrep for anything more than 'python' also returns always false. i am a bit clueless.

This works for me. Found it on the pgrep bro pages.
Find the pids of processes with 'test.py' as an argument, like 'python test.py'
pgrep -f test.py
And I use it to check if a python process is running:
searcher="backend/webapi.py"
if pgrep -f "$searcher" > /dev/null
then
echo "$(date +%y-%m-%d-%H-%M-%S) $searcher is alive. Doing nothing."
else
echo "No $searcher. Kickstarting..."
pushd $HOME/there/;
./run_backend
popd;
echo "Pgrepping $searcher:"
pgrep "$searcher" # out to loggers
fi

In your daemon python script you should create PID file:
def writePidFile():
pid = str(os.getpid())
f = open('/tmp/my_pid', 'w')
f.write(pid)
f.close()
Now killing this process is simple:
kill `cat /tmp/my_pid`
Or you can just use grep and filter its own process:
ps ax | grep 'python main.py [defunct]' | grep -v grep

I think I've found a pretty safe way to do find the proper python process PID for a specific file only. The idea is to find all the python processes, then all "test.py" processes and intersect the results to find the correct PID.
I use the subprocess module in a python script (you can interpolate for .sh script):
import subprocess as subp
#Get a list of all processes with a certain name
def get_pid(name):
return list(map(int,subp.check_output(["pidof", "-c", name]).split()))
#Get a list of all pids for python3 processes
python_pids = get_pid('python3')
#Get a list of all pids for processes with "main.py" argument
main_py_pids = list(map(int,subp.check_output(["pgrep", "-f", "main.py"]).split()))
python_main_py_pid = set(python_pids).intersection(main_py_pids)
print(python_pids)
print(main_py_pids)
print(python_main_py_pid)
Result with 3 running python processes (including the one from this script) and one "sudo nano main.py" process:
Python pids: [3032, 2329, 1698]
main.py pids: [1698, 3031]
Interesection: {1698}

Related

Why processes run from a shell script has parent id 1

running the following command would spawn a python process with parent pid 1
echo "python3 -m http.server 2>&1 &" > a && chmod 777 a && ./a && ps -ef | grep "python3 -m http.server"
result
501 4622 1 0 4:45PM ttys000 0:00.00 python3 -m http.server
while running the following
python3 -m http.server 2>&1 &
ps -ef | grep "python3 -m http.server"
will have something different
501 4646 665 0 4:51PM ttys000 0:00.07 python3 -m http.server
Can anyone explain?
All processes must have a parent. If a parent spawns a child, then the parent exits, the child must still have parent. When this happens, its parent is set to the init process, which has an pid of 1.
As for why, the above 2 cases are different... The second has its parent set to the shell pid, and the shell has not yet exited.
As for the first, you are spawning it from a shell, the server is put into the background by the use of '&', the shell then exits. We then go through paragraph 1.

Getting Process ID Linux using pgrep

I have a bash script called run.sh which launches two python scripts meter_1.py and meter_2.py
#!/bin/sh
./meter_1.py &
./meter_2.py &
When the scripts are running and I search for the PID of the scripts using the command
ps -aux | grep python
The output is
openhab+ 9328 84.0 1.6 25320 16580 pts/0 R 22:23 0:04 python ./meter_1.py
openhab+ 9329 84.6 1.6 25320 16596 pts/0 R 22:23 0:04 python ./meter_2.py
Using the pgrep command I can get the the PID
>pgrep python
9328
9329
However I could have multiple python scripts running and I want to get the process ID name by the script it is running not based on if it is python or not.
For example:
>pgrep python" "./meter_1.py
9328
Is there a functionality for this in pgrep? The following seems to work however it would be nice to get just the process ID back.
>ps -aux | grep python" "./meter_1.py
openhab+ 9328 84.0 1.6 25320 16580 pts/0 R 22:23 0:04 python ./meter_1.py
In ba(sh) you can get the PID of the last started process with $!
so in your run.sh script you can simply use:
#!/bin/sh
./meter_1.py &
echo PID of process1: $!
./meter_2.py &
echo PID of process2: $!
If you have control of the process, like you're the one starting the process in a script, to expand on what have posted already.
#!/bin/sh
./meter_1.py & meter_1_py_pid=$!
./meter_2.py & meter_2_py_pid=$!
The pids are in the variables $meter__1_py_pid and $meter__2_py_pid
You can do whatever you want with the pids, check if it is running, kill it and so on.

How to find the commands executed by a /bin/bash process? (Linux)

TL;DR :
I want to get the command running (if running) in the /bin/bash processes.
I want a script that can identify in the /bin/bash process the command /bin/bash is running. Tried to find it in /proc/[pid]/cmdline but it only show /bin/bash.
Is there a way to do this or what I'm wondeing is impossible. :o
I'm asking because when I run a ps -ef, some processes (like ssh) show how they'r running.
user 30410 30409 0 10:58 pts/0 00:00:00 ssh name#127.0.0.1 <-- here
There is the ssh command fully printed.
We can see the same if I do the command ps -ef | grep "/bin/bash", it return :
user 20080 4999 0 13:40 pts/9 00:00:00 grep /bin/bash <-- here
There is the command grep /bin/bash printed.
But if I run a bash loop like while true; do echo "hello"; done
And then I do ps -ef | grep "while" It return nothing !!!
that depends on what type of command are you looking for.
for external commands running from a shell, "ps -efH" shows you a hierarchical list of running processes, which you can then find the info you need.
bash built-in commands doesn't show up on ps list, you will have to enable script debugging using "set -x" and then monitor the stderr to see what the script is doing.
To answer the edits you made:
while is a built-in, so it doesn't show up. but the "echo" will show up in the "ps -efH" output i mentioned above.

How to get the process ID to kill a nohup process?

I'm running a nohup process on the server. When I try to kill it my putty console closes instead.
this is how I try to find the process ID:
ps -ef |grep nohup
this is the command to kill
kill -9 1787 787
When using nohup and you put the task in the background, the background operator (&) will give you the PID at the command prompt. If your plan is to manually manage the process, you can save that PID and use it later to kill the process if needed, via kill PID or kill -9 PID (if you need to force kill). Alternatively, you can find the PID later on by ps -ef | grep "command name" and locate the PID from there. Note that nohup keyword/command itself does not appear in the ps output for the command in question.
If you use a script, you could do something like this in the script:
nohup my_command > my.log 2>&1 &
echo $! > save_pid.txt
This will run my_command saving all output into my.log (in a script, $! represents the PID of the last process executed). The 2 is the file descriptor for standard error (stderr) and 2>&1 tells the shell to route standard error output to the standard output (file descriptor 1). It requires &1 so that the shell knows it's a file descriptor in that context instead of just a file named 1. The 2>&1 is needed to capture any error messages that normally are written to standard error into our my.log file (which is coming from standard output). See I/O Redirection for more details on handling I/O redirection with the shell.
If the command sends output on a regular basis, you can check the output occasionally with tail my.log, or if you want to follow it "live" you can use tail -f my.log. Finally, if you need to kill the process, you can do it via:
kill -9 `cat save_pid.txt`
rm save_pid.txt
I am using red hat linux on a VPS server (and via SSH - putty), for me the following worked:
First, you list all the running processes:
ps -ef
Then in the first column you find your user name; I found it the following three times:
One was the SSH connection
The second was an FTP connection
The last one was the nohup process
Then in the second column you can find the PID of the nohup process and you only type:
kill PID
(replacing the PID with the nohup process's PID of course)
And that is it!
I hope this answer will be useful for someone I'm also very new to bash and SSH, but found 95% of the knowledge I need here :)
suppose i am running ruby script in the background with below command
nohup ruby script.rb &
then i can get the pid of above background process by specifying command name. In my case command is ruby.
ps -ef | grep ruby
output
ubuntu 25938 25742 0 05:16 pts/0 00:00:00 ruby test.rb
Now you can easily kill the process by using kill command
kill 25938
jobs -l should give you the pid for the list of nohup processes.
kill (-9) them gently.
;)
You could try
kill -9 `pgrep [command name]`
Suppose you are executing a java program with nohup you can get java process id by
`ps aux | grep java`
output
xxxxx 9643 0.0 0.0 14232 968 pts/2
then you can kill the process by typing
sudo kill 9643
or lets say that you need to kill all the java processes then just use
sudo killall java
this command kills all the java processes. you can use this with process. just give the process name at the end of the command
sudo killall {processName}
If your application always uses the same port, you can kill all the processes in that port like this.
kill -9 $(lsof -t -i:8080)
This works in Ubuntu
Type this to find out the PID
ps aux | grep java
All the running process regarding to java will be shown
In my case is
johnjoe 3315 9.1 4.0 1465240 335728 ? Sl 09:42 3:19 java -jar batch.jar
Now kill it kill -9 3315
The zombie process finally stopped.
when you create a job in nohup it will tell you the process ID !
nohup sh test.sh &
the output will show you the process ID like
25013
you can kill it then :
kill 25013
I started django server with the following command.
nohup manage.py runserver <localhost:port>
This works on CentOS:
:~ ns$netstat -ntlp
:~ ns$kill -9 PID
This works for mi fine on mac
kill -9 `ps -ef | awk '/nohup/{ print \$2 }'`
I often do this way. Try this way :
ps aux | grep script_Name
Here, script_Name could be any script/file run by nohup.
This command gets you a process ID. Then use this command below to kill the script running on nohup.
kill -9 1787 787
Here, 1787 and 787 are Process ID as mentioned in the question as an example.
This should do what was intended in the question.
If you are unaware of the PID, then first find it using TOP command
top -U userid
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
You will get the PID using top, then perform the kill operation.
$ kill -9 <PID>
Today I met the same problem. And since it was a long time ago, I totally forgot which command I used and when. I tried three methods:
Using the STIME shown in ps -ef command. This shows the time you start your process, and it's very likely that you nohup you command just before you close ssh(depends on you) . Unfortunately I don't think the latest command is the command I run using nohup, so this doesn't work for me.
Second is the PPID, also shown in ps -ef command. It means Parent Process ID, the ID of process that creates the process. The ppid is 1 in ubuntu for process that using nohup to run. Then you can use ps --ppid "1" to get the list, and check TIME(the total CPU time your process use) or CMD to find the process's PID.
Use lsof -i:port if the process occupy some ports, and you will get the command. Then just like the answer above, use ps -ef | grep command and you will get the PID.
Once you find the PID of the process, then can use kill pid to terminal the process.
About losing your putty: often the ps ... | awk/grep/perl/... process gets matched, too! So the old school trick is like this
ps -ef | grep -i [n]ohup
That way the regex search doesn't match the regex search process!
if you are on a remote server, check memory usage with top , and find your process and its ID. After that, just execute kill [your process ID] .

How to get a list of programs running with nohup

I am accessing a server running CentOS (linux distribution) with an SSH connection.
Since I can't always stay logged in, I use "nohup [command] &" to run my programs.
I couldn't find how to get a list of all the programs I started using nohup.
"jobs" only works out before I log out. After that, if I log back again, the jobs command shows me nothing, but I can see in my log files that my programs are still running.
Is there a way to get a list of all the programs that I started using "nohup" ?
When I started with $ nohup storm dev-zookeper ,
METHOD1 : using jobs,
prayagupd#prayagupd:/home/vmfest# jobs -l
[1]+ 11129 Running nohup ~/bin/storm/bin/storm dev-zookeeper &
NOTE: jobs shows nohup processes only on the same terminal session where nohup was started. If you close the terminal session or try on new session it won't show the nohup processes. Prefer METHOD2
METHOD2 : using ps command.
$ ps xw
PID TTY STAT TIME COMMAND
1031 tty1 Ss+ 0:00 /sbin/getty -8 38400 tty1
10582 ? S 0:01 [kworker/0:0]
10826 ? Sl 0:18 java -server -Dstorm.options= -Dstorm.home=/root/bin/storm -Djava.library.path=/usr/local/lib:/opt/local/lib:/usr/lib -Dsto
10853 ? Ss 0:00 sshd: vmfest [priv]
TTY column with ? => nohup running programs.
Description
TTY column = the terminal associated with the process
STAT column = state of a process
S = interruptible sleep (waiting for an event to complete)
l = is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
Reference
$ man ps # then search /PROCESS STATE CODES
Instead of nohup, you should use screen. It achieves the same result - your commands are running "detached". However, you can resume screen sessions and get back into their "hidden" terminal and see recent progress inside that terminal.
screen has a lot of options. Most often I use these:
To start first screen session or to take over of most recent detached one:
screen -Rd
To detach from current session: Ctrl+ACtrl+D
You can also start multiple screens - read the docs.
If you have standart output redirect to "nohup.out" just see who use this file
lsof | grep nohup.out
You cannot exactly get a list of commands started with nohup but you can see them along with your other processes by using the command ps x. Commands started with nohup will have a question mark in the TTY column.
You can also just use the top command and your user ID will indicate the jobs running and the their times.
$ top
(this will show all running jobs)
$ top -U [user ID]
(This will show jobs that are specific for the user ID)
sudo lsof | grep nohup.out | awk '{print $2}' | sort -u | while read i; do ps -o args= $i; done
returns all processes that use the nohup.out file

Resources