How to restart launchd OS X without rebooting?

14

7

When I run Zotero/Firefox, they often crash and I am left with zombie processes; after this I cannot open new instances of Zotero or Firefox. I want to get rid of these zombie processes rather than rebooting, so for <pid> of the zombie process,

$ ps -p <pid> -o ppid=

gives me the <parent_pid> and

ps aux | awk -v PID=<parent_pid> '$2 == PID {print $0}'

tells me the parent process is /sbin/launchd for user crippledlambda.

Is there a way to restart this without killing my system?

sudo kill -1 <parent_pid>

does nothing. I've tried writing this in a script and running it with sudo:

for i in `launchctl list | grep launchd | awk -v PID=<parent_pid> '$1==PID { print $NF }'`; do `launchctl stop $i && launchctl start $i` ; done

and this obviously(?) leaves me with an unresponsive gray screen so I have to reboot anyway. Thanks in advance for your suggestions.

hatmatrix

Posted 2013-04-20T18:30:29.473

Reputation: 774

Why are you killing launchd instead of the leftover processes themselves? launchd is a core part of the user session; even if you could restart it, the new instance would not be the parent of all the other processes it's responsible for... things like the Finder, Dock, etc. – Gordon Davisson – 2013-04-21T04:13:15.417

@Gordon, you cannot kill zombie processes so you have to go after the parent, unless I'm missing something. – hatmatrix – 2013-04-23T12:13:33.307

Are they true zombies (i.e. processes that have exited, but whose exit status has not been read)? If they are, something much deeper is wrong, since launchd should always read its children's exit statuses immediately. If they aren't truly zombies, then you should be killing them. In either case, killing launchd is going to cause more problems than it solves.

– Gordon Davisson – 2013-04-23T15:55:25.857

Answers

17

The proper way to stop and start launch daemons is launchctl unload.

For example:

You can stop a launch daemon service using the unload subcommand of launchctl.

$ sudo launchctl unload /System/Library/LaunchDaemons/<daemon name>.plist

To start a disabled or stopped launch daemon:

$ sudo launchctl load /System/Library/LaunchDaemons/<daemon name>.plist

★ Be careful with disabling launch daemons haphazardly - especially the official Apple ones; it can potentially make your system unbootable until you start in safe mode and manually re-enable them. You don't kill a launchctl like a normal process because it can potentially kill your system like you've experienced.

> more info on launchctl here. (Apple launchctl man page)

Mena Ortega

Posted 2013-04-20T18:30:29.473

Reputation: 171

For future reference, these commands also work with scrun simctl spawn <sim_id> sudo launchctl unload …. The plist files are under $(xcode-select -p)/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/LaunchDaemons/ – Leo Natan – 2017-04-19T10:49:40.257

Sorry, is that com.apple.bsd.launchdadd.plist? I don't see launchd exactly. And thanks for the warning... – hatmatrix – 2013-04-23T12:15:47.003

3

From the launchd man page:

You cannot invoke launchd directly.

Hence even if you could stop it you would not be able to start it so the answer is that there is no way.

ostergaard

Posted 2013-04-20T18:30:29.473

Reputation: 341

1Why the downvote? My post correctly answers the question asked. Granted the other post is more constructive but it actually fails to answer the actual question as per the title of the post! – ostergaard – 2018-01-01T12:18:32.417

The other post does answer the question. – DavidPostill – 2018-01-01T12:41:32.830

2No it doesn't, it says how to restart daemons not launchd itself. – ostergaard – 2018-01-01T12:44:12.850

{shrug} whatever. In any case complaining about downvotes is not constructive. Clearly people think your answer is not useful. – DavidPostill – 2018-01-01T12:46:33.080

3lol - and it's that attitude, that accuracy no longer matters, that explains the slow decline in quality on SO. Happy New Year! – ostergaard – 2018-01-01T13:00:37.823

1Too bad I can't downvote comments. Not only this answer is correct, the other answer fails to actually address the question at all. This isn't about daemons started by launchd, but zombie processes inherited by launchd when an unresponsive process has been forcibly terminated. I have the same problem and no bloody daemon to kill since it's a browser that triggers the faulty behaviour in launchd. Replying with "whatever" gets my blood boiling as the same people who have found the answer useful fail to understand the question in the first place and that's worse by an order of magnitude. – SaltwaterC – 2019-12-23T20:01:52.710