How to use Launchctl for schedule application

catalinalaunchdmojave

I try to work with Launchctl and have a question:

How can I set up an application to run in a few minutes using Launchctl? (for example, open the program MyApp.app with arguments "hello" "world" Every 10 minutes)

I know that it can easily be done by creating Launch Agent, I looking for a way to do it using Launchctl directly to programmatically.

Best Answer

launchctl is for loading, manipulating and unloading LaunchAgents and LaunchDaemons, it doesn't directly support delayed execution. What you can do instead is

  • Run sleep $((10 * 60)); launchctl submit -l my_choosen_name mkdir /tmp/mydir
  • Run launchctl submit -l my_name -- bash -c 'sleep 6$((10 * 60)) && mkdir /tmp/mydir'
  • Have your LaunchDaemon plist call a script (instead of the application directly) and include the sleep in the script before it starts the application

To have it run every 10 minutes use

while sleep $((10 * 60)); do
    launchctl submit -l my_choosen_name -- myapp hello world
done