You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Launch Agent problem

Hello, I have a problem with a particular plist file that executes one of my scripts.

The script executes a named pipe over the command:

pmset -g pslog

in other to monitor the power source and its work perfectly if executed manually or with a cron job.

I am trying to use launchd to execute this script because I am migrating all my automatic script execution over this service.


The content of the net.minilan.ps_observer.plist file is the following:

<?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>EnvironmentVariables</key>
    <dict>
      <key>PATH</key>
      <string>/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    
    <key>Author</key>
        <string>PB</string>
    <key>Description</key>
        <string>Monitor power state and export over MQTT</string>
    <key>Version</key>
        <string>0.1</string>


    <key>Label</key>
    <string>net.minilan.ps_observer</string>
    
    <key>ProgramArguments</key>
    <array>
      <string>/bin/bash</string>
      <string>~/Development/ps_observer.sh</string>
    </array>
    
    <key>RunAtLoad</key>
    <true/>
    
    <key>KeepAlive</key>
    <dict>
        <key>Crashed</key>
        <true/>
    </dict>
    
    <key>ThrottleInterval</key>
    <integer>10</integer>
</dict>
</plist>


This .plist file is located in ~/Library/LaunchAgents because I no need to execute this with other users or without login users, just with my user.


Then I have loaded it with:

launchctl load ~/Library/LaunchAgents/net.minilan.ps_observer.plist


Unfortunately, that plist file doesn't work.


The cronjob "equivalent" configuration (non exactly equivalent) is:

@reboot cd ~/Development && ./ps_observer.sh


Many thanks for any help.


Mac mini, macOS 10.13

Posted on Jun 4, 2021 8:38 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 5, 2021 1:48 PM

I have tried the Soma LaunchControll trial software and I have discovery that having a "sudo" command in the script the plist can't be placed in LaunchAgents, but it must be placed in LaunchDaemons.

Adding the key UserName and executing the plist as Daemons, it works properly now.

Many thanks for the suggested program.

Similar questions

5 replies
Question marked as Top-ranking reply

Jun 5, 2021 1:48 PM in response to BDAqua

I have tried the Soma LaunchControll trial software and I have discovery that having a "sudo" command in the script the plist can't be placed in LaunchAgents, but it must be placed in LaunchDaemons.

Adding the key UserName and executing the plist as Daemons, it works properly now.

Many thanks for the suggested program.

Jun 4, 2021 9:16 AM in response to paolo_83

Script management with launchd

The launchd process is used by macOS to manage daemons and agents, and you can use it to run your shell scripts. You don’t interact with launchd directly; instead you use the launchctl tool to load or unload launchd daemons and agents.

During system startup, launchd is the first process the kernel runs to set up the computer. If you want your shell script to be run as a daemon, it should be started by launchd. Other mechanisms for starting daemons and agents are subject to removal at Apple’s discretion.


https://support.apple.com/guide/terminal/script-management-with-launchd-apdc6c1077b-5d5d-4d35-9c19-60f2397b2369/2.8/mac/10.13

Launch Agent problem

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.