Periodic cleanup of folder through Crontab with tmpwatch
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Periodic cleanup of folder through Crontab with tmpwatch
Hi to everyone. This is my first post and literally my first time working on Linux.
I'm a PC technition that has to learn server administration. A+ and N+ don't cover this... lol
Anyway, I was asked to clean any file or folders in the /tmp folder that are older than 5 day's. I created a script (after hours of research...) that looks like this:
find /tmp* -mtime +5 -exec rm -rf {} \;
It works when I type it in the command view (note: I do all the work from SSH) but when I try and run it through crontab nothing happens. I tried changing the script to look like:
tmpwatch -m 120 /tmp
I added a line to crontab to make it run at 17:00 (* 17 * * * /usr/local/bin/tmpclean.sh) and after thta didn't work I tried copying the .sh into /etc/cron.d daily directory.
Yet to no avail. So now I need help. Like I said, I'm an uber nood who never touched Linux, because I didn't know how it worked and was pamperd too much by some other os, but I would really appreciate it if someone could help and also explain things as we go along.
First thing to consider: since you are new to Linux, most (if not all) questions you have may have already been asked and answered.
You first resource for information is www.google.com/linux for Linux related issues. That link filters out non-linux returns.
Then, please notice that there is a search function here that allows you to search these boards for posts relating to issues you may be having.
So, point you favorite browser at the link given above, and enter these search words: crontab ssh. The answer is in one of the returns on the first page of returns.
PS: If I were a manager who had a technition working for me who doesn't know how to spell techincian, or who doesn't use spellcheck, I'd have fuel for a performance review.
I have looked through your forums (almost) all day and did find some things that were related, however none of it helped. This is the reason why I said I tried all the way's that I listed when I posted.
Also thanks for the google linux thing. I didn't know that one. I posted here because I see that the forums were more active here than on other forums.
I have tried allot of ways that were replied to the same type of question I have, but none work and they mostly don't end with "Ok that worked, thanks".
As to your comment about my spelling. I try my hardest to type English because I'm actually Afrikaans. I do however speak better than I type. It could also have been a slip of the fingers like...
Quote:
So, point you favorite browser
but still, if there is anyone who can help I'd appreciate it.
It works when I type it in the command view (note: I do all the work from SSH) but when I try and run it through crontab nothing happens. I tried changing the script to look like:
Do I assume correctly that the script works from the command line if you are logged in to the system via SSH? But it doesn't work via crontab?
If so, it may be an authentication problem. Crontab has to login to the system on which you wish to run the script.
Here is a link the a post which addresses the issue of running crontab via SSH, with authentication, non-interactively. See #3.
The most common problem with cron is the $PATH - it differs between cron and a login
shell. Try to use the fully qualified path to find, and make that a "find /tmp "
rather than /tmp* ... chances are that there's no globbing happening, and the /tmp
will definitely cover the task at hand.
As per tinkster, always use full path for any cmd called from within cron, so that'd be:
/usr/bin/find
/bin/rm
also as he said, be careful with file paths as well.
If I wanna use the "find" command in the crontab I must give the path to the "find" file within the folder "/usr/bin/". It won't work with a script with the "find" command in that?
Like if my script only reads
find /tmp -mtime +5 -exec rm -rf {} \;
then it won't work, but
/usr/bin/find /tmp -mtime +5 -exec rm -rf {} \;
because it goes to the find command, right? I just want to know if I'm understanding this correctly.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.