Mac / Cocoa - Process Independent of user - macos

I have one cocoa program (I built it as an app, but none of it needs to be an app, it could be a command line tool), that I need to start on boot, and remain running till shutdown, regardless of if the user logs out.
Is this possible, preferably non-root?
Let me know if you have any more questions.

Create a system level launchd daemon. You have to create a launchd plist file and install it to either /System/Library/LaunchDaemons or /Library/LaunchDaemons. Since you mentioned that you want the daemon to be running at all times until shutdown, you'll want to set the KeepAlive option to true in your property list file.

Related

How to create mac application, which will be always running in the background and collects Location?

I need to know how to create an application which will always be running in the background and collects location info once every 10 minutes. I don't know much about launch agents and daemons.
I have read a few examples where I found Launch agents is just a plist file. Should we launch the app through launch agents every 10 minutes? How to do that?
There's no simple answer to this; you really need to read—or at least familiarize yourself with—the Daemons and Services Programming Guide, particularly the "Creating Login Items" section.
If you plan on creating a sandboxed (App Store) application, that limits your options somewhat, but not dramatically (for this kind of application).
You need to decide if your background process needs a UI or not. I suspect not, so I'd direct you to the either the XPC Service or user agent route. These will be registered with launchd and launchd has about a billion different options for how and when your process gets launched, and one of those is periodically. For more info on those, see man launchd.plist, particularly StartInterval and StartCalendarInterval.
With this knowledge, I'd then suggest to do some internet and open source searches looking for examples. I'm sure you'll find plenty.

Start/stop process in embedded linux

I have my own embedded Linux system on PocketBeagle board. I have developed a simple gpio application in C that issues an on/off command to one of the pins of the connectors of the board. The application is called gpio_aa6 and located at /root.
The first challenge was to find a way to launch my application automatically after booting the board. I found two ways to do that; the first was to add an entry to etc/rcS directory. This entry is a simple script file that launches my application. The second way was to edit /etc/inittab file and add an entry to that file (::respawn:/root/gpio_aa6). In both these ways my application was launched successfully: but I am still not sure if this is the right way to launch my application automatically.
Then I came to the second challenge, how can I stop my running application, as the respawn re-launches the application if it's terminated?
I am communicating with the board in two ways; via a serial communication (using screen terminal) and via web sever (root#192.168.42.2). I have tried to use Ctrl+C, Ctrl+Z, Ctrl+\, but couldn't stop the program from being continue running. Then I used command "killall" with killsignals -9 or -15, it seems that the program is interrupted but it's launched again directly after that.
My application is to run infinitely, but I need to stop it sometimes to update it and re-launch it again.
Is there any suggestion how to overcome this problem?
Thanks.
Both solutions you have used are correct. I personally prefer the option of adding an init script to /etc/init.d though.
I believe the behavior that you observe that you apparently can't kill the program is because you are starting your program from inittab, with the respawn keyword, which precisely tells the init program to restart your application when it exits. If you actually check the PID of your application, you will see that it changes everytime you kill it.
Therefore, I would recommend you to use an init script instead, with which you can implement start and stop actions. See ./package/lldpd/S60lldpd for a basic example in Buildroot.

How to Check the Number of Times an Application has been opened on a Mac?

I have a script that imitates the war games logon and I was hoping to track the number of times an application has been open, so that way the script doesn't run every time I open a new terminal. Rather I would like it to run when I reboot the machine and open iTerm for the first time since reboot. Is there a way to track the number of times an application has been open since reboot? That way I can write a condition for an if statement.
If you want it to only execute once on boot, you could check for the existence (or not) of a specific file, and just touch it after it's opened for the first time, and delete it on shutdown or on startup before the full desktop/terminal window/environment loads in.

Launching an application from another process

We have an application that we have built as a bundle and we want to launch it from another process.
How should we do it?
From what I understand we can use openUrls(), openFile() or execve()
but I don't know which one better suits us.
Thanks
Since you're talking about an application, you don't want to go through the file association mechanisms. They're for opening documents, images etc. with an appropriate application. Since you don't seem to be sure what to ask, I'd say keep it simple:
The exec* family launches an executable directly. But note that it replaces the launching process with the launched application. Your launcher will stop executing at that point. If you want the launcher to continue to run, you want to use something that launches a subprocess. The low-level way is fork/vfork followed by exec, but it's far simpler to launch your app with system, which takes care of all that behind the scenes. (Assuming there are no security concerns about users on the other side of the world injecting execution paths).
If the launcher does not terminate as soon as it launches your app, you'll want to think about whether it "blocks" until the launched application terminates, or whether it launches the app asynchronously-- so that they then run in parallel. The launcher might also "wait" for the return value of the app, to check whether it succeeded and maybe do something afterwards. There are ways to do all that, but since we don't know what you need, I won't go into details.
In short: If the only job of your launcher is to start your app, use execl. If your launcher needs to do more, use system. If neither one quite fits your needs, you'll need to provide more information-- starting with the language your launcher is written in.
PS. Both of these have the advantage of generality and portability. They work for GUI and commandline applications, and they'll work on any Unix-like system, and to some extent on Windows. There's no need to lock yourself into Cocoa for something so simple.
If you're using Cocoa, you can use NSWorkspace's -launchApplication:.
From OSX documentation on NSWorkspaces:
openFile: Opens the specified file specified using the default application associated with its type.
openURL: Opens the location at the specified URL.
With url you can open also file on ftp, or http for example.

Where to place a shell script?

Hello fellow computer people :)
I have a shell script that I will use as a watchdog timer. It checks to see if my other 'main' program is running. If it is not, it restarts it.
The question is: How do I get this installed on a Mac? Is there a folder/plist file scenario somewhere where the OS will automatically and periodically call the script, ensuring that my program never goes that long without running? I would ideally like to test every minute, but every hour or even a couple of times a day would be satisfactory.
Thank you!
The way to do this on Mac OS X is using Launch Services. It replaces the older system services such as init and crontab and provides a single, unified framework for managing system services.
In your case, you probably don't need a separate script - keeping an instance of your app running should be handled by the system. First you need to create .plist file that describes your daemon/script/application. You place it in one of the following locations, depending on the type of service:
~/Library/LaunchAgents: Per-user agents provided by the user.
/Library/LaunchAgents: Per-user agents provided by the administrator.
/Library/LaunchDaemons: System wide daemons provided by the administrator.
/System/Library/LaunchAgents: Mac OS X Per-user agents.
/System/Library/LaunchDaemons: Mac OS X System wide daemons.
Once you have defined the service, you can use the launchctl command to control launchd. For example, you can list running services, start/stop services, and so on.
The full documentation is here:
Creating launchd Daemons and Agents
Daemons vs Agents
I'm not a Mac user but there should be cron daemon. http://hints.macworld.com/article.php?story=2001020700163714
Crontab should do you nicely. Set your script to run every X minutes and cron will do the rest. If you prefer a GUI interface to your programs, try cronnix.

Resources