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], "", 1)
fin_path = cur_path + "/" + ext_name
print(f"folder is {fin_path}")
if os.path.isdir(fin_path) == False:
os.mkdir(fin_path)
cur_path = cur_path + "/" + x
fin_path = fin_path + "/" + x
os.rename(cur_path,fin_path)
print(f"Moved {x}\n")
launchd
, etc, BUT I do useLaunchControl
extensively. A great utility IMHO. – Seamus Oct 04 '23 at 20:50exited due to exit(2)\n internal event: EXITED, code = 0
. – A-A Oct 05 '23 at 01:12cd
viaos.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