Kill -9 -1 behaviour on MacOS - macos

Our teachers told us to experiment with the terminal and kill -9 -1
To my understanding, on UNIX based OS, the first process charged is Init with PID -1 from which the other processes will spawn. I assumed that you couldn't kill it as it is charged in a secured part of memory.
On a VM running LinuxMint, the command would would cause the session to close itself. On MacOS, it would close/crahs(?) all applications.
On some other people laptops running different distribution of Linux the command would be denied which was the behaviour I would have expected in any OS.
So I am confused by the behaviour of the command.
What should be the normal result? Or is it bound to each OS implementation?
Thanks.

The general behaviour, from Kill MAN page:
If pid equals -1, then sig is sent to every process for which the calling process has permission to send signals, except for process 1 (init)
So, "kill -9 -1" will kill all processes it can.

Related

How to Turn Off a Program Running from .bash_profile

I am working with a Raspberry Pi running linux, and have modified the .bash_profile file in order to a run a program I've made automatically upon login. This works great, but I was wondering how to turn the program off, since for ctrl+c I would need to be running the program in the terminal.
You can use Kill to terminate program by its process Id, Top command will list all the processes, You can also use Pkill, which will Terminate the process by its name.
-9 option forces process to shut down, so its very commonly used.
Example:
kill -9 "Process ID without Quotation marks"
pkill -9 "Name with Quotation marks (Case Sensitive)"
Check this.
I always use
pkill - f processname

How can I kill a Festival speech command?

Let's say I run the following command:
cat /var/log/dmesg | festival --tts
This might return the message [1] 4726, indicating a process ID associated with this operation. When I run kill 4726 or killall festival or killall cat or killall aplay, the speech does not stop (or, at least, it continues on for quite some time before stopping). If I run the command above, how can I kill what it starts doing?
Kill sends a SIGTERM to the program. SIGTERM tells the program to stop, allowing it to shut down gracefully. The program is unallocating memory, closing connections, flushing to disk, removing temp files, etc. So SIGTERM may not be immediate or quick.
Kill -9, sends a SIGSTOP or SIGKILL, which is only seen by the kernel. The kernel will terminate the process. While this is faster, it does not allow for a graceful exit.
I am not familiar with festival, so if you are worried that these commands are forking off processes and you want to stop all the children, you can brute force the issue by spawning them all out of a bash shell. When you kill the parent bash shell, it will kill all of the processes owned by it.
bash -c "cat /var/log/dmesg | festival --tts" &
You will get bash the pid for the bash shell, which you can kill and clean up all sub-procs.

How to replicate CTRL+C in a shell script?

I'm running a script that performs a command that lasts too long, so I want to interrupt it. Any help? I've tried to search a lot, and I've been pointed to the kill command. Although, I can't get it to work. By the way, I'm using a Mac OS X. Thanks!
Assuming you have the process' PID, send it a SIGINT signal:
kill -SIGINT PID
If you don't have the PID you can try pkill or killall, but they're somewhat less safe; a PID is the only way to uniquely identify a process. Right after you spawn the other process the PID should be in $!, so you can save it then
process-that-takes-a-long-time &
pid=$!
# other stuff
kill -SIGINT $pid
timeout 10 your_command
To replicate Linux Ctrl+C on a terminal on Mac OS X, use ctrl+C!
Its the same keystroke combination
Worked for me (first Steps on Mac)
edit: I didnt read the script is running in background.

os x process state UE

I've got process running that can't be killed. It's my EyeTV App and here is what 'ps aux' says:
cb0 87583 1,0 3,4 812796 144236 ?? UE 21Nov09 2638:11.45 [.....]/EyeTV
The process state is really strange as I've never seen UE before. The manpage tells me
U means Marks a process in uninterruptible wait
E means The process is trying to exit
But I'm not able to kill the process. Any Idea how I could force it to exit??
Additional Info: None of the following statments worked:
kill -S KILL
kill -S QUIT
kill -2
kill -9
Have you tried using
top
in order to get all active processes, and then
kill(PID)
where PID will be referring to the EyeTV process ID handed to you by top?
Also (and I am not sure this works two, but it just might), you could try
kill(pid,SIGTERM)
which sends a termination signal to the process (however that is used in C programming to kill a process, so I am not 100% sure it would work outside of the programming language, but worth a try)

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.

Resources