I am trying to kill a nohup process in an EC2 instance but so far have been unsuccessful. I am trying to grab the process ID (PID) and then use it with the kill command in terminal, like so:
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
with columns, (I believe) they're:
UID PID PPID C STIME TTY TIME CMD
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
However, each time I try to kill the process, I get an error saying that the PID doesn't exist, seemingly because the PID changed. Here is a sequence I am running into in my command line:
// first try, grab the PID and kill
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16580
-bash: kill: (16580) - No such process
// ?? - check for correct PID again, and try to kill again
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16583 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-172-31-41-213 ~]$ kill 16583
-bash: kill: (16583) - No such process
// try 3rd time, kill 1 PID up
[ec2-user#ip-myip ~]$ ps -ef |grep nohup
ec2-user 16584 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
[ec2-user#ip-myip ~]$ kill 16585
-bash: kill: (16585) - No such process
This is quite a struggle for me right now, since I need to kill/restart this nohup process. Any help is appreciated!
EDIT - I tried this approach to killing the process because it was posted as an answer in this thread (Prevent row names to be written to file when using write.csv) and was the 2nd highest rated answer.
Very very bad question ...
You are trying to kill you grep process...
ec2-user 16580 16153 0 19:50 pts/0 00:00:00 grep --color=auto nohup
The command is grep --color=auto nohup
I'm not sure you can kill nohup
nohup will run your command in a particular way. But after its launching, the nohup process dies.
If you want to grep the ps output :
ps -ef | grep '[n]ohup'
or
pgrep -fl nohup
because you are trying to kill not nohup pid but the grep itself...
I have running docker ubuntu container with just a bash script inside. I want to start my application inside that container with docker exec like that:
docker exec -it 0b3fc9dd35f2 ./main.sh
Inside main script I want to run another application with nohup as this is a long running application:
#!/bin/bash
nohup ./java.sh &
#with this strange sleep the script is working
#sleep 1
echo `date` finish main >> /status.log
The java.sh script is as follow (for simplicity it is a dummy script):
#!/bin/bash
sleep 10
echo `date` finish java >> /status.log
The problem is that java.sh is killed immediately after docker exec returns. The question is why?
The only solution I found out is to add some dummy sleep 1 into the first script after nohup is started. Than second process is running fine. Do you have any ideas why it is like that?
[EDIT]
Second solution is to add some echo or trap command to java.sh script just before sleep. Than it works fine. Unfortunately I cannot use this workaround as instead of this script I have java process.
This is not an answer, but I still don't have the required reputation to comment.
I don't know why the nohup doesn't work. But I did a workaround that worked, using your ideas:
docker exec -ti running_container bash -c 'nohup ./main.sh &> output & sleep 1'
Okay, let's join two answers above :D
First rcmgleite say exactly right: use
-d
options to run process as 'detached' background.
And second (the most important!) if you run detached process, you don't needed nohup!
deploy_app.sh
#!/bin/bash
cd /opt/git/app
git pull
python3 setup.py install
python3 -u webui.py >> nohup.out
Execute this inside a container
docker exec -itd container_name bash -c "/opt/scripts/deploy_app.sh"
Check it
$ docker attach container_name
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 11768 1940 pts/0 Ss Aug31 0:00 /bin/bash
root 887 0.4 0.0 11632 1396 pts/1 Ss+ 02:47 0:00 /bin/bash /opt/scripts/deploy_app
root 932 31.6 0.4 235288 32332 pts/1 Sl+ 02:47 0:00 python3 -u webui.py
I know this is a late response but I will add it here for documentation reasons.
When using nohup on bash and running it with 'exec' on a docker container, you should use
$ docker exec -d 0b3fc9dd35f2 /bin/bash -c "./main.sh"
The -d option means:
-d, --detach Detached mode: run command in the
background
for more information about docker exec, see:
https://docs.docker.com/engine/reference/commandline/exec/
This should do the trick.
Today when I tried to kill all the processes related to docker, I noticed something really funny:
➜ ~ ps aux | grep docker
Caesar 73944 0.0 0.0 2423372 220 s000 R+ 6:49PM 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn docker
➜ ~ kill 73944
kill: kill 73944 failed: no such process
➜ ~ ps aux | grep docker
Caesar 74064 0.0 0.0 2432788 572 s000 R+ 6:50PM 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn docker
➜ ~
I used ps aux | grep docker to find the process related to docker, although I am not sure if it is really a docker process. The funny thing is that: when I kill it using pid, I couldn't do it, as you can see from the screenshot. Also, I noticed that the pid changed at the second ps command.
I know the problem may look stupid, however, I couldn't find anything on Google because I didn't know how to describe it. Maybe you can help me with that. Thanks ; )
Update
Thanks for the comments under this question. I noticed that the pid belongs to the grep process, and no wonder why the pids differ each time. Thank y'all for the help!
You're seeing the grep process, not the docker process. Whenever you use grep to filter the output of ps you'll run into this issue. To avoid listing the grep process, the canonical solution is to put square brackets around the first character in the target process name:
ps aux | grep '[d]ocker'
Since the search string contains square brackets (which don't effectively alter the regular expression) it will no longer be a match for the regular expression itself when found in the name of the grep process.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 11 months ago.
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.
On MacOSX, I'm using Packer to build a Vagrant box so I need to continually bring it up and tear it down. I'm attempting to 'vagrant up', and receive the standard error because the port is in use:
"Vagrant cannot forward the specified ports on this VM, since they would collide with some other application that is already listening on these ports. The forwarded port to 8080 is already in use on the host machine."
The solution seems simple enough: I just need to identify the process that is holding port 8080 open and kill that process, right?. It's not that easy.
If I run the command:
nmap localhost -p 8080
I receive the following output:
PORT STATE SERVICE
8080/tcp open http-proxy
If I run the following command:
top -o prt
The highest port in use in 1360
If I run the following command:
netstat -tulpn | grep :8080
I receive:
netstat: n: unknown or uninstrumented protocol
If I run the following command:
lsof -i :8080
I receive no output
If I restart my computer, the port is now available and I can now 'vagrant up'.
How can I kill whatever process is using port 8080 so that I can vagrant up without restarting my computer?
This might help
lsof -n -i4TCP:8080
The PID is the second field in the output.
Or try:
lsof -i -P
Fast and quick solution:
lsof -n -i4TCP:8080
PID is the second field. Then, kill that process:
kill -9 PID
Less fast but permanent solution
Go to /usr/local/bin/ (Can use command+shift+g in finder)
Make a file named stop. Paste the below code in it:
#!/bin/bash
touch temp.text
lsof -n -i4TCP:$1 | awk '{print $2}' > temp.text
pidToStop=`(sed '2q;d' temp.text)`
> temp.text
if [[ -n $pidToStop ]]
then
kill -9 $pidToStop
echo "Congrates!! $1 is stopped."
else
echo "Sorry nothing running on above port"
fi
rm temp.text
Save this file.
Make the file executable chmod 755 stop
Now, go to terminal and write stop 8888 (or any port)
In case above-accepted answer did not work, try below solution.
You can use it for port 8080 or for any other ports.
sudo lsof -i tcp:3000
Replace 3000 with whichever port you want. Run below command to kill that process.
sudo kill -9 PID
PID is process ID you want to kill.
Below is the output of commands on mac Terminal.
Use the following command:
lsof -n -i4TCP:8080 | awk '{print$2}' | tail -1 | xargs kill -9
The process id of port 8080 will be picked and killed forcefully using kill -9.
I needed to kill processes on different ports so I created a bash script:
killPort() {
PID=$(echo $(lsof -n -i4TCP:$1) | awk 'NR==1{print $11}')
kill -9 $PID
}
Just add that to your .bashrc and run it like this:
killPort 8080
You can pass whatever port number you wish
To script this:
pid=$(lsof -ti tcp:8080)
if [[ $pid ]]; then
kill -9 $pid
fi
The -t argument makes the output of lsof "terse" which means that it only returns the PID.
sudo lsof -i:8080
By running the above command you can see what are all the jobs running.
kill -9 <PID Number>
Enter the PID (process identification number), so this will terminate/kill the instance.
I needed to run this command
sudo lsof -i :80 # checks port 8080
Then i got
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
acwebseca 312 root 36u IPv4 0x34ae935da20560c1 0t0 TCP 192.168.1.3:50585->104.25.53.12:http (ESTABLISHED)
show which service is using the PID
ps -ef 312
Then I got this
UID PID PPID C STIME TTY TIME CMD
0 312 58 0 9:32PM ?? 0:02.70 /opt/cisco/anyconnect/bin/acwebsecagent -console
To uninstall cisco web security agent run
sudo /opt/cisco/anyconnect/bin/websecurity_uninstall.sh
credits to: http://tobyaw.livejournal.com/315396.html
It can be Cisco AnyConnect.
Check if /Library/LaunchDaemons/com.cisco.anyconnect.vpnagentd.plist exists. Then unload it with launchctl and delete from /Library/LaunchDaemons
You can also use the Activity Monitor to identify and quit the process using the port.
Run: nmap -p 8080 localhost (Install nmap with MacPorts or Homebrew if you don't have it on your system yet)
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00034s latency).
Other addresses for localhost (not scanned): ::1
PORT STATE SERVICE
8080/tcp open http-proxy
Run: ps -ef | grep http-proxy
UID PID PPID C STIME TTY TIME CMD
640 99335 88310 0 12:26pm ttys002 0:00.01 grep http-proxy"
Run: ps -ef 640 (replace 501 with your UID)
/System/Library/PrivateFrameworks/PerformanceAnalysis.framework/Versions/A/XPCServices/com.apple.PerformanceAnalysis.animationperfd.xpc/Contents/MacOS/com.apple.PerformanceAnalysis.animationperfd
Port 8080 on mac osx is used by something installed with XCode SDK
try netstat
netstat -vanp tcp | grep 3000
if your netstat doesn't support -p , use lsof
sudo lsof -i tcp:3000
For Centos 7 use
netstat -vanp --tcp | grep 3000
After referring to the solution of #voutasaurus. I wrote this utility to simplify the process of killing all the processes that are running on the port.
killProcessesUsing3000 () {
pid=$(lsof -ti :3000) # The -t argument makes the output of lsof "terse" (Brief) which means that it only returns the PID.
# PID contains process processes that run on the 3000 port. In new lines if they are multiples
for num ($=pid) {
echo $num
kill -9 $num
}
}
#Alias
alias kill3000="killProcessesUsing3000"
For me this worked
Open your mac terminal
kill $(lsof -t -i:8080)
Explanation:
lsof -t returns the PID and passes that to kill.
I tried many of the above and they didn't work for me.
After many hours, I found this one liner:
# kill 8080
alias nuke88="lsof -i tcp:8080 | grep LISTEN | awk '{print \$2}' | xargs kill"
# kill 3000
alias nuke3k="lsof -i tcp:3000 | grep LISTEN | awk '{print \$2}' | xargs kill"
I'm working on automated deployment using Rake of a mono asp.net website to ubuntu server with nginx.
As far as I've discovered fastcgi-mono-server4.exe can't be stopped gracefully and must be terminated. I currently do that manually in htop. How do I do that from rake or the shell?
I've only a few hours of experience with rake & ruby and a few weeks of linux but made a lot of progress already, however somethings are eluding me even potentially obvious ones like this.
To find the running mono process ids in Linux:
ps ax | grep mono
This will give you something like:
user 1452 0.0 0.0 9396 876 pts/4 S+ 17:33 0:00 grep mono
user 2810 98.2 16.1 967424 330432 ? Sl Mar30 7866:50 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.logappconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
The 2nd column contains the process id which you can kill with:
sudo kill -9 2810
Excluding grep from ps output using a character class
Thanks to #Yevgeniy comment, you can exclude grep from the ps output by using a grep character class as explained in this question, e.g:
ps aux | grep [m]ono
Which will exclude the grep process from the ps output and give you something like:
2810 ? Sl 7861:07 /usr/bin/mono /usr/lib/mono/4.0/fastcgi-mono-server4.exe --appconfigdir /etc/init.d/mono-fastcgi /socket=tcp:127.0.0.1:9000 /logfile=/var/log/mono/fastcgi.log
This now means if you only have one process of mono running you can now kill it with this 1-liner:
sudo kill -9 $(ps aux | grep '[m]ono' | awk '{print $2}')