How to Setup Cron Job on Amazon Linux Ami

Is it possible to have cron job running on Amazon EC2 instance?

Yes, cron should just run on Amazon EC2 instances.
Did you look at the answers to this question ?
Cannot get cron to work on Amazon EC2?

Cannot get cron to work on Amazon EC2?

Cron can be run in Amazon-based linux server just like in any other linux server.

  1. Login to console with SSH.
  2. Run crontab -e on the command line.
  3. You are now inside a vi editor of the crontab of the current user (which is by default the console user, with root permissions)
  4. To test cron, add the following line: * * * * * /usr/bin/uptime > /tmp/uptime
  5. Now save the file and exit vi (press Esc and enter :wq).
  6. After a minute or two, check that the uptime file was created in /tmp (cat /tmp/uptime).
  7. Compare it with the current system uptime by typing the uptime command on the command line.

The scenario above worked successfully on a server with the Amazon Linux O/S installed, but it should work on other linux boxes as well. This modifies the crontab of the current user, without touching the system's crontabs and doesn't require the user inside the crontab entry, since you are running things under your own user. Easier, and safer!

How to write cron job in AWS EC2 server

First of all, you need to put an space between php and /var:

From

* * * * * php/var/www/html/welcome.php

to

* * * * * php /var/www/html/welcome.php
^

Then, you'd better use /bin/php instead of just php. To determine where the php executable is located, type which php in your console, it will give you the file path. So it will become something like this:

* * * * * /bin/php /var/www/html/welcome.php
^^^^^^^^

More things:

  • check if crontab is saved properly? Type crontab -l. Your new crontab line should be there.
  • is the script exactly in this dir? Try ls -l /var/www/html/welcome.php.
  • is the script executing if you do from console? Try /bin/php var/www/html/welcome.php to see if it is a script or crontab problem.
  • does the script have executing mode? Try chmod 755 /var/www/html/welcome.php

Keep us updated so we can find what can be causing the error.

Laravel 5.7 schedule via cron on AWS EC2 linux ami - commands not running

Based on our comments, the issue is with your PHP executable's path. The path is wrong in your cron job definition. Change it to:

* * * * * /usr/bin/php /var/www/html/artisan schedule:run >> /dev/null 2>&1

Assuming that the artisan executable is located at /var/www/html, that should fix it.

In case you are not sure if that's the correct path, you can type pwd from your project root (where artisan is located) to get the correct path to your artisan executable.



Related Topics



Leave a reply



Submit