Unable to stop Webrick launched by "rackup" - ruby

I'm developing a Sinatra application and I'm using "rackup" to start Webrick. What should I do to stop it? Now I'm using Ctrl+Z and it seems like it stops. However when I try to start it again it will say that the port is already bound.
I tried it with many ports and each time it started, stopped and then said it was in use when I restarted it again.
How do I solve it?

Ctrl+Z will just "pause" the process, not terminate / kill it.
To truly kill it, find it in the process table and do kill -9 [PID]
like:
ps auxwww | grep ruby
slivu 16244 0.0 0.5 2551140 61220 s020 R+ 1:18AM 0:10.70 ruby app.rb
the second column(16244) is the PID.
The other way is to "catch" the INT signal with Ruby and exit the app explicitly.
in your app:
Signal.trap 'INT' do
Process.kill 9, Process.pid
end

Extending on slivu's reply,
use CTRL+C to kill the process if you are still in the same terminal.
If you are launching it in the background, or want to kill from a different terminal, use
ps aux | grep [r]ackup | awk '{print $2}' | xargs sudo kill -9

Related

ROS/Linux: What exactly does '&' in the linux terminal?

I am working with ROS. And for starting ros-packages you need to have the ROS Master run in the background. Now when I want to start the ROS-package rviz, instead of opening two terminals:
Terminal1:
$ roscore
Terminal2:
$ rviz
I can do the follwing in one Terminal:
$ roscore& rviz
But what exactly is happening here? Because when I end that terminal with Str+C it only closes rivz, but roscore is kept running in the background? Why and how can I close it?
in case using single & the left side will run in the background, while the right side will run normally in the terminal.
Now to close the first process you need to find PID (Process ID) and do the termination command, so first of all you need to find PID and you can use pgrep (in your case PROCESS_NAME can be roscore):
pgrep -f PROCESS_NAME
Now to kill the process you can easily do:
kill -9 PID_HERE
Or you can do it by single command:
pgrep -f PROCESS_NAME | xargs kill -9

How do I kill background processes / jobs started by a bash script after it finishes executing?

So I want to start a docker image, then a Django back-end and finally an angular front-end, let them run as long as I need to do tests/develop and then kill them when I'm done. To do this I first tried starting them all in a script and have them run in a background, and have a second script do kill %n for both processes. This doesn't work because the background processes are in another context, so the second script cannot reference them.
Then I tried this:
#!/bin/bash
# Exit Angular, Django and kill docker_img
function clean_up()
{
echo "Exiting..."
kill %2
kill %1
docker stop docker_img
reset
exit
}
# Trigger cleanup on CTRL + C
trap clean_up SIGINT
# Start docker database
docker start docker_img
# Start django backend
cd ~/Projects/DjangoBackend
source venv/bin/activate
python src/manage.py runserver &
sleep 3
echo 'Done starting django, starting angular'
sleep 1
# Start angular front end
cd ~/Projects/AngularFront
npm start &
However, after npm start & runs, the trap stops working, so it effectively becomes useless. I'm guessing it could be because once my script is done running the trap is no longer active, but I don't know how to fix this. What can I do?
If you are looking to kill a process in unix/linux, one way of doing it is you can record their PID in a file using ps -ef command.
And then use kill -9 to kill the process.
Example:
$ ps -ef | grep <process_name> | awk -F ' ' '{print $2}' > pid.txt
$ kill -9 `cat pid.txt`
ps -ef command will give all the running processes, using grep and process name, you can get PID of the particular process
awk is used to extract only PID from above command
kill -9 will forcefully kill the process
The answer seems to have been pretty easy, all I had to do was add wait to the end of the script, which allows the script to wait until the processes are done executing. Since two of the processes are servers, they don't stop unless prompted, so it'll just wait until SIGINT is received, at that point it'll run the clean_up function and exit gracefully.
Additionally, one could use the same trap but with the EXIT trigger instead of SIGINT to clean up when the script exits on it's own due to the processes closing.

Starting and stopping a process daily using the shell?

I know I can start the process with cron, but how would I go about stopping it? I thought about starting a new process1 everyday at 00:00, then at 23:59 I'd start a process2 doing ps -aux | grep process1, getting the pid from the line and then killing it, but I was wondering if there's be a better way to do it.
I could use Python or Java too if it's easier with either language.
Record its PID:
nohup my_service &
echo $! > /var/run/my_service.pid
and then kill by that PID.
You might want to check both PID and process name to make sure that during the day the process did not crash and some other process did not get that PID. (And even better idea would be to set up supervisor.) But if you are sure that there are no other process with that name you can simply use pkill or killall.

How to kill a process in MacOS?

I tried kill -9 698 but the process did not die.
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
$ kill -9 698
$ ps -ef | grep chromium
502 698 811 0 0:01.24 ?? 0:07.28 /Users/lucius/chromium/src/xcodebuild/Debug/Chromium.app/Contents/MacOS/Chromium
502 854 732 0 0:00.00 ttys001 0:00.00 grep chromium
If you're trying to kill -9 it, you have the correct PID, and nothing happens, then you don't have permissions to kill the process.
Solution:
$ sudo kill -9 PID
Okay, sure enough Mac OS/X does give an error message for this case:
$ kill -9 196
-bash: kill: (196) - Operation not permitted
So, if you're not getting an error message, you somehow aren't getting the right PID.
Some cases you might want to kill all the process running in a specific port. For example, if I am running a node app on 3000 port and I want to kill that and start a new one; then I found this command useful.
Find the process IDs running on TCP port 3000 and kill it
kill -9 `lsof -i TCP:3000 | awk '/LISTEN/{print $2}'`
If you know the process name you can use:
killall Dock
If you don't you can open Activity Monitor and find it.
Do you can list the process using a port with command lsof, for example:
lsof -i tcp:PORT_NUMBER_HERE
Replace the word PORT_NUMBER_HERE to the port number that you are using, then a the process running on the port will be listed. Then you have just to kill the process ID founded like this:
kill PID_NUMBER
Where PID_NUMBER is the process ID running on the port.
If kill -9 isn't working, then neither will killall (or even killall -9 which would be more "intense"). Apparently the chromium process is stuck in a non-interruptible system call (i.e., in the kernel, not in userland) -- didn't think MacOSX had any of those left, but I guess there's always one more:-(. If that process has a controlling terminal you can probably background it and kill it while backgrounded; otherwise (or if the intense killing doesn't work even once the process is bakcgrounded) I'm out of ideas and I'm thinking you might have to reboot:-(.
I just now searched for this as I'm in a similar situation, and instead of kill -9 698 I tried sudo kill 428 where 428 was the pid of the process I'm trying to kill. It worked cleanly for me, in the absence of the hyphen '-' character. I hope it helps!
Given the path to your program, I assume you're currently running this under Xcode, and are probably at a debug breakpoint. Processes cannot be killed in this state because of the underlying implementation of breakpoints.
The first step would be to go to your Xcode process and stop debugging. If for some strange reason you have lost access to Xcode (perhaps Xcode has lost access to its gdb sub-process), then the solution is to kill the gdb process. More generally, the solution here is to kill the parent process. In your case this is PID 811 (the third column).
There is no need to use -9 in this case.
I have experienced that if kill -9 PID doesn't work and you own the process, you can use kill -s kill PID which is kind of surprising as the man page says you can kill -signal_number PID.
in the spotlight, search for Activity Monitor. You can force fully remove any application from here.
I recently faced similar issue where the atom editor will not close. Neither was responding. Kill / kill -9 / force exit from Activity Monitor - didn't work. Finally had to restart my mac to close the app.

Mac OS X terminal killall won't kill running process

I have an instance of lighttpd running. When I do "ps -axc" the process is listed as
"614 ?? 0:00.15 lighttpd"
But when I do "killall lighttpd" I get
No matching processes belonging to you were found
I'm on Mac OS X 10.5.6. Is there something I'm missing?
As per the other response, if it's not your process, prepend sudo if you're an administrator. If not, you may be out of luck.
Also, try sudo killall -9 lighttpd which sends the specific signal KILL instead of TERM.
Just to be sure you can also try sudo kill -9 614 using the PID.
Is the task written in the ps aux list in brackets? If so, it is a zombie, it is waiting some I/O task, which probably never completes. You can't kill it as far as I know.
Does it belong to you ? If you do
ps aux | grep lighttpd
that will give you the user id associated with that process (I'm guessing it's chowned to another user)
It works: killall -u root -c lighttpd
Try
sudo kill -9 `pgrep lighttpd`

Resources