Page 1 of 1

Cron job question

Posted: Sun Apr 18, 2010 11:27 am
by Bill H
My hosting control panel has a Cron job utility that lets me create them using a thing where I specify and time interval and a command line.
Problem is I'm not a Unix guy at all and I know squat about what the "command" should be, so...

There is a thing on my site where a temp file is created in a subdirectory, a special one created for that purpose. When the user exits he is supposed to do so by clicking on an "exit" button which deletes the temp file and redirects but, of course, many just use their browser "back" button. No way am I going to disable the "back" button, so that directory keeps getting cluttered up with temp files and I have to go in and delete them. So I'm thinking in terms of a cron job to delete than once a month or so. But I don't know what unix is for "delete all files." The effect is:

Code: Select all

delete "/clients/charter/*.*"

Re: Cron job question

Posted: Sun Apr 18, 2010 12:37 pm
by s.dot
You must be very careful when deleting files.

I would use a php script and delete using unlink() instead of executing a command line command like rm -r

I would do..

Code: Select all

<?php

if ($dh = opendir('/your/directory/here'))
{
	while (($file = readdir($dh)) !== false)
	{
		if ($file != '.' && $file != '..')
		{
			unlink($dir . $file);
			
			//comment this out if you like
			echo 'Deleted file: ' . $dir . $file . '<br />' . "\n";
		}
	}
}

closedir($dh);
For this to work, this directory must be writable. (chmod)

Then you would put this script's location into the cron command

Re: Cron job question

Posted: Sun Apr 18, 2010 12:41 pm
by Eran
Why would you use a php script instead of a bash script or a simple command for this purpose?

Re: Cron job question

Posted: Sun Apr 18, 2010 1:50 pm
by s.dot
The OP isn't very familiar with unix commands.
I didn't feel advising rm -r or rmdir would be appropriate considering it could do lots of damage if not used correctly.

Re: Cron job question

Posted: Sun Apr 18, 2010 2:03 pm
by Eran
Those are really simple commands to understand. Using someone else's delete script is definitely not safer than understanding and applying simple shell commands. Use the right tool for the job, I would say

Re: Cron job question

Posted: Sun Apr 18, 2010 2:22 pm
by Benjamin
This is perfectly safe:

Code: Select all

system("/bin/rm -fr /path/to/files/*", $output);
Or simply something like:

Code: Select all

/bin/rm -fr /path/to/files/* > /dev/null
NOTE: The absolute path to rm is used because some servers are configured to ignore the -f argument and add the -i argument causing rm to prompt for every file. Using the full path bypasses this.

Re: Cron job question

Posted: Sun Apr 18, 2010 2:46 pm
by Bill H
Thanks folks, that gives me what I need.

Re: Cron job question

Posted: Tue Apr 20, 2010 6:01 am
by josh
pytrin wrote:Why would you use a php script instead of a bash script or a simple command for this purpose?
Portability (run in safemode? Or different OS?), readability (Windows programmers have to work on the code base), etc.
Benjamin wrote:This is perfectly safe
Not if someone plays an evil trick on you with symlinks/hardlinks :twisted:

On a side note, why not just store your temp file in the session. PHP already has a way to garbage collection session data. That's what that mechanism is for.