Need to email then clear file using cron?

Hi,
just wondering if there is a way to email then clear file /var/log/phpmail.log using PHP and cron?

Thanks in advance for your feedback.

In my php.ini file I have below
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; log all php mail scripts to check for spam ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mail.log = /var/log/phpmail.log
sabecsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gerwin Jansen, EE MVETopic Advisor Commented:
Hi sabecs,

You can read this article on how to write a crontab entry: How to setup a crontab job in Linux / Unix

Do you already have a script that is sending the email and doing the cleanup? If so then you can add that script to the crontab. If you don't have a script yet, how can we help you with that?
0
savoneCommented:
I don't know how to send the email with php, but you can do it with bash.

Send the file to yourself:
mail -s "LOG FILE" me@domain.com < /var/log/phpmail.log

Clear the log file:
cat /dev/null > /var/log/phpmail.log
0
sabecsAuthor Commented:
Thanks for your feedback, much appreciated.
I don't have a script yet. Just wondering on where I would need to set it up?
Can I run from a users cPanel account, would there be access issues or do I setup in WHM.
I do have root access to my VPS?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

savoneCommented:
I don't know if you have root access. You will have to ask your hosting company. You will need root to access the logs in any way.
0
sabecsAuthor Commented:
Thanks, I have root access.
0
sabecsAuthor Commented:
Hi, Savone's suggestion 'mail -s "LOG FILE" me@domain.com < /var/log/phpmail.log' works great from the command line , but how can I automate to run once a weekly?
0
savoneCommented:
If you want it to run once a week, put it in a cron job.

type the following command to edit crontab:

crontab -e

It will open in your default editor, usually vi.  If you don't know how to use vi, let me know.

Add the following line to the file:

* * * * 0 mail -s "LOG FILE" me@domain.com < /var/log/phpmail.log


This will run the job once a week on Sunday.
0
sabecsAuthor Commented:
Thanks Savone, much appreciated.
I have downloaded the file root from /var/spool/cron, can I just edit it with editplus or notepad and then uploaded the file instead of using vi?

Also can I append cat /dev/null > /var/log/phpmail.log to existing command to clear file after its emailed like below?

* * * * 0 mail -s "LOG FILE" me@domain.com < /var/log/phpmail.log && cat /dev/null > /var/log/phpmail.log
 

0 6 * * * /usr/local/cpanel/scripts/exim_tidydb > /dev/null 2>&1
30 5 * * * /usr/local/cpanel/scripts/optimize_eximstats > /dev/null 2>&1
2,58 * * * * /usr/local/bandmin/bandmin
0 0 * * * /usr/local/bandmin/ipaddrmap
41 21 * * * /usr/local/cpanel/scripts/upcp --cron
0 1 * * * /usr/local/cpanel/scripts/cpbackup
0 2 * * * /usr/local/cpanel/bin/backup
35 * * * * /usr/bin/test -x /usr/local/cpanel/bin/tail-check && /usr/local/cpanel/bin/tail-check
45 */4 * * * /usr/bin/test -x /usr/local/cpanel/scripts/update_mailman_cache && /usr/local/cpanel/scripts/update_mailman_cache
30 */4 * * * /usr/bin/test -x /usr/local/cpanel/scripts/update_db_cache && /usr/local/cpanel/scripts/update_db_cache
45 */8 * * * /usr/bin/test -x /usr/local/cpanel/bin/optimizefs && /usr/local/cpanel/bin/optimizefs
30 */2 * * * /usr/local/cpanel/bin/mysqluserstore >/dev/null 2>&1
15 */2 * * * /usr/local/cpanel/bin/dbindex >/dev/null 2>&1
15 */6 * * * /usr/local/cpanel/scripts/autorepair recoverymgmt >/dev/null 2>&1
*/5 * * * * /usr/local/cpanel/bin/dcpumon >/dev/null 2>&1
13 21 * * * /usr/local/cpanel/whostmgr/docroot/cgi/cpaddons_report.pl --notify
10,25,40,55 * * * * /usr/local/cpanel/whostmgr/bin/dnsqueue > /dev/null 2>&1

Open in new window

0
Marco GasiFreelancerCommented:
About sending email with php it's quite simple. Just use the mail() function.
Here you have an example:

		$name = "John Foo";
		$email = "email@email.com";
		$message = "Hello world";
		$to = 'dest@email.com';
		$subject = 'A new message';
		$headers = "From: " . strip_tags($email) . "\r\n";
		$headers .= "Reply-To: ". strip_tags($email) . "\r\n";
		$headers .= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
		if (mail($to, $subject, $message, $headers))
		{
			echo 'Message sent';
		}
		else
		{
			echo 'Ooops!';
		}

Open in new window

0
savoneCommented:
Yes, you can do both, so basically add the following:

* * * * 0 mail -s "LOG FILE" me@domain.com < /var/log/phpmail.log; cat /dev/null >> /var/log/phpmail.log
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marco GasiFreelancerCommented:
@sabecs: was this question even about how to sen an email with php or was it just about to use cron to delete a log file? I'm missing something...
0
sabecsAuthor Commented:
Hi Marco, I didn't know that you could send the email with just a cron job only, so no need complicate things by adding php as well.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.