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.
Why is crontab not working in your system? Crontab might fail for a variety of reasons: The first reason is that your cron daemon might not be working for any reason, resulting in your crontab failing. There also exists a possibility that your system's environment variables are not settled correctly.
Cron is a time-based job scheduler run on the server in the Unix-like operating system. To perform such action we write cron expression made of five fields, followed by a shell command to execute. First, we have to log in to AWS EC2 instance using CLI (Command Line Interface). then select any editor from the list (preferred nano ).
You can use Elastic Beanstalk configuration files, called .ebextensions, to create cron jobs that run on all Amazon EC2 instances in an Elastic Beanstalk environment. In the Elastic Beanstalk application zip file, create a directory named .ebextensions.
To check whether your Cron Jobs is saved or not, run the below command. Now you can see your Cron Job file which has all your Cron Jobs. For testing, you can schedule a task for a minute and check. For any Mobile app development services contact Cumulations.
When you migrate your databases to Amazon Relational Database Service (Amazon RDS) or Amazon Aurora, you lose the ability to log in to the host and schedule cron jobs. You can use services such as Amazon Elastic Compute Cloud (Amazon EC2), AWS Lambda, AWS Batch, Amazon CloudWatch, and Amazon EventBridge to schedule the maintenance tasks.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With