Docker kill process inside container - bash

I exec into Docker container with docker exec -it container-name bash
Inside container I run command ps aux | grep processName
I receive a PID and after that I run:
kill processId but receive:
-bash: kill: (21456) - No such process
Am I missing something or? I know that Docker shows different process IDs from top command inside the host and ps aux inside the container (How to kill process inside container? Docker top command), but I am running this from inside container?

That response is because the process you are trying to kill is not existing at the moment of killing it. For example, if you launch ps aux you can get an output like this inside a container (it depends of the container of course):
oot#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 15 0.0 0.0 36840 2904 pts/0 R+ 13:57 0:00 ps aux
Then if you try to kill process with PID 15 you'll get the error because PID 15 is finished at the moment of trying to kill it. The ps command terminates after showing you the processes info. So:
root#69fbbc0ff80d:/# kill 15
bash: kill: (15) - No such process
In a docker container you can kill process in the same way as normal excepting the root process (id 1). You can't kill it:
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 16 0.0 0.0 36840 2952 pts/0 R+ 13:59 0:00 ps aux
root#69fbbc0ff80d:/# kill 1
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 17 0.0 0.0 36840 2916 pts/0 R+ 13:59 0:00 ps aux
As you can see you can't kill it. Anyway if you want to proof that you can kill processes you can do:
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 18 0.0 0.0 36840 3064 pts/0 R+ 14:01 0:00 ps aux
root#69fbbc0ff80d:/# sleep 1000 &
[1] 19
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 19 0.0 0.0 4372 724 pts/0 S 14:01 0:00 sleep 1000
root 20 0.0 0.0 36840 3016 pts/0 R+ 14:01 0:00 ps aux
root#69fbbc0ff80d:/# kill 19
root#69fbbc0ff80d:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 18400 3424 pts/0 Ss 13:55 0:00 bash
root 21 0.0 0.0 36840 2824 pts/0 R+ 14:01 0:00 ps aux
[1]+ Terminated sleep 1000
Hope it helps.

Related

When ssh to remote host, how can I know the bash pid running on remote host?

When I ssh to a remote host, Is it possible that I get the remote bash pid in my local machine? or vice versa?
in local terminal, I simply do:
ssh user#remote.com
for example, I know the ssh pid in local machine:
ps faux
...
shunh 32683 0.1 0.0 121148 5116 pts/2 Ss 07:02 0:00 \_ -bash
shunh 33079 0.6 0.0 189360 8484 pts/2 S+ 07:03 0:00 \_ ssh -X remote_host
and in remote_host:
ps faux
...
root 214679 0.0 0.0 158820 5724 ? Ss 07:03 0:00 \_ sshd: shunh [priv]
shunh 214681 0.0 0.0 158820 2556 ? S 07:03 0:00 \_ sshd: shunh#pts/1
shunh 214682 0.0 0.0 121124 3608 pts/1 Ss 07:03 0:00 \_ -bash
I wanna know if I can read the pid number "214682" in my local machine?
ssh user#remote.com 'echo $$'
Refer to the Bash manual, 3.4.2 Special Parameters:
$
($$) Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.

Inaccessible tty still has some bash processes

This is a very strange situation. I'm on OS X 10.11.6
I have an old tty still hanging around (ttys001) but I don't know how to access it and why its still there. It simply does not have any window on the os x desktop. I'm on ttys000.
$ tty
/dev/ttys000
This means I'm currently on ttys000
$ w
22:01 up 15 days, 7:47, 3 users, load averages: 1.65 1.43 1.45
USER TTY FROM LOGIN# IDLE WHAT
Sidharth console - 30Jul16 15days -
Sidharth s000 - 13:48 - w
Sidharth s001 - Thu13 9:12 -bash
I can understand the login from console (it happens automatically) but where is this s001 (i.e. ttys001) coming from -- I can't switch to it -- I don't see any os x terminal windows corresponding to ttys001.
USER PID PPID PGID SESS JOBC STAT TT TIME COMMAND
root 30994 30725 30994 0 0 Ss s000 0:00.04 login -pf Sidharth
Sidharth 30995 30994 30995 0 1 S s000 0:00.33 -bash
root 32409 30995 32409 0 1 R+ s000 0:00.01 ps aj
root 26065 1 26065 0 0 Ss+ s001 0:00.04 login -pfl Sidharth /bin/bash -c exec -la bash /bin/bash
Sidharth 26066 26065 26065 0 0 S+ s001 0:00.28 -bash
Sidharth 29465 26066 26065 0 0 S+ s001 0:00.00 -bash
These are the various processes with associated ttys. Again, I can't understand the life of me what 26065, 26066 and 29465 (all associated with 26065) are doing/why are they there.
Some observations: the parent of 30944 is 30725 which is the Mac Terminal application (this makes sense). But equally interesting is that the parent of 26065 (corresponding to login -pfl Sidharth /bin/bash -c exec -la bash /bin/bash is launchd i.e. pid 1)
I've noticed stuff like this before: there is usually an old ttys but its not visible.
Nope I'don't have any additional tabs open in my os x terminal program that could cause this
My question is this: Why is my ttys001 inaccessible? How do I "get to" ttys001

How can I tell many unicorn workers are running on a heroku dyno right now?

I know that you can look in config/unicorn.rb (or the equivalent) and see what those settings are, but I'm wondering specifically how I can tell, right now, how many unicorn workers are running on a given dyno.
I tried to ps aux after running 'heroku run bash' but that didn't give me the actual processes the dyno was running.
If you run:
$ heroku run bash
$ unicorn -c config/unicorn.rb &
$ ps euf
you should get something similar to this:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
u16236 2 0.0 0.0 19444 2024 ? S 20:55 0:00 bash GOOGLE_ANALYTICS_ID=XXX HEROKU_POSTGRESQL_COPPER_URL=postgres://XXX:
u16236 3 19.4 0.3 288716 131568 ? Sl 20:55 0:04 \_ unicorn master -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 5 31.0 0.3 305844 129636 ? Sl 20:55 0:04 | \_ sidekiq 3.2.5 app [0 of 2 busy] GOOGLE_ANALYTICS_
u16236 7 0.0 0.3 288716 124724 ? Sl 20:55 0:00 | \_ unicorn worker[0] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 10 0.0 0.3 288716 124728 ? Sl 20:55 0:00 | \_ unicorn worker[1] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 13 0.0 0.3 288716 124728 ? Sl 20:55 0:00 | \_ unicorn worker[2] -c config/unicorn.rb -l0.0.0.0:8080 GOOGLE_ANALYTICS_ID=XXX
u16236 30 0.0 0.0 15328 1104 ? R+ 20:55 0:00 \_ ps euf GOOGLE_ANALYTICS_ID=XXX DEVISE_PEPPER=XXX
You can see that processes 7, 10, & 13 are my 3 Unicorn workers that are each consuming 30% of total memory.

Ps aux: shows the same script multiple times

I have a simple script that checks for files to download.
The problem is after some time I can see him multiple times running, started in different times, even though I started him only once:
ps aux | grep _db
root 2804 0.0 0.0 11288 1756 ? S 00:26 0:06 /bin/bash /script/downloader/downloader_db.sh
root 8606 0.0 0.0 11284 872 ? S 12:18 0:00 /bin/bash /script/downloader/downloader_db.sh
root 8649 0.0 0.0 11168 680 pts/0 S 12:18 0:00 /bin/bash /script/downloader/downloader_db.sh
root 11552 0.0 0.0 11272 860 ? S 11:25 0:00 /bin/bash /script/downloader/downloader_db.sh
root 11562 0.0 0.0 11152 672 pts/0 S 11:25 0:00 /bin/bash /script/downloader/downloader_db.sh
root 39150 0.0 0.0 11172 1644 pts/0 S 10:51 0:01 /bin/bash /script/downloader/downloader_db.sh
I started the script with nohup from rc.local:
nohup /script/downloader/downloader_db.sh &> /dev/null &
the script:
#!/bin/bash
while true; do
while IFS=$'\t' read -a line; do
...
sleep 2
done < <(mysql --batch -u${user} -p${password} ${database} -e "${query}" -h ${host})
sleep 10
done
Is anything in the while loop running in the background? It is conceivable that it has the same name as the parent process.
You can check whether some of the processes are subprocesses by running ps wafux and looking at the process tree.
If they are not "related" to each other, you have very likely simply run the script more than once, and the other processes are not finished yet.

change user owner of process on Mac/Linux?

I have a program that is running as root. This app calls another program (processA) to run. When processA is running, it is owned by root but I want owner of it to be the current user logged on. How to do it?
Well it's a little bit tricky... Depends if it's a daemon (service) or you run this command/app.
For the 2nd case you can use "su" command.
Here's a short example.
1. I create o simple script with following content (it will sleep in background for 100 seconds and will output the process list coresponding to this script):
#!/bin/bash
sleep 100 &
ps faux | grep test.sh
2. I run the "su" command like this (I'm currently logged in as "root" and I want to run this script as "sandbox" user):
su - sandbox -c ./test.sh
sandbox = the username that will run this command.
-c ./test.sh = means it will execute this command
3. Output (first column = the user that owns this process):
root#i6:/web-storage/sandbox# su - sandbox -c ./test.sh
sandbox 18149 0.0 0.0 31284 1196 pts/0 S+ 20:13 0:00 \_ su - sandbox -c ./test.sh
sandbox 18150 0.0 0.0 8944 1160 pts/0 S+ 20:13 0:00 \_ /bin/bash ./test.sh
sandbox 18155 0.0 0.0 3956 644 pts/0 S+ 20:13 0:00 \_ grep test.sh
root#i6:/web-storage/sandbox#
I hope it will help,
Stefan

Resources