1833883 Members
1794 Online
110063 Solutions
New Discussion

Erase file

 
SOLVED
Go to solution
peterchu
Super Advisor

Erase file

I have a file that always generated in the system eg. /tmp/log.txt , it is generated by the application program , but this file should not be present in the system otherwise there are some program problem , I want to erase this file once the program has generate it , as I know , it can link to /dev/null , could suggest how to make it ? thx
16 REPLIES 16
twang
Honored Contributor
Solution

Re: Erase file

make a symbolic link on /tmp as below,
# ln -s /dev/null /tmp/log.txt
Mobeen_1
Esteemed Contributor

Re: Erase file

Peter,
I would suggest you look at accomplishing this in either of the 2 ways listed below

1. This could be taken care in your
application program itself. If your
application is a script, right at the end
make sure that if the file /tmp/log.txt
is deleted

2. Else you submit a job in the cron with
$ rm /tmp/log.txt
and submit this cron job to be run every
10 mins or so, depending on the frequency
with which this log is created

regards
Mobeen
Mobeen_1
Esteemed Contributor

Re: Erase file

Peter,
I am sorry i did not see your last part of the question about links....i think you could go ahead with the suggestion prior to my post...you would be fine...

rgds
Mobeen
Biswajit Tripathy
Honored Contributor

Re: Erase file

If you have access to the application source code,
then you could call unlink() inside the application
code (I'm assuming that application source is in C)
to delete the file. If you don't have access to the
source code, then you could use a small shell
script like the following:

--- Script start
./application_name
rm -rf /tmp/log.txt
---- End script

- Biswajit
:-)
peterchu
Super Advisor

Re: Erase file

thx reply ,

I use the s.link function , it is OK to link the file , but the /tmp/log.txt is exist , even the file can't be update but I really don't want the file exist in the system , how can I make the file don't appear in the system ( even the file size is 0 ) ? thx


/tmp/log.txt -- > /dev/null
twang
Honored Contributor

Re: Erase file

I don't think that you can hide the file.
Peter Godron
Honored Contributor

Re: Erase file

Hi,
I can't see a way of prventing the application from creating the file, unless you change the permissions on /tmp to exclude the application owner from creating files via a Acccess Control List(man acl).

Either change the application or the code that causes the problem if the /tmp/log.txt file exists.
H.Merijn Brand (procura
Honored Contributor

Re: Erase file

Then you can also be very wicked and binary edit the application and find the spot where "/tmp/log.txt" is opened and replace the string with "/dev/null\0\0\0"

But that is for the brave of heart only :)

(Men would I laugh when the application maintainers come in and don't understand why there is no log file written)

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Michael Schulte zur Sur
Honored Contributor

Re: Erase file

Hi,

how often is this file created? Just once when the application is started? You may write a script which checks the existence and deletes the files.

greetings,

Michael
Geoff Wild
Honored Contributor

Re: Erase file

Why can't it exist?

Anyways, in the startup script for the Application, add this to the end:

rm -f /tmp/log.txt

If it keeps re-creating it, how about a cron job that deletes every minute - if it exists:

* * * * * [ -f /tmp/log.txt ] && /usr/bin/rm -f /tmp/log.txt >/dev/null 2>&1


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
peterchu
Super Advisor

Re: Erase file

thx suggestions , if can't hide the file , I have to write a script to erase it by crontab job , but as I know , the crontab job only run the file every minute , how can I run the script every second ? is it possible ? thx
Biswajit Tripathy
Honored Contributor

Re: Erase file

How about starting your application from a script
like the following:

---- Start ---
./application &
while [ /bin/true ]
do
ps -ef | grep -v grep | grep -q application
if [ $? -eq 0 ]
then
rm -rf /tmp/log.txt
sleep 1
else
exit 0
fi
done
---- End ----

This script will start your application in background
and delete /tmp/log.txt every second. Once
your application is done and leaves, the script
will exit.

To tell you the truth, I would use the above
technique as a last option and would consider
re-designing the whole thing if I can.

- Biswajit
:-)
Geoff Wild
Honored Contributor

Re: Erase file

cron limitation is 1 minute...

to run every second, you would have to call a script every minute, then in theat script, loop every second and issue your command. You have to be carefull - because your command might not finish in 1 second - and the next one kicks off...

sample code (not tested):

limit=60
while [ $limit -gt 0 ]; do
if [ -f /tmp/log.txt ] ; then
rm /tmp/log.txt
else
limit=0
fi
limit=`expr $limit - 1`
done

Rgds...Geoff

Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Paul Cross_1
Respected Contributor

Re: Erase file

It would be interesting to understand why you would want to do this. Does the log file get too big? Is the partition too small? Why every second?

If you have access to the source code, don't bother unlinking the file, just remove the logging functionality, or change your $LOGFILE var to /dev/null.

NB: If you decide to binary edit a file, make a backup first!!
Stephen Keane
Honored Contributor

Re: Erase file

You could always do the following, though I'm not sure what the application would do in response.

create a DIRECTORY called /tmp/log.txt with permissions d---------. Thus the application would be prevented from creating a file with the same name. Of course you might not want a directory called /tmp/log.txt hanging around either!
Gordon  Morrison
Trusted Contributor

Re: Erase file

I would be very wary of deleting a file while it is open for writing. rm just unlinks the file from the inode, so it won't appear in an ls listing, but the process that has that file open doesn't know it's been deleted, so it just keep writing to it... you then find your disk space disappearing down a black hole, and you won't get it back until all the processes that have that file open terminate.

I think by far the best idea is:
ln -s /dev/null /tmp/log.txt
What does this button do?