Solved

Remove folder contents automatically every night in OSX

Posted on 2012-03-12
18
904 Views
Last Modified: 2012-03-20
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..
0
Comment
Question by:anvendarnamn
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 6

Assisted Solution

by:ishcabittle
ishcabittle earned 175 total points
ID: 37710318
It will be better to create your own launchd plist than to tack it on to the com.apple.periodic-dailey.plist, as future updates may modify that file and erase your addition.

Creating a LaunchAgent plist and scheduling it is not as hard as you might think, there is an excellent tutorial at afp548.com: http://www.afp548.com/article.php?story=20050620071558293
0
 
LVL 2

Expert Comment

by:JonyHolt
ID: 37710738
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.
0
 

Author Comment

by:anvendarnamn
ID: 37710759
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.
0
 
LVL 2

Expert Comment

by:JonyHolt
ID: 37710848
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.
0
 
LVL 6

Expert Comment

by:ishcabittle
ID: 37711429
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).
0
 
LVL 12

Accepted Solution

by:
nxnw earned 175 total points
ID: 37718455
I second the idea of making a launched plist. For this purpose, you would want a launchdaemon in the /library/launchdaemons/ folder.

There is a small app called lingon which makes this trivially easy. Launchd is a perfect fit for this kind of job.
0
 

Author Comment

by:anvendarnamn
ID: 37720147
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?
0
 
LVL 12

Expert Comment

by:nxnw
ID: 37720488
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.
0
 

Author Comment

by:anvendarnamn
ID: 37720606
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..
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:nxnw
ID: 37720723
Have you checked the "enabled" box?
0
 

Author Comment

by:anvendarnamn
ID: 37720763
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?
0
 

Author Comment

by:anvendarnamn
ID: 37720766
enabled is checked, yes
0
 
LVL 6

Expert Comment

by:ishcabittle
ID: 37721685
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.
0
 
LVL 12

Expert Comment

by:nxnw
ID: 37721852
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.
0
 
LVL 12

Expert Comment

by:nxnw
ID: 37722136
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.
0
 

Author Comment

by:anvendarnamn
ID: 37723977
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?
0
 

Author Closing Comment

by:anvendarnamn
ID: 37741184
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.
0
 
LVL 12

Expert Comment

by:nxnw
ID: 37742175
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.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Back to my mac / teamviewer 1 52
MAC Needs 2 Domains 2 45
Mac Online Backups 7 49
Do I Really Need to “Eject” USB Drives? 10 40
This article describes in detail how to set up the iPad in the Enterprise using iPCU aka iPhone Configuration Utility.  This could also be used for the iPhone although I have not detailed out any differences. Preparation as an iPad Administrator:…
Create a default user profile for Mac OS X 10.7/10.8 Create a user account on OS X that will be a template for every other user of that computer. I usually call it “profile” and make it an administrator account for the time being. 1. Install a…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now