Where to place a shell script? - macos

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.

Related

Monitor what processes are writing to a file on Windows from Go

I am writing a Go app targeted at macOS and Windows and that needs to monitor what processes write to a file at a given path. More specifically, I need to verify that only one specific process writes to the file for the duration that my program is running. On macOS, I can monitor the file via the built-in fs_usage command. Does anyone have an idea for how to achieve equivalent monitoring on at least Windows 10 and later without requiring the user to install any additional software.
Note that I don't expect for there to exist a pure Go solution and I don't mind interoperating to achieve the desired result.

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.

How can I run an alert program when a Windows Service fails to start?

I have a Windows service whose start-up is set to 'Automatic', so it starts whenever the system boots up.
But this jobs sometimes did not start due to unknown reasons. I want to setup an alert program (.bat) to run whenever the service fails to start.
I tried using Recovery Mode options in service property, but it is not working. I think it is for jobs crashing with errors.
You can create a script that lists the service of yours and see whether it is running (using the sc command). Then you might to put that script to Startup menu. Should the script run before the service is started, you would need to schedule the script to be running periodically (to give the service some time to be started) using either of schtasks, at, or back in the nt4 days there was (still functional I presume) nice gui version of at in nt 4 resource kit called winat, if you have access to it.

Mac / Cocoa - Process Independent of user

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.

Windows service porting to linux

I am porting an application which runs as a background service in windows at startup, we are porting the application to linux(SUSE Enterprise server), I'am completely new to linux. Can somebody help me on how to proceed with this. Like
Should I build the linux executable
After builiding the binary, what changes should I make to linux startup files to run this executable
How my service can register call back function to modify or change or send commands to my service while it is running
Yes, you should build a Linux binary. You may want to rephrase your question since I doubt this is the answer you want :-)
You should generally create what is known as an "init" file, which lives in /etc/init.d. Novell has a guide online which you can use to author the file. Note that while the init file is common, the exact method of letting the operating system use it varies depending on the distribution.
This is going to be a marked change for you. If you are doing simple actions such as re-loading a configuration file, you can use the signals functionality, especially the SIGHUP/HUP signal which is generally used for this purpose. If you require extended communication with your daemon, you can use a UNIX domain socket (think of it as a named pipe) or a network socket.
Another task you are going to need to accomplish is to daemonize your application. Generally this is done by first fork()ing your process, then redirecting the stdin/stdout pipes in the child. There are more details which can be answered by reading this document
See how-to-migrate-a-net-windows-service-application-to-linux-using-mono.
Under Linux, deamons are simple background processes. No special control methods (e.g start(), stop()) are used as in Windows. Build your service as a simple (console) application, and run it in the background. You can use a tool like daemonize to run a program as a Unix daemon.

Resources