Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

can You use cron to delete a file 2

Status
Not open for further replies.

MAWarGod

Programmer
Feb 15, 2002
352
US
can You use cron to delete a file say every half hour?

MA WarGod

I believe if someone can think it, it can be programmed
 
Yes you can. However, I wouldnt just put in an "rm" command. I would write a small script that checks for the existance of the file and if found, delete it.

Cron entry would look something like this:

0,30 * * * * /some/path/to/script

and the (really simple, no error checking or anything) script would look something like this:

if [ -f /some/file ]; then rm /some/file; fi
 
Might want to touch the file afterwards, to create an empty file just in case anything relies on it being there...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
If you don't care whether the file already exists, you could just use:
[tt]
rm /file/path 2>/dev/null
[/tt]
This will suppress any error output.
 
Or even
Code:
cat /dev/null > /file/path
to truncate the file without deleting it...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
ok confussed I was a windows platfrom preson..

so I take this and put it in the from in cron or put in a file (script) and so how excute the file (script) from cron? again sorry new to this first time useing cron. to me cron seems like a bin or autoecex file that I used in windows, I may be wrong but trying to learn.

MA WarGod

I believe if someone can think it, it can be programmed
 
No, cron is like the Windows Scheduler, it runs commands at certain times.

If you just want to create a script that you can execute manually, all you need to do is create a plain text file containing the commands, and then make it executable using chmod u+x filename.

Then you type /full/path/to/filename to run it, or just filename if it is located somewhere in your PATH.

Annihilannic.
 
ok so its kinda like a bin file I am starting to understand so I can set the "cmd" or "CD' to run in the times I wish

see I have a room log file and say every 30 min's I wish it to be delete as per I don't wish all the data within the log to be loaded within the room (when some come in)
this will make the load times better and tranfer rate better I beleive.

thanks

MA WarGod

I believe if someone can think it, it can be programmed
 
What do you mean by "room"? Free disk space?

For that sort of thing you should probably use logrotate, which is designed for managing logs like this.

If you just create a file /etc/logrotate.d/mylog containing a rule like:

[tt]/path/to/mylog {
rotate 7
daily
}[/tt]

It will rotate the log file once a day, keeping 7 days worth. The previous day's log will be called /path/to/mylog.1, the day before that /path/to/mylog.2, etc.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top