MyBB Community Forums

Full Version: Cronjob to mail most recent files inside a directory?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I hope someone here can help me with setting up a cronjob on a linux hosting account (with cPanel). I don't even know if it's possible, but who know's. What I'd like to do is create a cronjob which checks a certain directory 1 time a day on my server (in this case the MyBB backup directory), and emails the last modified file to a specified email address.

Anyone knows if this possible, and if so, how to do it?
You would probably need to write a PHP script which performs the task then get cron to run the php script once a day. I have played with cron and failed :p But i am sure it is simple once you get the hang of it.
ignore this \/
I'm not sure how cpanel arranges its crontab interface but if you can figure it out add this:
0 0 * * * find <full path> -type d ! -name '<directory name>' -prune -o -type f -mtime -1 -print

replace <full path> with the path of the directory to search. eg: /home/www/distfiles/
replace <directory name> with the name of the directory to search. eg: distfiles

cron usually emails you with the results so ive not included an email command. This should run at midnight (depending on the system clock) everyday and send you a list of files created in the last 24 hours.
Info cunningly stolen from http://www.unix.com/shell-programming-sc...mmand.html



I misread your post. the following should send the latest file in <dir> to <email>

0 0 * * * echo "Server Backup" | mutt -s Backup -a <dir>$(ls -1rt --color=never <dir> | tail -1) <email>

<dir> must have a trailing slash, eg: /home/user/backups/

The only problem is the default command line program for sending emails does not support attachments, so that command won't work if you don't have another program called "mutt" installed on your server.
Nice!

Ok, so I need to check with my host if they have Mutt running, otherwise the cronjob won't work right?
Not quite. It needs to be installed on the system. It won't run until you execute the command but yes, you need to make sure mutt is installed.
Ok, I checked with them and mutt is installed on the server but I also got this reply (I removed some info because I didn't know if it was sensitive):

Quote:Normally we see people use 'mail' and this might be recommened since mutt isnt fully supported.

As a courtsey, i have enabled jailed SSH shell so you / the person helping you can go through some of the syntax arguments for mail in the cmdline (shell)

When you connect you will need to specify port 2222

ex:

[manne@removed ~]$ whoami
manne
[manne@removed ~]$
Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...
[-- sendmail-options ...]
mail [-iInNv] -f [name]
mail [-iInNv] [-u user]
[manne@removed ~]$

Alternately you could issue the command 'man mail' and it will give you the syntax of various commands available to you.

I have no idea what he's talking about though Huh Should I just try your approach posted above? Or does it make sense what the support just emailed me?
He's suggesting you use the more commonly used mail command. He's given you more direct access to the server so you can try out the command before making it a cron job but if you've never used linux before you'll find it hard to find what you're looking for. You've got the following options;
  • Go ahead with the cron job specified above. It should work. I've tried it out on my system. If your system is different and for some reason the command doesn't work then it shouldn't be a problem fixing it.
  • PM me the details the host gave you, I can log into your server and make sure everything will work. I can also try sending a file using the mail command, though that didn't work on my system. Then you can do the cron job certain it will work.
  • You can log into the server (you'll need another program on your desktop to do it) and see if the command works. and then add it as a cron job.
Thanks for you explanation! Smile

I added the cronjob like you said

Quote:0 0 * * * echo "Server Backup" | mutt -s Backup -a /home/manne/public_html/forum/admin/backups/$(ls -1rt --color=never /home/manne/public_html/forum/admin/backups/ | tail -1) [email protected]

I'm excited, I hope all goes well!
Nice, it works like a charm so far. Last midnight the latest backup file has been emailed to me (well, midnight of the server, which is 7 hours off, but that's not a problem).

Thanks for your help, I've been looking for something like this for quite some time Smile