0

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.

  • If you know how to do it with LaunchAgent, why do you want to do it the complicated way? What is the problem you are trying to solve here? – nohillside Apr 7 at 8:17
1

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
| improve this answer | |
  • It does not help with periodic task or tasks that need to start in a specific time and date – park joh Apr 7 at 9:03
  • @parkjoh Also, for periodic tasks or tasks with a specific time/date, use either cron or a LaunchAgent. Doing this the hard way isn't useful. – nohillside Apr 7 at 9:09
  • Why not just use sleep 600 instead of sleep $((10 * 60))? – user3439894 Apr 7 at 13:23
  • @user3439894 I had to answer the question about how to change the sleep duration too often in the past :-) – nohillside Apr 7 at 14:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .