Cron job question

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Cron job question

Post 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/*.*"
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Cron job question

Post 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
Last edited by Benjamin on Sun Apr 18, 2010 2:24 pm, edited 2 times in total.
Reason: Please use [syntax=php]...[/syntax] tags. [code] tags have been depreciated.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Cron job question

Post by Eran »

Why would you use a php script instead of a bash script or a simple command for this purpose?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Cron job question

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Cron job question

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Cron job question

Post 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.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Re: Cron job question

Post by Bill H »

Thanks folks, that gives me what I need.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Cron job question

Post 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.
Post Reply