Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to setup cron-job on Amazon EC2

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.

like image 275
user1491022 Avatar asked Sep 13 '13 09:09

user1491022


People also ask

Why cron job is not working?

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.

How to set up a cron job in AWS EC2?

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 ).

How do I set up a cron job in Elastic Beanstalk?

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.

How to check if cron jobs is saved or not?

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.

How do I schedule a cron job on Amazon RDS or Aurora?

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.


1 Answers

It's just the same as setting up a cron job on any other server via command line.

  1. Connect via SSH
  2. Navigate to /etc/cron.daily
  3. Make a new script that runs / calls a PHP script to send the email/other tasks
  4. Make sure its executable

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

Step 1: Create a bash script in the cron.daily folder

Connect 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.

Step 2: Bash script contents

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.

Step 3: Make it executable

We need to allow the file to be executed, type sudo chmod +x mailscript.

Step 4: Try it out

To test it out type ./mailscript to run the cron in cron.daily. Now check your emails!

like image 162
SamV Avatar answered Oct 05 '22 15:10

SamV