2

I'm trying to write a bash script to clean up my Downloads folder every day at a certain time. The bash script should get called then a python program afterwards to do the cleanup.

I'm using launchd to schedule the job but the job status code keeps returning as 2. I ran the executable of my bash script in the root directory as /Users/aabdi/Library/Scripts/clean.sh and it works fine. But when I load the file into launchctl I get a error code of 2.

Also the plist file is in ~/Library/LaunchAgents and when running putil on it returned 'OK'. My question is the error code states "No such file or directory" but it exists. Is there some sort of security permission on the Downloads folder?

plist file

<?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.aabdibot.cleandownloads</string>
    <key>Program</key>
    <string>/Users/aabdi/Library/Scripts/clean.sh</string>
  <key>EnvironmentVariables</key>
  <dict>
    <key>PATH</key>
    <string>/bin:/usr/bin:/usr/local/bin</string>
  </dict>
    <key>RunAtLoad</key>
    <true/>
  <key>StartCalendarInterval</key>
  <dict>
    <key>Hour</key>
    <integer>4</integer>
    <key>Minute</key>
    <integer>30</integer>
  </dict>
</dict>
</plist>

bash script

#!/bin/bash

Go to directory

cd /Users/aabdi/Downloads/

#print directory echo "$PWD"

#run python script python3 clean.py

Python script

#!/usr/bin/python3

import os from pathlib import Path

os.system("cd /Users/aabdi/Downloads")

for x in os.listdir(): tup = os.path.splitext(x) file_name = tup[0] ext_name = tup[1].lower()

if os.path.isdir(x) or ext_name == '' or ext_name == '.py' or ext_name == '.zsh':
    continue

cur_path = os.getcwd()
ext_name = ext_name.replace(ext_name[0], &quot;&quot;, 1)
fin_path = cur_path + &quot;/&quot; + ext_name 

print(f&quot;folder is {fin_path}&quot;)

if os.path.isdir(fin_path) == False:
    os.mkdir(fin_path)

cur_path = cur_path + &quot;/&quot; + x
fin_path = fin_path + &quot;/&quot; + x

os.rename(cur_path,fin_path)
print(f&quot;Moved {x}\n&quot;)

Greenonline
  • 2,004
A-A
  • 21
  • Sorry, I'm no good with plists, launchd, etc, BUT I do use LaunchControl extensively. A great utility IMHO. – Seamus Oct 04 '23 at 20:50
  • Check /var/log/com.apple.xpc.launchd/launchd.log – Martin R Oct 05 '23 at 00:13
  • I checked the log in Launch Control, it might be similar since it gives info for that specific job. It only gave me this exited due to exit(2)\n internal event: EXITED, code = 0. – A-A Oct 05 '23 at 01:12
  • Where is python3 installed? Does it help to call python3 with an absolute path from the shell script? – Martin R Oct 05 '23 at 04:34
  • 1
    Your Downloads folder is subject to privacy protection by the TCC (Transparency, Consent, and Control) system, so you probably need to grant the launchd item access to it. See this question (but note that you only want to grant access to Downloads, not Full Disk Access). Also, running cd via os.system() won't do anything useful, and when you run it in a script, you should always include an error check in case it fails (like it's doing here). – Gordon Davisson Oct 05 '23 at 10:00
  • Can you grant that within Settings. The Full Disk Access tab in settings lets you add applications to it. But the Files and Folder tab only works if the app requests it. – A-A Oct 06 '23 at 14:05

0 Answers0