Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check for a launched process is loaded or not in MAC OS X from shell script?

Tags:

macos

launchd

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!!

like image 775
Govind Karmakar Avatar asked Jan 09 '23 12:01

Govind Karmakar


1 Answers

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
like image 114
baf Avatar answered Jan 16 '23 22:01

baf