Remember to maintain security and privacy. Do not share sensitive information. Procedimento.com.br may make mistakes. Verify important information. Termo de Responsabilidade

How to Schedule Tasks on macOS Using Terminal and Automator

Scheduling tasks on macOS can be efficiently managed using built-in tools like Terminal and Automator. While macOS does not have a direct equivalent to Windows Task Scheduler, it offers robust alternatives for automating tasks.

Examples:

  1. Using launchd and launchctl:

    launchd is a system service manager in macOS that can be used to schedule tasks. Here's a step-by-step guide on how to create a scheduled task using launchd.

    • Create a Launch Daemon/Agent:

      Launch Daemons and Agents are defined in XML property list files (plist). These files are typically stored in /Library/LaunchDaemons for system-wide services or ~/Library/LaunchAgents for user-specific services.

      Example plist file to run a script every day at 10 AM:

      <?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>Label</key>
       <string>com.example.daily-task</string>
       <key>ProgramArguments</key>
       <array>
           <string>/path/to/your/script.sh</string>
       </array>
       <key>StartCalendarInterval</key>
       <dict>
           <key>Hour</key>
           <integer>10</integer>
           <key>Minute</key>
           <integer>0</integer>
       </dict>
      </dict>
      </plist>
    • Load the plist file:

      Save the plist file in the appropriate directory and load it using launchctl:

      sudo cp com.example.daily-task.plist /Library/LaunchDaemons/
      sudo launchctl load /Library/LaunchDaemons/com.example.daily-task.plist
  2. Using Automator:

    Automator is a macOS application that can automate repetitive tasks. You can create a Calendar Alarm in Automator to schedule tasks.

    • Create a Calendar Alarm:

      1. Open Automator and select "New Document."
      2. Choose "Calendar Alarm" as the document type.
      3. Add the actions you want to automate, such as running a shell script.
      4. Save the workflow, and it will create a new event in your Calendar app.
      5. Adjust the event's time and recurrence in the Calendar app to schedule the task.

To share Download PDF

Gostou do artigo? Deixe sua avaliação!
Sua opinião é muito importante para nós. Clique em um dos botões abaixo para nos dizer o que achou deste conteúdo.