How to check a running process is loading or not in MAC OS X from a shell script?

I have a startup process for unloading, the command I 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 ran the command, it gives a message stating that no such process is loaded. So I need to have a check if the .plist file is currently being loaded, then it needs to be unloaded otherwise.

How can I achieve this .. please help. Thank you !!


source to share

1 answer

You can get information about starting processes with launchctl


One possibility is to query launchd

using a command launchctl list


list [-x] [label]

With no arguments, list all 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 signal that killed the job. Thus, "-15" indicates that work was terminated with SIGTERM. The third column is the job label.

If your plist is loaded it must be listed, otherwise it isn't. Also the first column contains pid

processes, so you can check if a process is running, for example:

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


There is also a command launchctl print

that gives detailed output about the process. Check if you can use it.

print domain-target | service target

Prints information about the specified service or domain. The domain output includes various domain properties, as well as a list of services and endpoints in the domain, with state specific to each. Issuing a service 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




All Articles