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!!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With