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.
- Login to console with SSH.
- Run
crontab -e
on the command line. - You are now inside a vi editor of the crontab of the current user (which is by default the console user, with root permissions)
- To test cron, add the following line:
* * * * * /usr/bin/uptime > /tmp/uptime
- Now save the file and exit vi (press Esc and enter :wq).
- After a minute or two, check that the uptime file was created in /tmp (
cat /tmp/uptime
). - 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
Linux/Unix Socket Self-Connection
Docker Installation on Linux Mint 19.2 Doesn't Work
Can Tmux Save Commands to a File, Like .Bash_History
Gdb/Ddd Program Received Signal Sigill
How Does Docker Share Resources
How Syscall Knows Where to Jump
How to Get Use Count from Linux Kernel Module
Socket Getting Created with Same Ip and Port on Local Host
How to Add a Carriage Return as a Character to a File
Difference Between Different Ways of Running Shell Script
Is There an Os Command I Can Run to Determine If Running Inside a Xen Based Virtual Machine
Install Lisp on My Linux Machine
How to Determine The Available Physical Memory in Linux
How Does Copy-On-Write in Fork() Handle Multiple Fork
Install R in Linux/Unix Without Having Root Privilage
Window Placement: Winsplit Revolution -Like Application for Linux (Kde)