Link to home
Start Free TrialLog in
Avatar of anvendarnamn
anvendarnamnFlag for Sweden

asked on

Remove folder contents automatically every night in OSX

Hey guys!

I would like some help with removing a folders contents once every night automatically on an osx server.
Lets call the folder "level1". This folder is located on my firewire drive named "MAC". So the path is /Volumes/MAC/level1
I only want to remove its contents(files & folders) but want to keep the folder level1.
So the terminal command would look like this: rm -rf /Volumes/MAC/level1/*
I heard I could add it to com.apple.periodic-daily.plist but I dont understand how to add it. Should I make a script and add the script to com.apple.periodic-daily.plist or is it possible to just add the command in there somehow?
If you think im going the wrong way and know of a better way to do this please tell..
SOLUTION
Avatar of ishcabittle
ishcabittle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would actually create an automator action with this action. It's a little easier to manage and will cue up whenever you have set its schedule.
Avatar of anvendarnamn

ASKER

I havent had time to read all on the page you told me about yet ishcabittle but will do tomorrow.
JonyHolt, I dont think I can make it in automator if I want it to run without me being logged on to the server. I think I forgot to mention that I want it to run without me having to log on though.
You can actually if you create it as a cron job. A very GUI way of doing this is to create a repeat calendar event that runs an alarm script (or automator action). This is typically what we do to run cleanups or workflows without anyone being logged in.
Avatar of ishcabittle
ishcabittle

Cron is a good solution on earlier versions of OS X Server, but Apple has been moving away from Cron and heading towards Launchd for a couple versions now.

If you do decide to run your script via iCal, make sure that the user that you set the iCal alarm in has privileges to delete the content in /Volumes/MAC/level1

Your script would be pretty simple:

#!/bin/bash

# this script will remove the contents of /Volumes/MAC/level1 and print the date/time of completion to a text file called level1_deletion.log

rm -R /Volumes/MAC/level1
date >> /Volumes/MAC/logs/level1_deletion.log

Open in new window


With the date log, you can verify that the operation has completed (or at the least the script has fired).
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
So I have made a plist and its located in /Library/LaunchDaemons but it doesnt seem to happen anything at all.
The plist looks like this:

<?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>se.test.testlaunchd</string>
	<key>ProgramArguments</key>
	<array>
		<string>rm</string>
		<string>-R</string>
		<string>/Volumes/MAC/level1/*</string>
	</array>
	<key>QueueDirectories</key>
	<array/>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>14</integer>
		<key>Minute</key>
		<integer>43</integer>
	</dict>
	<key>WatchPaths</key>
	<array/>
</dict>
</plist>

Open in new window


It is supposed to run every day at 02.43 PM. I just used that time to test it but nothing happens at that time.
What could I be doing wrong?
Is it that I cant put that terminalcommand straight into lingon?
Do I need to make a script and tell this plist to run the script at that time instead of a terminalcommand?
If you used lingon, all you need to do is reboot. If you did it manually, you need an additional command. I have not looked at the plist, assuming you used lingon.
I made the plist in lingon and checked that it was in the right directory. Then I restarted my mac, logged back in with the same user I made the plist with and waited for something to happen but nothing at all happened.
Could it be that Im missing something? Im very new to scripts on mac..
Have you checked the "enabled" box?
So I made the script again now and can see in the console that it tried to do its job but with the error "no such file or directory".
So that means that my command is wrong.. When I made it this time I tried with a local folder that I made instead of the one on my firewire disk.
I made a folder on my desktop called level1 that Im trying to clean. So the command I used now is rm -R /Users/myusername/Desktop/level1/*
As I said in an earlier comment I just want to clean out the contents of the folder "level1". Is my command wrong?
enabled is checked, yes
It looks like the <Program Arguments> strings have been separated out into three separate strings what happens if you change:

<string>rm</string>
<string>-R</string>
<string>/Volumes/MAC/level1</string>

Open in new window


to:

<string>rm -R /Volumes/MAC/level1</string>

Open in new window


You might also set up your lingon plist to target a script that contains your commands instead of embedding the commands into the plist, but let's see if this solves it before we go that route.
A couple of things.

1. No problem with program arguments being split.
2. I may be wrong, but I do not think program arguments would work in one string as suggested in the previous comment, unless quoted.
3. The WatchPaths and QueueDirectories keys should not be there. I do not know if they are interfering with the operation of the script, but they are superfluous and I suggest that you delete those 2 lines.
4. I am wondering whether the asterisk may be an issue. Try entering the command in the "what" field as follows:
rm -rf "/Volumes/MAC/level1/*"
5. Just to confirm, this should be a "users daemon", which puts it in /library/launchdaemons/, and you need to reboot.

If the above does not solve it, we will figure out whether it the daemon is not running, or running and failing.
AHA! The wildcard IS the problem. See http://hints.macworld.com/article.php?story=20101005132236801

Apparently, they do not work by default in launchd scripts.

You can either:

1. enable wildcards by adding the following key (I would put it right before program arguments):

<key>EnableGlobbing</key>
 <true/>

2. work around the limitation by, for instance, calling a script that deletes the directory (instead of deleting the contents), and then recreates the directory.
Sweet! We are almost over the finish line...
The script looks as follows atm.
<?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>EnableGlobbing</key>
	<true/>
	<key>Label</key>
	<string>se.test.testlaunchd</string>
	<key>ProgramArguments</key>
	<array>
		<string>rm</string>
		<string>-R</string>
		<string>/Volumes/MAC/level1/*</string>
	</array>
	<key>QueueDirectories</key>
	<array/>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>10</integer>
		<key>Minute</key>
		<integer>15</integer>
	</dict>
	<key>WatchPaths</key>
	<array/>
</dict>
</plist>

Open in new window


It works if im logged on but not if im logged off. Its made as a users daemon so that might be the problem?
The solution I used was to use Lingon because it looked very easy.

I think I might have been missing to restart the computer or something. Today I did this again and now it works. Thank you very much for all the help guys.
This is how it looks when I want to remove the contents of a folder named "testar" on the desktop every day at 09.58AM.
<?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>EnableGlobbing</key>
	<true/>
	<key>Label</key>
	<string>com.test.testar</string>
	<key>ProgramArguments</key>
	<array>
		<string>rm</string>
		<string>-rf</string>
		<string>/Users/admin/Desktop/testar/*</string>
	</array>
	<key>QueueDirectories</key>
	<array/>
	<key>StartCalendarInterval</key>
	<dict>
		<key>Hour</key>
		<integer>9</integer>
		<key>Minute</key>
		<integer>58</integer>
	</dict>
	<key>WatchPaths</key>
	<array/>
</dict>
</plist>

Open in new window


Its made as a users daemon btw.
You should still remove the following lines 15, 16, 24 and 25, which are superfluous or (in the case of the unmatched <array/>s) incorrect.