Reputation: 171
I have an Amazon EC2 instance (Ubuntu Server 13.04 - 64 bit [ami-bf1d8a8f]) running my website. I need to setup a Cron Job to get an email alert everyday.
Does anyone have any advise or possible solutions?
Thanks for your time.
Upvotes: 5
Views: 5632
Reputation: 7588
It's just the same as setting up a cron job on any other server via command line.
/etc/cron.daily
You can use a command such as wget -q -O temp.txt http://www.site.com/cron.php
to call the PHP script, or via command line php /var/www/site/cron.php
.
Regarding the wget
method, temp.txt
will contain the output of the script, or you can redirect the output to /dev/null
. This will just discard all output. If you need to save the output of the cron script and you decided to go with the command line method then you can output the data to a file php /var/www/site/cron.php > output.txt
If you need more explanation/detail post a comment. Also take a look at Basic cron job set up to execute php file daily
cron.daily
folderConnect to the instance via SSH, navigate to the daily cron folder with cd /etc/cron.daily
. Next type sudo nano mailscript
, notice there is no .sh
bash extension. I had a problem with the extension which caused the script to not run.
Enter this into the command line text editor nano
#!/bin/bash
php /var/www/mail-script.php > /var/www/mail-script-log.txt
Now save the text file and exit, to save use CTRL + O
and then press enter, to exit press CTRL + X
.
We need to allow the file to be executed, type sudo chmod +x mailscript
.
To test it out type ./mailscript
to run the cron in cron.daily
. Now check your emails!
Upvotes: 8