~/Library/LaunchAgents plist runs manually but not automatically - launchd

I am starting to work with launchd and want to set up a plist file such that whenever I insert an SD card into my Mac mini server (with Snow Leopard Server), I want a shell script to run (which should copy all the jpg files, rename them etc).
So, I created a plist file in the ~/Library/LaunchAgents (see below for its contents - it should be looking for changes to /Volumes) and I created a shell script which says "beep" - later it will do something more useful.
The plist file is registered with launchctl, and when I run it (launchctl start com.peters.runwhenSDmount), the computer says beep whenever a memory card is plugged in, and stays silent when there is no memory card. So, apparantly the plist does call the shell script, which subsequently checks if the specific SD card is there. I assume this also proves that there is no problem with permissions for the SD card.
But, it doesnt seem to run by itself??? Any idea why??
plist file: ~/Library/LaunchAgents/com.peters.runwhenSDmount.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<dict>
<key>Label</key>
<string>com.peters.runwhenSDmount</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/peter/Library/Scripts/runwhenSDmount</string>
</array>
<key>ThrottleInterval</key>
<integer>10</integer>
<key>WatchPaths</key>
<array>
<string>/Volumes</string>
</array>
</dict>
</plist>
shell script: ~/Library/Scripts/runwhenSDmount
#!/bin/bash
if [ -d "/Volumes/NIKON D40X" ]; then
say beep
fi

After you create a new plist in your ~/Library/LaunchAgents folder, you have to tell the launchd application about it. The two basic ways to do that are:
Logout then log back in. - Every time you log in, launchd will scan the contents of your ~/Library/LaunchAgents folder and add any plist it finds there.
Load the plist from a terminal command line with "launchctl". The syntax of the command is:
launchctl load {Path-to-plist}
The launchctl command can also be used to stop launchd from using a plist. To do that, use:
launchctl unload {Path-to-plist}
The launchctl command is very useful when developing plists since it makes unloading/loading them between changes quick and easy. Once you have a plist working the way you like, the automatic login launchd loading can take over.

I just had a similar problem with automatically launching the services in ~/Library/LaunchAgents, but in my case NONE of the *.plist defined services got started.
The problem was obviously connected to the directory ~/Library/LaunchAgents and not the plist files itself. The solution was to reset the file permissions.
chmod 700 ~/Library/LaunchAgents.
Update for homebrew users: (2015-02-23)
Yesterday I just found LaunchRocket which is a Mac PreferencePane for managing services with launchd. It is homebrew aware ands adds a nice UI for managing launchd homebrew services.
This may not help you with incorrect user permissions but it is open source so you can fork the project and add the permission check as a feature.

Two tools that help with the creation and management of launchd items are:
LaunchControl - "LaunchControl is a fully-featured launchd(8) frontend allowing you to create, manage and debug system- and user services on your Mac."
Lingon - "An easy to use yet powerful utility that runs things automatically on your Mac"
As a note: Brett Terpstra (who does a bunch of great Mac work on things like nvAlt) recently commented in his post "Triggering tasks remotely with notifyutil and launchd" that he used to use Lingon, but has been using LaunchControl more recently. Either of them is worth looking into.

Related

Apple Hypervisor is completely broken on MacOS Big Sur beta 11.0.1

I am trying to make a basic command-line tool that makes a VM and runs something using Apple Hypervisor.
When I try to run hv_vm_create(HV_VM_DEFAULT) it gives me an error code -85377023.
I tried enforcing App Sandbox and setting the entitlement accordingly for the Big Sur compile target, and now I am getting this output in the console:
Killed
From what I understand from this, this is AMFI killing my process. Is this a bug or my problem?
When I decided to go down the rabbit hole, I found that in the MacOS Big Sur 11.0.1 beta release notes, they deprecated hv_vm_run(_:), while the API documentation says that this function is in beta.
I didn't go as far as disabling AMFI with a kernel flag, but I am almost certain that this is not expected behavior. And that, no matter what, hv_vm_run(_:) can never be deprecated OR be in beta. I am sure that this function existed well before Big Sur.
If anyone can help me with this or just give a response, please do. Do not keep me in darkness as I don't want to waste time on something which is potentially broken.
Thank you.
P.S: I know that there is already a thread in the Apple Developer forums, I am the one who posted it. Though, no one is answering there because their community is not even close to being as large as Stack Overflow.
This error code you're seeing is just a poor UX on the macOS side: what it's really trying to communicate is that the com.apple.security.hypervisor entitlement is missing.
If you're using Xcode, you can add it like that:
Press ⌘N and create a new Property List file.
Add a new property with the key com.apple.security.hypervisor and a boolean value set to YES.
Go to Project → Target → Build Settings → Signing → Code Signing Entitlements and make sure it uses your newly created entitlements file.
Alternatively, you can use command line:
codesign -s - --entitlements app.entitlements --force path/to/your/binary
Where app.entitlements contents is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.security.hypervisor</key>
<true/>
</dict>
</plist>
The other way you to solve this is to simply set the Deployment Target to 10.13 in the Xcode's settings (props to Alexander Pinske):
The reason this works is most likely due to backwards-compatibility, so bear in mind that this might change in the future macOS releases, or, perhaps, on newly introduced platforms like Apple Silicon.

KNIME Command Line Execution - ClassNotFoundException

I'd like to schedule a KNIME workflow. The workflow does its job very good as long as I start it from the KNIME GUI application. When I execute the same workflow via command line, java complains that com.microsoft.sqlserver.jdbc.SQLServerDriver
could not be found (ClassNotFoundException).
I invoke it via:
"D:\Progamme\KNIME\knime.exe" -nosplash -application -consoleLog org.knime.product.KNIME_BATCH_APPLICATION -preferences="absolutepathto\preferences.epf" -workflowDir="absolutepathto\workflow"
Since the error message signals missing content in the java CLASSPATH I also tried to add the parameters
-vmargs -classpath .;"absolutepathto/sqljdbc42.jar"
But still I earn a java slap, pointing to the same error...
I also tried to run the command from within the knime.exe's directory and I also tried to add the JAR file to Preferences -> Java -> Build Path -> Classpath Variable / User Libraries (referenced via the -preference argument). But that had no effect.
Did anybody face the same problems? Maybe with other third party JARs?
It is all about a Database connector that is configured like this:
Does the integrated security maybe force a misleading error?
System spec: KNIME 3.2.2 on Windows Server 2008 R2
Update - extract from preferences file
/configuration/org.eclipse.core.net/org.eclipse.core.net.hasMigrated=true
/configuration/org.eclipse.ui.ide/MAX_RECENT_WORKSPACES=10
/configuration/org.eclipse.ui.ide/RECENT_WORKSPACES=<list of some workspaces>
/configuration/org.eclipse.ui.ide/RECENT_WORKSPACES_PROTOCOL=3
/configuration/org.eclipse.ui.ide/SHOW_RECENT_WORKSPACES=false
/configuration/org.eclipse.ui.ide/SHOW_WORKSPACE_SELECTION_DIALOG=true
Is there maybe a problem due to the fact that it is a shared KNIME instance among several users and the command line execution does not know which workspace has to be chosen? Is the workspace somehow needed and why?
Partial Solution:
I finally managed it but I don't know exactly why it works now. What I did was to load a fresh portable version of KNIME and ran the same commands only changing the executable path to the new portable version. Before that I started the portable version once to set the workspace directory and register the database driver in preferences dialog and .ini file, nothing else, same configuration so far as the shared KNIME instance. What I am really wondering abpout is that from now on the commands are also working with the shared KNIME instance. I really don't know what caused the change that let KNIME find the driver class.
Info
Because I encountered a few more problems within shared environment in KNIME command line mode, that led to undeterministic execution results, I wrote a little .NET library. This gives me more flexibility/control over the workflow execution (which returncodes and error messages occured and so on). You can find it here if you're interested: KnimeNet
I took a very minimal approach:
cd "C:\Program Files\KNIME"
.\knime -nosplash -noexit -consoleLog -reset -application org.knime.product.KNIME_BATCH_APPLICATION -workflowFile="D:\Work\Knime Workflows\Output\CMD_Test.knwf" -preferences="D:\Work\Knime Workflows\Output\CMD_Test.epf"

Run .bat as a Windows Service

I'm here again with another case that is getting me out my mind.
So, this is happening, I'm trying to run an executable java class(.jar) as a Windows Service, and all my attempts failed so far. To make it a little easier, I turned my jar into a batch file, wich only executes the jar in background, here is the code:
start "" javaw -jar C:\LocalService.jar
The batch works fine. However I have tried to install this batch as a service by using the next line in cmd:
sc create "LocalService" binPath= "C:\LocalService.bat"
The Service installs correctly, but as soon as I try to start it, it pops up an error (The code error is 1053, says something about the service did not start correctly)
Also, I have try with a software called NSSM (non-sucking service manager) It installs fine too, but the service does not start either.
Do you guys know what am I missing?
By the way, I'm doing all this on Windows 7 Professional.
Thanks!
thanks for your comments
Both tools didnt work for me, sadly. However I was able to do it with a software called Java Service Wrapper. This is not a free software, so I needed to buy a license to get it to work.
The steps were simple:
1.-Create a folder with the name of the service, then inside add 4 folders: lib,bin,logs,conf
2.-On the lib folder you have to copy your jar and also the wrapper.jar and wrapper.dll (these last two are in the zip you download from the website)
3.-Copy 4 files to the bin folder: InstallApp-NT.bat.in, App.bat.in, UnintstallApp-NT.bat.in and wrapper.exe (this last one is the one that defines your license, if you can get a file wrapper.exe from somebody else who had paid a license it will work fine. These file also came in the zip from the website) Remove the .in from the batch files
4.-The most tricky step is this: You have to copy from the wrapper's folder called conf a file called wrapper.conf.in Remove the .in extension and edit it with a tex editor. The most important lines you have to edit are:
wrapper.java.command=C:\Program Files (x86)\Java\jre7\bin\java //Specify JRE Path. Will work with eviroment variable
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperJarApp //Choosing this class means your are using a .jar file to execute when the service starts
wrapper.java.classpath.1=C:\LocalService\lib\wrapper.jar //This one is constant.
wrapper.java.classpath.2=C:\LocalService\lib\LocalService.jar //This is the path to your executable jar
wrapper.java.library.path.1=C:\LocalService\lib //Path to your own lib folder (the you created at the begining)
wrapper.app.parameter.1=C:\LocalService\lib\LocalService.jar // again the path to your jar
Then just execute the InstallApp-NT.bat and start the service and your are done
It worked to me with absolute paths, however according to documentation it will work fine with relative path too.
This is how I solved my problem and I hope someone with the same issue will find this helpful
See you next time!

Install Packages (for dummies)

I know there's a lot of information on here about installing python packages, but I'm quite new to python and I think I need a more "for dummies" level of help.
I was trying to install openpyxl for which as far as I can tell I need easy_install, for which, as far as I can tell, I need setuptools. I tried running the code provided here https://pypi.python.org/pypi/setuptools which is supposed to download and install setuptools (and according to some sites, easy_install aswell?) - it runs successfully, but help(modules) doesn't show setuptools or easy_install as modules, I have no idea whats installed and what isn't, or how I'm supposed to install any of it!
Essentially I'm very confused, very frustrated and really need someone to talk me through (in idiot-speak) what I'm supposed to do.
Thankyou!
We all start somewhere, I was there two weeks ago.
I'll assume you're using Python2. I believe Distribute and Pip are recommended for Python3 (which I will be using as examples). I will also assume you are on Windows.
First, python needs to be registered to Path. To check if this has been done automatically, open a command prompt (start -> programs -> accessories), and type 'python', then enter. If it returns the version number, etc, skip down a bit. If it throws an error, you need to add Python to Path.
Adding Python to Path
To add Python to Path on a Windows computer, go to:
Control Panel -> System -> Advanced Settings -> Environment Settings -> System Variables
Scroll down to select path, then click edit. Copy the entire line to a text document, and add your install directory for Python.exe (and the scripts folder) using ';' as a delimiter between different directories. Copy this back to Path and save. (Additionally close your command prompt window to reset it.)
For my Windows 7 machine, I added:
;C:\Python33;C:\Python33\scripts;
Take care when editing this file. There are many videos out there describing this in detail if you feel unsure about changing this.
Installing Modules (Such as setup_tools)
Once Python is registered in the Path file, download and unzip setup_tools to a folder within your Python install directory called 'modules'. I use ExtractNow to unzip, as it will unzip twice (as required) automatically.
Open a command prompt window again, and direct it to change directories by typing
cd [directory for module you want to install]
On my computer, this would be
cd C:\Python33\modules\distribute-0.6.40
Again, I use distribute, rather than setup_tools as it sounds like you need would for Python2. Simply use the appropriate directory. Press enter to change the directory.
Once you've entered this and it shows a changed directory, type:
python setup.py install
This indicates that you want to use the program python to use the setup.py file in the specified folder to install the module. This should be successful, and will write many lines of code.
If you want to install other modules, you would install them in a similar way. Python would automatically use setup_tools on your computer to finish each install.
Remember to import at the start of your script when using them to code:
import [module]
Happy Programming!

WAMP: How to "Install Database Schema by Running Command ..."

At a higher-ed institution and I am trying to install an app called "Guide on the Side" (https://github.com/ualibraries/Guide-on-the-Side). It sits on a LAMP stack of varying flavors, but--of course--we are a Windows-based institution. My question is of the "what does that mean!?" variety.
I have my db created, the config "configurated," but then there's the following procedure:
Install the database schema by running the following commands from the guide_on_the_side/app folder:
../lib/Cake/Console/cake Migrations.migration --plugin Tags all
../lib/Cake/Console/cake Migrations.migration all
Can someone translate? Can I actually "run" this in a windows cmd. I've some familiarity with terminals in *Nix OS's but I neeeeever touch the command line on Windows, so I'm just out of my element.
cake is a console command (shell script) for the CakePHP application framework.
http://book.cakephp.org/2.0/en/console-and-shells.html
You should be able to run the cake command from the Windows shell (it will refer to the cake.bat batch file).
You may need to make other adjustments for running CakePHP apps on IIS.

Resources