LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 March 2 2016

hussam
Member

cron job to check for open/closed files

I have a folder /home/hussam/.cache/someapp/Tempfiles-*****.
where ***** is a random string.
The application I am using is creating those files. It tries to clean them up on exit but occasionally fails if they are too small.
I already contacted the application author but no reply.
lsof correctly reports which are open and which are not. it doesn't read files from previous sessions.
So I want to create a cron job that checks for /home/hussam/.cache/someapp/files-something files and then checks if they are not in use and if so, delete them.
I can do find /home/hussam/.cache/someapp/ -name Tempfiles-*
and
[[ ! $(lsof | grep a_file) ]] && /usr/bin/rm -rf a_file.

How do I combine it all in one line so it works as a cronjob?
Thank you.

Offline

#2 March 2 2016

rolf
Member

Re: cron job to check for open/closed files

Maybe you can create a bash script, and then add a call to that script in the crontab file (does that still exist?).
I'm guessing what you need is some kind of "foreach"? Apparently it's just called "for" in bash, and here is an example:

for fn in `ls`; do
    echo "file name: $fn"
done

This basically echoes for every item in the ls output. Of course you can replace echo by something more useful, and ls by your find command.
I know bits of all the needed stuff so let me know if I can maybe fill you in on a particular point.
You might need sed, awk or something like that.

Last edited by rolf (March 2 2016)

Offline

#3 March 2 2016

xterm
Moderator

Re: cron job to check for open/closed files

find takes and -exec argument.

Offline

#4 March 2 2016

Joe
Member

Re: cron job to check for open/closed files

The command

Here's what you're looking for:

for file in /home/hussam/.cache/someapp/Tempfiles-*; do [[ $(lsof | grep "$file") ]] || /usr/bin/rm "$file"; done

Don't use rm -rf. If there's an issue and it fails, you don't want it to follow through. Be defensive, not aggressive.

If you want to use find instead, use the -delete argument.

Cron

Adding this to your crontab is pretty straightforward. I generally recommend going through a file on disk. Something like this:

$ crontab -l > my_crontab
$ echo '0 21 * * * my_super_command' >> my_crontab  # this will execute my_super_command every day at 21:00.
$ crontab my_crontab

Every time you want to modify the crontab, modify the my_crontab file and update it like this.

Caution

Seriously though, be careful with these commands. I wouldn't feel comfortable having something that deletes files in my cron. If you want to be extra safe, tweak this command to have a --dry-run option that will simply list the files it'd delete. Run it for a week or two and once you're confident it's not doing anything wrong, turn the real delete on.

Offline

#5 March 2 2016

hussam
Member

Re: cron job to check for open/closed files

Joe wrote:
The command

Here's what you're looking for:

for file in /home/hussam/.cache/someapp/Tempfiles-*; do [[ $(lsof | grep "$file") ]] || /usr/bin/rm "$file"; done

Don't use rm -rf. If there's an issue and it fails, you don't want it to follow through. Be defensive, not aggressive.

Ok, thanks Joe :)
Yay, this did it. I replaced the f with v so the operation gets logged (and removed the r since those are files so no need for recursive).

Offline

#6 March 2 2016

Joe
Member

Re: cron job to check for open/closed files

Quick quesiton: why are you using /usr/bin/rm instead of /bin/rm?

Offline

#7 March 2 2016

hussam
Member

Re: cron job to check for open/closed files

/bin, /sbin, and /usr/sbin are symlinks to /usr/bin on my system.
/lib/ is a symlink to /usr/lib.
I don't remember why. I think it was changed like this in the early systemd days.

Edit: If I remember correctly, the plan was to consolidate everything the system needed to boot into /usr, move package metadata from /var/lib to /usr/something and make Linux bootable with empty /etc and /var. Systemd would populate them by default files if they are empty. It was Poettering's attempt at copying android's factory reset to Linux but I'm not sure how far along they got. Systemd is full of unfinished parts.

Last edited by hussam (March 2 2016)

Offline

Board footer