I can not get launchd to run a shell script. Even a very basic script.

I'm using MacOS Sonoma in a Parallels VM. I have a development version of WordPress running in that VM, and everything works great. But I'm trying to get launchd to run a script every 5 minutes, but it will not run the script. It returns a 1 result, but no explanation of what the problem is.

I am able to get the plist to do something very basic every 5 minutes, so there isn't a problem with the plist running. But if I try to get it to run a script, the script never runs.


<key>ProgramArguments</key>

<array>

<string>/bin/zsh</string>

<string>/Users/devuser/scripts/strokewpcron.zsh</string>

</array>


The script is never called. Inside the script I have paths to things spelled out completely:


#!/bin/zsh


{

echo "$(date '+%Y:%m:%d %H:%M:%S') - Script started"

if /usr/bin/pgrep nginx >/dev/null 2>&1 ; then

echo "Nginx is running, proceeding with wget"

/usr/local/bin/wget --no-check-certificate 'https://mydevsite.com/wp-cron.php?doing_wp_cron' 2>&1

else

echo "Nginx is not running"

fi

echo "$(date '+%Y:%m:%d %H:%M:%S') - Script ended"

} >> /Users/devuser/Desktop/nginx-logs/wpStroke.log 2>&1


I am doing something wrong.


Posted on Jan 17, 2024 1:04 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 17, 2024 5:08 PM

I figured it out. I was trying to write to a log file which was in a Desktop folder.


By adding this line to the script:


exec 2>>/Users/devuser/Desktop/nginx-logs/error_log.log


I learned that line 9 was causing an error:


/Users/devuser/scripts/strokewpcron.zsh:9: operation not permitted:


And that line was writing to a file in a folder on my Desktop. I moved the location of that file to a file inside my home directory, and all errors went away. From ChatGPT, "This can occur due to macOS security features, especially those related to protected folders like Desktop, Documents, Downloads, etc."


Finally, it is working like a charm.



6 replies
Question marked as Top-ranking reply

Jan 17, 2024 5:08 PM in response to MrHoffman

I figured it out. I was trying to write to a log file which was in a Desktop folder.


By adding this line to the script:


exec 2>>/Users/devuser/Desktop/nginx-logs/error_log.log


I learned that line 9 was causing an error:


/Users/devuser/scripts/strokewpcron.zsh:9: operation not permitted:


And that line was writing to a file in a folder on my Desktop. I moved the location of that file to a file inside my home directory, and all errors went away. From ChatGPT, "This can occur due to macOS security features, especially those related to protected folders like Desktop, Documents, Downloads, etc."


Finally, it is working like a charm.



Jan 17, 2024 1:19 PM in response to Bow

That’s a pretty short launchd. I’ll assume there is more to that XML than what was shown.


In that more XML, is there anything written into the file passed as stderr? Something akin to this:

   <key>StandardErrorPath</key>
    <string>/Users/yourusername/Desktop/yourscriptname.err</string>
    <key>StandardOutPath</key>
    <string>/Users/yourusername/Desktop/yourscriptname.out</string>


Is the script marked as executable, and with its protections accessible?


Same for wget.

Jan 17, 2024 1:32 PM in response to MrHoffman

This is the full 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">

<plist version="1.0">

<dict>

<key>Label</key>

<string>com.devuser.strokewpcron</string>

<key>ProgramArguments</key>

<array>

<string>/bin/zsh</string>

<string>/Users/devuser/scripts/strokewpcron.zsh</string>

</array>

</dict>

</plist>


The script is marked as executable. I have no problem running it in terminal.

-rwxr-xr-x@ 1 devuser  staff  153 Jan 17 12:07 strokewpcron.zsh

Jan 17, 2024 2:17 PM in response to MrHoffman

So I did this:

ls -ale@ strokewpcron.zsh

-rwxr-xr-x@ 1 devuser  staff  122 Jan 17 13:59 strokewpcron.zsh

com.apple.TextEncoding 15 

com.apple.lastuseddate#PS 16


I tried this:

chmod -N strokewpcron.zsh

And this:

chmod -a# 0 strokewpcron.zsh

chmod: No ACL present 'strokewpcron.zsh'


And still no luck:

launchctl start com.hjmag.strokewpcron

launchctl list | grep com.hjmag

- 1 com.hjmag.strokewpcron


I did strip everything out of the strokewpcron.zsh so it just had a few comment lines, and when I launchctl started it, it did complete successfully. But the minute I have the script do anything, and all I have in it at the moment is: echo "Launchd worked" >> /Users/devuser/Desktop/nginx-logs/wpStroke.log


And the permissions on wpStroke.log are:

-rw-rw-rw-@ 1 defuser  staff  1110 Jan 17 13:24

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

I can not get launchd to run a shell script. Even a very basic script.

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