LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-02-2011, 11:33 AM   #1
sir-lancealot
Member
 
Registered: Aug 2007
Posts: 336

Rep: Reputation: 31
bash simple cleanup script then curl


Not a programmer, but did manage to get some good rsync scripts, some scp things, each is wrapped around an if then where if things work, I curl a webpage which will ack the process.

I now want a cleanup script on the server that cleans x amount of day's and upon completion of all the cleanups, simply do the same, but a little lost in the tree, and I am sure it's something simple.

So I am using the following example;
Code:
if find /backup/server/folder1 -type f -mtime +3;
 then find /backup/server/folder1 -type f -mtime +3 -exec rm -rf {};
elseif find /backup/server/folder2 -type f -mtime +3
 then find /backup/server/folder2 -type f -mtime +3 -exec rm -rf {};
curl http://intserver.com/check.php?pid=111
fi
The server already checks to see if anything is late, so I don't need a check here for that, just want to say cleanup folder1, folder2 if no errors, run the curl script or just drop out.

Thanks
*note: I'm sure there is a way to use the results from line 1 in the delete line rather than a re-find. Things are small and quick, but if there is way to say $1 rm -rf, etc. feel free to throw that out as well.*

Last edited by sir-lancealot; 02-02-2011 at 11:34 AM.
 
Old 02-02-2011, 12:59 PM   #2
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,189

Rep: Reputation: 105Reputation: 105
Not sure what your objective is or whether the logic of your "if" is what you want.

What you are doing: If there is anything in folder1 that was last modified at least 3 days ago, then remove it and drop out of that whole segment of script you are showing. If (and only if) nothing was found in folder1, then -> if there is anything in folder2 that was last modified at least 3 days ago, then remove it, do the curl command, and drop out of that whole segment of script you are showing. If nothing is found in either folder, do nothing.

If your intention is to remove things older than three days from both folders, then getting rid of the whole "if" structure would simplify things. Is the curl just to say that you did it? If so, then the following would work.

Code:
find /backup/server/folder1 -type f -mtime +3 -exec rm -rf {} \;
find /backup/server/folder2 -type f -mtime +3 -exec rm -rf {} \;
curl http://intserver.com/check.php?pid=111
You could even change those two finds to one line

Code:
find /backup/server/folder1 /backup/server/folder2 -type f -mtime +3 -exec rm -rf {} \;
Unfortunately, if you want to know whether anything was found, it gets more complicated. The linux man page for find indicates the following about the exit status:

Code:
find exits with status 0 if all files are processed successfully, greater than 0 if 
errors occur. This is deliberately a very broad description, but if the return value 
is non-zero, you should not rely on the correctness of the results of find.
Playing around with some test cases indicates that your "if" structure will always execute the first case and never check folder2 or do the curl, unless, of course, your script encounters an error, such as access denied to folder1.

Last edited by choogendyk; 02-02-2011 at 01:01 PM.
 
  


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 bash script help zok Linux - General 4 12-15-2006 11:56 AM
Simple bash script lp449 Linux - Networking 3 08-02-2005 04:25 PM
Simple bash script Soulful93 Programming 1 06-04-2005 07:26 PM
Simple Bash Script Help njdownes Programming 2 03-05-2005 07:35 AM
Help with simple bash script - please tw001_tw Linux - Software 5 12-19-2004 09:02 PM


All times are GMT -5. The time now is 01:43 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration