Anyway to get the ID of processes created by Supervisord? - pid

I need the process ID of processes created using supervisord for use in a script. Processes spawned by supervisord don't create .pid files in their default directories, if at all.
How do I get the process ID of a supervisord child process?

As of supervisor version 3 you can use the supervisorctl pid <name> command to list pids of managed processes:
supervisorctl pid programname
Use supervisorctl pid all to get a newline-separated list of pids of all managed processes.
For older supervisord versions, you are stuck with supervisord status, but with a little awk, sed and paste massaging, you can extract those pids to be acceptable as input to other commands:
echo `bin/supervisorctl status | grep RUNNING | awk -F' ' '{print $4}' | sed -e 's/,$//' | paste -sd' '`
would list all pids of running programs as a space-separated list. Replace echo with a kill -HUP command to send them all the SIGHUP signal, for example.

You can now do the following:
sudo supervisorctl pid all
sudo supervisorctl pid myprogramname

System centos7
command:
ps -ef|grep $(cat /tmp/supervisord.pid)|grep -v grep |grep -v supervisord|awk '{print $2}'
The file /tmp/supervisord.pid records the supervisord id.
You can get child process by ps -ef|grep ${fatherProcess}

Related

How to kill hidden process?

I have the following script.
#!/bin/bash
if [ "$EUID" -ne 0 ]
then
echo ''
echo -e "\e[1;31m Please run the script as root \e[0m"
echo ''
exit
fi
for run in {1..11}
do
echo -e '\e[1;32m Initializing AP in backfround... \e[0m'
sudo screen -dmS hotspot
sleep 5
# start the AP in background
echo -e '\e[1;32m Starting AP in backfround... \e[0m'
sudo screen -S hotspot -X exec ./start_hostapd.sh
sleep 20
# save PIDs for dmS
ps -ef | grep "dmS" | awk '{print $2}' > dms.log
sleep 1
# save PIDs for hostapd
ps -ef | grep "hostapd" | awk '{print $2}' > process.log
sleep 1
echo -e '\e[1;33m Running data... \e[0m'
for run in {1..10}
do # send 10 times
sudo /home/ubuntu/Desktop/send_data/run_data
sleep 1
done
echo -e "\e[1;31m Stopping sending... \e[0m"
sleep 2
echo -e "\e[1;31m Quiting hotspot... \e[0m"
sudo /home/ubuntu/Desktop/kill_dms/kill_dms
sleep 5
echo -e "\e[1;31m Stopping AP... \e[0m"
sudo /home/ubuntu/Desktop/kill_hostapd/kill_hostapd
sleep 5
echo -e '\e[1;31m Wiping dead screens... \e[0m'
echo
sudo screen -wipe
sudo screen -X -S hotspot quit
sleep 5
done
I use a bash script that starts the AP (hostapd) and then it executes some another commands. Unfortunately, once the AP is started, the next lines will not be executed anymore. To avoid this problem, in the Script I start the AP using screen command that allows to run AP in background and also it allows to execute next lines.
For each iteration in the for-loop, the AP must be restarted. For this purpose I write out the PIDs of screen and hostapd and then I call my C programs, which kill these processes. At last I use screen commands again to ensure that the AP in the background has been stopped and it can be started again.
This implementation works good. However, when the script comes to the end and all processes has been already killed, the AP disappears in other devices and after some minutes it appears again and it happens several times. Only the system reboot helps to stop the AP completely.
I use htop to find out the processes which runs AP. However, I can not find the processes. The htop says that there is no processes, which I created using script from above. This is right, because the script kills the processes once it is finished.
So, I suppose that there are hidden processes for my AP and I do not see them. Is there a way to find that hidden processes and kill them to stop the AP?
When I just start the AP in another terminal and then I stop it just using CTRL+C, the AP will be stopped and my devices do not see it anymore.
That's why I suppose that the screen starts a hidden process, which can not be found by htop or by other programs like htop.
If you don't need any hostap process at all, I'd rather use pkill instead of trusting the management of pids. Easiest usage should look like:
pkill -f hostap
pkill -f screen
If you'd want to use another signal like 9, use:
pkill -9 -f hostap
pkill -9 -f screen
https://linux.die.net/man/1/pkill

Find tmux session that a PID belongs to

I am using htop so see what processes are taking up a lot of memory so I can kill them. I have a lot of tmux sessions and lots of similar processes. How can I check which tmux pane a PID is in so I can be sure I am killing stuff I want to kill?
Given that PID in the below line is the target pid number:
$ tmux list-panes -a -F "#{pane_pid} #{pane_id}" | grep ^PID
The above will identify the pane where the PID is running. The output will be two strings. The first number should be the same as PID and the second one (with a percent sign) is "tmux pane id". Example output:
2345 %30
Now, you can use "tmux pane id" to kill the pane without "manually" searching for it:
$ tmux kill-pane -t %30
To answer your question completely, in order to find *tmux session* that a PID belongs to, this command can be used:
$ tmux list-panes -a -F "#{pane_pid} #{session_name}" | grep ^PID
# example output: 2345 development
Here's another possibly useful "line":
$ tmux list-panes -a -F "#{pane_pid} #{session_name}:#{window_index}:#{pane_index}" | grep ^PID
# example output: 2345 development:2:0
The descriptions for all of the interpolation strings (example #{pane_pid}) can be looked up in tmux man page in the FORMATS section.
The answers above give you the pids of the shells running in the panes, you'll be out of luck if you want to find something running in the shells.
try:
https://gist.github.com/nkh/0dfa8bf165a53832a4b5b17ee0d7ab12
This scrip gives you all the pids as well as the files the processes have opened. I never know in which session, window, pane, attached or not, I have a file open, this helps.
I haven't tried it on another machine, tell me if you encounter any problem.
lsof needs to be installed.
if you just want pids, pstree is useful, you can modity the script to use it (it's already there commented)
The following script displays the tree of processes in each window (or pane). It takes list of PIDs as one parameter (one PID per line). Specified processes are underlined. It automatically pipes to less unless is a part of some other pipe. Example:
$ ./tmux-processes.sh "$(pgrep ruby)"
-- session-name-1 window-index-1 window-name-1
7184 7170 bash bash --rcfile /dev/fd/63 -i
7204 7184 vim vim ...
-- session-name-2 window-index-2 window-name-2
7186 7170 bash bash --rcfile /dev/fd/63 -i
10771 7186 bash bash ./manage.sh runserver
10775 10771 django-admi /srv/www/s1/env/bin/python /srv/www/s1/env/bin/...
5761 10775 python /srv/www/s1/env/bin/python /srv/www/s1/env/bin/...
...
tmux-processes.sh:
#!/usr/bin/env bash
set -eu
pids=$1
my_pid=$$
subtree_pids() {
local pid=$1 level=${2:-0}
if [ "$pid" = "$my_pid" ]; then
return
fi
echo "$pid"
ps --ppid "$pid" -o pid= | while read -r pid; do
subtree_pids "$pid" $((level + 1))
done
}
# server_pid=$(tmux display-message -p '#{pid}')
underline=$(tput smul)
# reset=$(tput sgr0) # produces extra symbols in less (^O), TERM=screen-256color (under tmux)
reset=$(echo -e '\033[m')
re=$(echo "$pids" | paste -sd'|')
tmux list-panes -aF '#{session_name} #{window_index} #{window_name} #{pane_pid}' \
| while read -r session_name window_index window_name pane_pid; do
echo "-- $session_name $window_index $window_name"
ps -p "$(subtree_pids "$pane_pid" | paste -sd,)" -Ho pid=,ppid=,comm=,args= \
| sed -E 's/^/ /' \
| awk \
-v re="$re" -v underline="$underline" -v reset="$reset" '
$1 ~ re {print underline $0 reset}
$1 !~ re {print $0}
'
done | {
[ -t 1 ] && less -S || cat
}
Details regarding listing tmux processes you can find here.
To underline lines I use ANSI escape sequences. To show the idea separately, here's a script that displays list of processes and underlines some of them (having PIDs passed as an argument):
#!/usr/bin/env bash
set -eu
pids=$1
bold=$(tput bold)
# reset=$(tput sgr0) # produces extra symbols in less (^O), TERM=xterm-256color
reset=$(echo -e '\033[m')
underline=$(tput smul)
re=$(echo "$pids" | paste -sd'|')
ps -eHo pid,ppid,comm,args | awk \
-v re="$re" -v bold="$bold" -v reset="$reset" -v underline="$underline" '
$1 ~ re {print underline $0 reset}
$1 !~ re {print $0}
'
Usage:
$ ./ps.sh "$(pgrep ruby)"
Details regarding less and $(tput sgr0) can be found here.

How do get the process name & process id pid of newly created child process using fork?

I am using fork to create the child process. Now I want to know the name and process id of the child process using putty. Which command I need to use to get this information. I am trying with ps and pstree. how can give the name of the child process while creating new child process? Is it possible to get this information using any linux/unix command?
I want to know how much time child is active and when it is terminated. mean timing information of child process.
root#mx6q:~# ps aux|grep "childprogram"
ps: invalid option -- 'a'
BusyBox v1.20.2 (2014-03-13 11:47:37 CET) multi-call binary.
Usage: ps
Show list of processes
w Wide output
l Long output
T Show threads
root#mx6q:~#
root#mx6q:~# ps | grep "childprogram"
1407 root 1908 S grep childprogram
root#mx6q:~# ps | grep "childprogram"
1409 root 1908 S grep childprogram
root#mx6q:~# ps | grep "childprogram"
1411 root 1908 S grep childprogram
For Parent:
root#mx6q:~# readlink /proc/670/exe
.asoundrc .gvfs/
.bashrc adit-30-09-2014.vnclicense
.gstreamer-0.10/ enable_usb_dr_host_mode.sh
root#mx6q:~# readlink /proc/670/exe
but I am not able to find child pid inside /proc/? What does it mean?
You tagged this as C and mentioned that you are the actor forking the new process so you have all this information available to you in the parent process that forks the child but you need to alter your code to capture it.
You have the child's pid because it is returned in the parent by fork.
You (probably) have the child's name because under most circumstances you are the one who wrote the exec call. If not, with the child's pid you can readlink /proc/<pid>/exe.
If you need to know the child's stats while it is running you can call getrusage with the RUSAGE_CHILDREN option.
If you just want the child's stat's after it is completed you can wait on it with wait4
Try this:
$ ps xf
And analyze the output and make some filters with grep sed and/or awk.
I am not very much familiar with BusyBox as I know it is a tiny distro with limited functions.

Root PID of Docker container

How can I find the external PID of the root PID inside a Docker container - that is, the one that has PID 1 inside the container? docker ps doesn't seem to display that information.
One possible way is:
docker inspect -f '{{ .State.Pid }}' $CONTAINER_ID
Please try:
docker inspect -f '{{.State.Pid}}' $(docker ps -q)
Here is a POSIX shell function that captures the PID for a given container.
pid_for_container() {
ps -C lxc-start -o pid= -o args= | fgrep -- " -n $1" | cut -d' ' -f1
}
It is a minimal (I hope) pipeline for this purpose, using the cheapest possible (fgrep instead of grep, cut instead of awk) commands.

issue running a batch script to kill a process

I am using the following script on a command line to kill a hypothetical notepad process (using a KornShell (ksh) in Windows XP, if that matters):
kill $(tasklist | grep -i notepad.exe | awk '{print 2}')
Now I take this line, and put it into a batch file c:\temp\testkill.bat, thinking that I should just as well be able to kill the process by running the batch file. However, when I run the batch file, I get the following awk error about unbalanced parentheses:
C:/Temp> ./testkill.bat
C:\Temp>kill $(tasklist | grep -i notepad.exe | awk '{print $2}')
awk: unbalanced () Context is:
>>> {print $2}) <<<
C:/Temp>
So I am baffled as to why I am getting this error about unbalanced parentheses when I run this script via a batch file, but have no issues when I run the command directly from the command line?
(I am not necessarily tied to this way of killing a process - I am additionally wondering why if I write the following on the command line:
tasklist | grep -i notepad.exe | awk '{print $2}' | kill
The process ID that comes out of the tasklist/grep/awk calls does not seem to properly get piped to kill.
Why are you making a batch file if you have a Korn shell? Write a shell script - that will probably help you out a lot.
I can answer your final question - kill doesn't take the PID to kill from the standard input, it takes it on the command line. You can use xargs to make it work:
tasklist | grep -i notepad.exe | awk '{print $2}' | xargs kill

Resources