how to kill process in Mac OS X and not have it restart on its own

130,639

Solution 1

I tried to kill the process by sending it the TERM signal, and that worked. The command was:

sudo kill -15 {PID}

Solution 2

The process you are killing is probably being managed by launchd, the proper way to stop it and have it not restart is to use launchctl unload <path to plist>. The plist that controls that process is in either /Library/LaunchDaemons or /System/Library/LaunchDaemons. If it is a system process and not one of your own, then you will probably have to use sudo to get launchctl to work as desired.

A better way try and stop it might be;

${MYSQL_HOME}/bin/mysqladmin -u root -proot shutdown > /dev/null 2>&1

Solution 3

A couple of comments mention that "launchd is probably involved" - so I thought I'd put this out as an additional answer. As @jarrod-roberson says, you can check if launchd is involved by first running launchctl list | grep mysqld.

An important thing you learn here is whether MySQL was installed with Homebrew or not - Brew stores its launchctl files in a different location than where OSX puts the "regular" services.

On my OSX box, the plist files are in ~/Library/LaunchAgents/ So I ran:

launchctl unload -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

to stop the MySQL server. I had previously looked in /Library/LaunchDaemons/ and /Library/LaunchAgents but didn't find a file with mysqld in its name.

You can also install a brew-based system called services, to manage all Brew-installed services applications, as described in this post - http://robots.thoughtbot.com/starting-and-stopping-background-services-with-homebrew I haven't tried this myself, though, so YMMV.

Solution 4

For me, this worked once I figured out which label I was looking for.

launchctl list | egrep {DESIRED_LABEL}   
launchctl remove {DESIRED_LABEL}

Solution 5

Unload the service and stop the daemon:

sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysql.plist

Load the service and start the daemon:

sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysql.plist
Share:
130,639

Related videos on Youtube

Hristo
Author by

Hristo

Updated on September 17, 2022

Comments

  • Hristo
    Hristo over 1 year

    When I run sudo kill -9 [PID] with the proper process ID, the process stops but then is restarted and has a new PID. I'm trying to kill the mysqld process.

    How can I mimic the Activity Monitor in killing a process? In the Activity Monitor, when you press "Quit Process", the process permanently stops running, it is totally terminated. I figure that kill will do the same thing right?

    I had both the Activity Monitor and the terminal next to each other to see if the command works, but every time I do sudo kill -9 [PID], the process in Activity monitor doesn't go away, it just refreshes with a new PID.

    So... how do I kill the mysqld process via the terminal?

    • Yoosaf Abdulla
      Yoosaf Abdulla over 10 years
      I did it from the activity monitor because the 'kill' command was not recognising the PID. Again unlike your case my mysqld did not restart as soon as I killed it from the Activity monitor.
    • Jan Steinman
      Jan Steinman about 6 years
      Ouch! Don't use -9 unless you REALLY need to. It's a violent thing to do to a process. Other signals allow a process to terminate in an orderly manner, but not -9! So it means that RAM buffers don't get flushed to disk, for example. This is a particularly bad thing to do to a database that is in the process of doing work; you'll come back to damaged tables.
  • Hristo
    Hristo almost 14 years
    I'm trying to kill the mysqld process
  • Hristo
    Hristo almost 14 years
    I'm trying to kill the mysqld process. I'm not sure if that is part of LaunchDaemons... but the following command is the correct way to stop the server from running sudo /usr/local/mysql/support-files/mysql.server stop but I'm having problems with that, so I'm trying to kill the process directly.
  • Admin
    Admin almost 14 years
    if it is being restated after kill -9 the launchd is probably involved, even if indirectly. you can tell by using launchctl list
  • Hristo
    Hristo almost 14 years
    the list doesn't have "mysql" in it. I will try your command up top.
  • mipadi
    mipadi almost 14 years
    Probably managed by launchd, then, which will restart it if the process dies.
  • JJ_Australia
    JJ_Australia almost 14 years
    Redirect STDOUT and STDERR to /dev/null.
  • Hristo
    Hristo almost 14 years
    Using -15 as a flag opposed to -9 did the job. Thanks for your response!
  • Jeff
    Jeff over 8 years
    I had the same problem and was able to solve by removing mysql from launchd via sudo launchctl unload /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist
  • Eugene Morozov
    Eugene Morozov over 8 years
    This worked for me except that is was com.oracle.oss.mysql.mysqld.plist My local MySQL was installed from downloading from MySQL, not via homebrew.
  • Patrick
    Patrick about 8 years
    This is what fixed it for me. Always a pain when you are fighting multiple different ways to run a LAMP stack locally.
  • micjamking
    micjamking over 7 years
    I had to use the Homebrew path to remove this, i.e. /usr/local/opt/mysql/homebrew.mxcl.mysql.plist
  • Denys Kniazhev-Support Ukraine
    Denys Kniazhev-Support Ukraine about 7 years
    The launchd link is now broken.
  • Christia
    Christia about 7 years
    I can see com.oracle.oss.mysql.mysqld.plist in /Library/LaunchDaemons and have used the command sudo launchctl unload /Library/LaunchDaemons/com.oracle.oss.mysql.mysqld.plist only to receive the error message: Could not find specified service
  • K. Symbol
    K. Symbol about 4 years
    This helps me to finally kill Pulse Secure. Just note that the label for egrep can be partial keywords (like pulse), but the label for remove must be copied from the list output (like net.pulsesecure.pulsetray).