0

I'm looking for a script that will kill all the processes on my Mac. To be more specific, the script should kill all the processes and shut down.


Well the point is that I want to kill all the procesess and restart Mac OS X but not shutting it down. I know that there exist commands like sudo shutdown -r now but it's not working on my Mac (that command is restarting it, but does not kill the processes after rebooting).

I can't use sudo shutdown - h now because I haven't got access to my Mac (only TeamViewer to connect to my Mac Mini). So I can't push the button on/off/restart.

Any ideas how to do it?

2
  • 1
    It often helps to ask for the actual problem you need to solve rather than your attempted solution. Why can't you just shut down the Mac without killing processes? The processes will quit anyway. What you're asking seems rather contrived. See also: mywiki.wooledge.org/XyProblem
    – slhck
    Sep 3, 2013 at 16:23
  • 1
    You say that sudo shutdown -r now does not kill the processes AFTER rebooting. Well, no. If you kill all the processes, that'll include the init process which will once again halt your system. Rebooting will necessarily restart a number of processes. Sep 5, 2013 at 16:17

3 Answers 3

3

As Daniel J. pointed out, why can't you use

   sudo shutdown -h now

This will kill all processes on your computer and shut down the computer. Why do you have to "rewrite" a script that is already done by the shutdown command?

2

Well you need a bash script that gets a list of all running processes and their PIDs and then uses a loop to kill each one of those PIDs like kill 1002 inside the loop, once done you can call the shutdown process. The thing is that the shutdown process does that for you already.

1
  • 1
    No need to write a bash script. killall should work. Or heck, just kill -9 -1. Sep 5, 2013 at 16:18
0

If there are only a few specific processes you want to terminate, you could use killall and then use AppleScript to send loginwindow a kAEShutDown Apple event:

killall process1 process2
osascript -e 'tell app "System Events" to shut down'

If you want to shut down without being asked to save unsaved changes and so on, try using sudo shutdown -h now. It sends all processes a TERM signal followed by a KILL signal, which should be relatively safe even with graphical applications. If an application supports sudden termination, it is supposed to be safe to send it a KILL signal.

Normally when you shut down, Cocoa applications are terminated by calling the applicationShouldTerminate: delegate method, non-Cocoa applications and background processes are sent a kAEQuitApplication Apple event, and daemons are sent a TERM signal followed by a KILL signal. See Daemons and Services Programming Guide.

Edit: if you want to disable reopening applications after restarting, you can run defaults -currentHost write com.apple.loginwindow TALAppsToRelaunchAtLogin -array '()' before sudo shutdown -r now. The list of applications to reopen at login is stored in ~/Library/Preferences/ByHost/com.apple.loginwindow.*.plist.

1
  • Well the point is that I want to kill all the procesess and restart mac OS but not shuting him down. I know that exist command like sudo shutdown -r now but it s not working on my mac (restarting, but not kill the proceses). I can t use command sudo shutdown - h now because
    – Macko
    Sep 5, 2013 at 12:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .