6

I have a launchd process to unload, the command I have used is

launchctl unload /System/Library/LaunchDaemons/costomscript.plist

it works fine if the process is already loaded. But if it is not loaded and I executed the command it gives a message saying something like no such process is loaded. So I need to have a check, if the .plist file is loaded currently then only it should be unloaded otherwise not.

How I can achieve this.. please help. Thanks!!

Govind Karmakar
  • 131
  • 1
  • 3
  • 8

2 Answers2

11

You can get the information about running processes with launchctl.

One possibility is querying launchd with launchctl list command.

list [-x] [label]

With no arguments, list all of the jobs loaded into launchd in three columns. The first column displays the PID of the job if it is running. The second column displays the last exit status of the job. If the number in this column is negative, it represents the negative of the signal which killed the job. Thus, "-15" would indicate that the job was terminated with SIGTERM. The third column is the job's label.

If your plist is loaded, it should be listed, otherwise not. Also first column contains pid of the process, so you could check if the process is running, for example:

$ launchctl list |grep myprocess
600 0   org.example.myprocess.1234

There is also launchctl print command that gives detailed output about a process. Check if you can use it.

print domain-target | service-target

Prints information about the specified service or domain. Domain output includes various properties about the domain as well as a list of services and endpoints in the domain with state pertaining to each. Service output includes various properties of the service, including information about its origin on-disk, its current state, execution context, and last exit status.

For example:

$ launchctl print gui/501/org.example.myprocess.1234 | grep state
    state = running
baf
  • 4,531
  • 1
  • 21
  • 24
  • Do you know, what are numbers in job's label after the last dot? .123 in your example. – julia_v Nov 01 '17 at 10:08
  • what if there is a negative number under the status label like : 3832 -9 com.apple.SafariBookmarksSyncAgent, what does the neagative sign means? – Sayan Mar 01 '18 at 09:34
0

Try:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$ launchctl print-disabled user/uid
$ launchctl print-disabled user/501

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

And look for "=> true" in your script.

E.g.

$ launchctl print-disabled user/501 
disabled services = {
    "com.apple.Siri.agent" => true
    "com.apple.FileStatsAgent" => false
    "com.apple.ScriptMenuApp" => true

login item associations = {
    "version.com.docker.helper" => "31259"
    "com.docker.helper" => "com.docker.docker"
    "version.com.paragon-software.ntfs.FSMenuAppLoginItemHelper" => "15.4.59"

Tony Barganski
  • 1,873
  • 20
  • 17