LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-13-2006, 09:57 AM   #1
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Rep: Reputation: 30
How do I make a cron job run "every two weeks"?


Greetings all.

I'm looking to make a cron job that will purge the contents of a handful of folders the first and fifteenth of each month -

Basically my folder setup is:

/etc/stuff/1
/etc/stuff/2
/etc/stuff/3
/etc/stuff/4
/etc/stuff/5

I want to keep the 1/2/3/4/5 folders, but any files/subfolders under them I want to delete automatically every other Tuesday. I think the basic bash would just be something like

cd /etc/stuff/1
rm -rf *
cd /etc/stuff 2
rm -rf *

(etc)

But I'm not sure if *.* is the proper syntax, and very much unsure how to set it to run on those days.

Thanks very much in advance.

Last edited by Seventh; 01-13-2006 at 10:12 AM.
 
Old 01-13-2006, 10:12 AM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
To see how to setup your cron, run "man -S5 crontab" to see the syntax options.

For your remove commands:
Code:
cd /etc/stuff/1 
rm -rf *.*
...this can be dangerous. What if your cd command failed? Like if the directory had inadvertantly been removed prior to cron running this command? You would recursively remove stuff that you didn't intend to depending on what your old working directory was. Could be disasterous. Better to fully specify the target removal to the command, like this::
Code:
rm -rf /directory/subdirectory
 
Old 01-13-2006, 10:47 AM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Be careful with the rm -rf , you may want to test it by using ls first.
Code:
#!/bin/bash

for i in $(seq 1 4) ; do

find /etc/stuff/$i/* -exec rm -rf {} \; 2> /dev/null

done

minute hour day month dayofweek /full/path/to/script

an asterisk (*) can be used to specify all valid values.

dayofweek any integer from 0 to 7, where 0 or 7 represents Sunday

For example: run a script the first day of every month at 4:10AM
10 4 1 * * /root/scripts/backup.sh

Code:
This one should work for you...

For example: run a script every other tuesday at 4:10AM
10 4 1 * 2/2 /path/to/someplace/myscript.sh
 
Old 01-13-2006, 11:06 AM   #4
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Code:
rm -rf /directory/subdirectory
One other tip related to what I said above. If you expect something to be a directory, not a file, it's even better to explicitly say so on the command line. Add a trailing slash to the target remove path to force it to be interpreted as a directory. Like this:
Code:
rm -rf /directory/subdirectory/
 
Old 01-13-2006, 11:08 AM   #5
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
Great advice so quick - thank you very much guys, I really appreciate it.

My folders aren't actually named 1/2/3/4/5, they're user initials (I probably should have been clearer).


Would the following work, where aa, bb, cc = the user's initials?

Code:
#!/bin/bash

find /etc/stuff/aa/* -exec rm -rf {} \; 2> /dev/null
find /etc/stuff/bb/* -exec rm -rf {} \; 2> /dev/null
find /etc/stuff/cc/* -exec rm -rf {} \; 2> /dev/null
find /etc/stuff/dd/* -exec rm -rf {} \; 2> /dev/null
Or is there another part of that loop I'd need to add?

Also, if I just wanted to do it monthly, simply dropping this script into cron.monthly as something like cleanup.sh would do the trick, correct?

Trying to take baby steps so I don't inadvertantly delete anything. Thanks again1
 
Old 01-13-2006, 11:11 AM   #6
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by haertig
Code:
rm -rf /directory/subdirectory
One other tip related to what I said above. If you expect something to be a directory, not a file, it's even better to explicitly say so on the command line. Add a trailing slash to the target remove path to force it to be interpreted as a directory. Like this:
Code:
rm -rf /directory/subdirectory/
In this case my exact path is /etc/stuff/cq. I need to maintain the /cq/ folder itself, just delete all files and folders in it. Would I be better off doing:

rm -rf /etc/stuff/cq/*

Or am I way off base and that isn't even close?

(I've always said that it's fine to ask silly questions as long as you only ask 'em once, I hope you guys don't mind me being such a novice here).
 
Old 01-13-2006, 11:13 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
Or simply use:
rm -rf /etc/stuff/[12345]/*
or something like that.

homey, you're close, but I think you can't use the "/2" syntax when you're not specifying a range (multiple
values).
And "2" is just 1 value, not a range. Think of "a-b/2" as "from a to b in steps of 2".
So, using "2/2" as day of the week would probably run every Tuesday or not at all.

Using a combination of day of month and day of week doesn't work either, since there is the job runs when either of the two entries is matched ("or" relationship, not "and"). At least, that's so for my Vixie cron.
Other implementations of cron (ATT cron, BSD cron) may work differently.
Look carefully at the man pages, is my advice...

One alternative, would be to run the script every Tuesday, but to make it check (via a toggle) if it should
be run or not.
So: first Tuesday => script runs and sets the toggle (ie via a file) so that it knows that it shouldn't run
on next Tuesday.
2nd Tuesday => script runs again, but sees the toggle, clears it, and stops immediately after, without
performing the "rm -rf" task.
3rd Tuesday, no toggle, so same as first Tuesday...
Running a script every Tuesday is easy:
0 2 * * 2 path/to/your_script

I believe the easiest way of implementing a toggle like that is to store a file, ie in /var/run.
You'll need to have write permission on the directory where the toggle file is stored.
The script can then simply check (in Bash):
if [[ -f /var/run/toggle_file ]]; then
#toggle exists
rm -f /var/run/toggle_file
exit;
else
touch /var/run/toggle_file #stores the empty toggle file
#now do the clean up commands
exit;
 
Old 01-13-2006, 11:38 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
If you have a lot of names, it may be good to put them in /home/names.txt ...
Code:
#!/bin/bash

names=/home/names.txt
for i in $(cat $names); do

find /home/etc/stuff/$i/* -exec rm -rf {} \; 2> /dev/null

done
If you have a few names, this may work...
Code:
#!/bin/bash

name="aa bb cc dd"
for i in $name ; do

find /home/etc/stuff/$i/* -exec rm -rf {} \; 2> /dev/null

done
 
Old 01-13-2006, 11:42 AM   #9
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
You guys are awesome, thank you.


Code:
#!/bin/bash

name="aa bb cc dd"
for i in $name ; do

find /home/etc/stuff/$i/* -exec rm -rf {} \; 2> /dev/null

done
So that, saved as cleanup.sh cron.monthly, should monthly purge everything, right?
 
Old 01-13-2006, 11:51 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Don't forget to chmod +x myscript.sh

I put a line in /etc/crontab but you can put the script into the /etc/cron.monthly if you prefer.

For example: run a script every other tuesday at 4:10AM
10 4 * * 2/2 /path/to/someplace/myscript.sh

Last edited by homey; 01-13-2006 at 12:03 PM.
 
Old 01-13-2006, 11:54 AM   #11
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 2,131

Rep: Reputation: 333Reputation: 333Reputation: 333Reputation: 333
Quote:
Originally Posted by Seventh
rm -rf /etc/stuff/cq/*
This will work. If you also have hidden files in these directories (hidden files begin with a dot), you should add a command to get those explicitly, e.g.:
Code:
rm -rf /etc/stuff/cq/*
rm -rf /etc/stuff/cg/.*
An standalone * wildcard will not match a filename that begins with a dot.
 
Old 01-13-2006, 11:59 AM   #12
Seventh
Member
 
Registered: Dec 2003
Location: Boston, MA
Distribution: Redhat / Debian
Posts: 269

Original Poster
Rep: Reputation: 30
Thank you very much everyone.
 
Old 01-13-2006, 12:55 PM   #13
merchtemeagle
Member
 
Registered: Oct 2004
Location: Belgium
Distribution: Slackware 13.37
Posts: 512

Rep: Reputation: 31
Quote:
Originally Posted by haertig
To see how to setup your cron, run "man -S5 crontab" to see the syntax options.

For your remove commands:
Code:
cd /etc/stuff/1 
rm -rf *.*
...this can be dangerous. What if your cd command failed? Like if the directory had inadvertantly been removed prior to cron running this command? You would recursively remove stuff that you didn't intend to depending on what your old working directory was. Could be disasterous. Better to fully specify the target removal to the command, like this::
Code:
rm -rf /directory/subdirectory
Or:

Code:
cd /etc/stuff/1 && rm -rf *
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple "echo > qbc" cron jobs doesn't run shole Linux - Newbie 3 09-23-2006 03:09 PM
info to run "cu" command from cron tcore Mandriva 4 03-12-2005 07:03 AM
How can I run the "cu" command under cron? tcore Linux - General 1 03-08-2005 08:45 AM
"permission denied" error when cron job executes jillu Linux - Newbie 11 11-02-2004 02:19 PM
system("top") in a C program giving problems when the C prg is run by cron rags2k Programming 1 09-02-2004 04:25 PM


All times are GMT -5. The time now is 10:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration