0

I'm trying to accomplish a simple task and getting stuck. I've written a very simple script

#!/bin/bash

cd /Users/nathan/Downloads/
mv test test2
mkdir test

This works when I manually invoke it. But when trying to schedule it via launchd I'm getting 'mv operation not permitted' in the stdout.log. Can anyone advise? Thanks!


Yes, the cd was working fine.

Yes, launchd...is a pain some times. I've grown a little more familiar with it but...a 'little' lol.

Here's the script

#!/bin/bash

cd /Users/nathan/Kometa/config
# Define your Discord webhook URL
DISCORD_WEBHOOK_URL="webhook"
# Optional: You can customize the username and avatar for the webhook
USERNAME="Bash Bot"
AVATAR_URL="https://example.com/avatar.png"  # Replace with an actual image URL
#Define the directory name with spaces
directory_name="28 Days-Weeks Later"
# Define the path to your directories
base_path="/Users/nathan/Kometa/config"
assets2_path="$base_path/assets2"
assets_path="$base_path/assets"



# cd /Users/nathan/Kometa/config

# DIR="/Users/nathan/Kometa/config/assets2"

# Check if the directory exists
if [ -d "$assets2_path" ]; then
    # Directory exists, remove it
    rm -r "$assets2_path"

    # Define the message you want to send to Discord
    MESSAGE="@everyone Assets2 Was Removed for the Upcoming Operation"

    # Send the message using curl
    curl -X POST "$DISCORD_WEBHOOK_URL" \
        -H "Content-Type: application/json" \
        -d "{\"username\": \"$USERNAME\", \"avatar_url\": \"$AVATAR_URL\", \"content\": \"$MESSAGE\"}"

    #older assets2 removed proceed        
    # Rename assets to assets2 and create a new assets directory
    mv "$assets_path" "$assets2_path"
    mkdir "$assets_path"

    # Check if 28 days-weeks later directory exists in assets2
    if [ -d "$assets2_path/$directory_name" ]; then
        cp -r "$assets2_path/$directory_name" "$assets_path"
    else
        echo "Loaned Directory $directory_name does not exist."
    fi

    # Send a success message using Discord webhook
    MESSAGE="@everyone Kometa Assets Were Successfully Pruned"

    # Send the message using curl
    curl -X POST "$DISCORD_WEBHOOK_URL" \
        -H "Content-Type: application/json" \
        -d "{\"username\": \"$USERNAME\", \"avatar_url\": \"$AVATAR_URL\", \"content\": \"$MESSAGE\"}"

else
    # Directory doesn't exist proceed with mv
    # Rename assets to assets2 and create a new assets directory
    mv "$assets_path" "$assets2_path"
    mkdir "$assets_path"

    # Check if 28 days-weeks later directory exists in assets2
    if [ -d "$assets2_path/$directory_name" ]; then
        cp -r "$assets2_path/$directory_name" "$assets_path"
    else
        echo "Loaned Directory $directory_name does not exist."
    fi

    # Send a success message using Discord webhook
    MESSAGE="@everyone Kometa Assets Were Successfully Pruned"

    # Send the message using curl
    curl -X POST "$DISCORD_WEBHOOK_URL" \
        -H "Content-Type: application/json" \
        -d "{\"username\": \"$USERNAME\", \"avatar_url\": \"$AVATAR_URL\", \"content\": \"$MESSAGE\"}"
fi

And here's the launchd agent

<?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.nathan.assetpurge</string>
    <key>ProgramArguments</key>
    <array>
      <string>/Users/nathan/daps_other/myscripts/other/assetpurge.sh</string>
    </array>
    <key>RunAtLoad</key>
    <false/>
    <key>StartCalendarInterval</key>
    <array>
      <dict>
        <key>Day</key>
        <integer>1</integer>
        <key>Hour</key>
        <integer>1</integer>
        <key>Minute</key>
        <integer>00</integer>
      </dict>
    </array>
    <key>StandardErrorPath</key>
    <string>/tmp/stdout.log</string>
    <key>StandardOutPath</key>
    <string>/tmp/stderr.log</string>
  </dict>
</plist>

Thanks for reading!

2
  • I don't have macos. Things you might want to check: Is the script running as the correct user? Is the cd command successful? Commented Feb 28 at 16:04
  • @Bodo I think this most likely has to do with the extra layers of protection that macOS add (which I don’t remember the name of) on top of the ordinary Unix permissions etc. It is likely that the user or the launchd service, when executing launchd jobs, has to be granted permission to make changes in the user’s home directory, or something. Commented Feb 28 at 16:10

1 Answer 1

0

I think the "extra Mac control stuff" is SIP. I didn't need to disable that but it's working because I added both Terminal and bash to "Full Disk Access" in "System Preferences".

3
  • I believe it would have been enough to add bash as the terminal is not involved with running the launchd job. Commented Mar 3 at 7:13
  • Yes, one should definitely not disable SIP! It is better to tell SIP that some applications should have special access, which is what you did. Commented Mar 3 at 7:15
  • Probably yes, wrt to adding just bash. That’s what I was thinking re SIP yes Commented Mar 3 at 19:08

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.