EC2 Cron - Not Loading External File - php

I have built a program which loads values from an external file like so:
$terms = fopen('terms.csv','a+');
while($row = fgets($terms)) {
$termarr[] = urlencode($row);
}
This works on localhost, as well as when launched on my EC2 server remotely through the command line. However, when I run this through a cron job (*/5 * * * * ec2-user /usr/bin/php -q /var/www/html/loader.php), the file is not loaded and I get errors later on in my script due to $termarr containing 0 elements.

Add the following line to your cron :
*/5 * * * * ec2-user cd /var/www/html/ && /usr/bin/php -q loader.php
To make sure you are executing your script from /var/www/html
Last thing to checkout is the file's owner of your script, please be sure that ec2-user owns it, by chown it.
MrRhum.

Related

Cron tab doesn't run most of tasks

I setup several cron jobs to make things work. laravel scheduler works perfectly but my other cronjobs not working at all.
*/2 * * * * /usr/bin/php /var/www/cronjobs/index.php
when I run on the console /usr/bin/php /var/www/cronjobs/index.php it works properly. I checked executable php path with which php and gives me /usr/bin/php nothing wrong with path afaik. I tried to run php script as apache user www-data I opened crontab with crontab -u www-data -e and paste command there.. it didn't work too.
I also tried send dummy notify with crontab and it also didn't work either
dummy example
* * * * * /usr/bin/notify-send 'test'
both of them doesn't work. What am I missing here ?
The second command will not send notification as cron have no idea of your desktop environment.
The first command probably use some environment variables. So instead of run in command line you can try to create a script:
#!/bin/bash
source /path/to/user/home/.bashrc #you can try also .bash_profile
/usr/bin/php /var/www/cronjobs/index.php
and your cron to be like:
*/2 * * * * /path/to/script.sh

Cron job does not execute a php file

I have setup a cron job thusly:
*/15 * * * * root /usr/bin/php5.6 /var/www/example.com/cr.php
(Yes, root is owner for now - I'll fix that later)
I have also tried with -q after .../php5.6
but it doesn't seem to be working, even though syslog shows it executes every 15 minutes - for example:
Dec 20 17:45:01 e2e-53-27 CRON[2601]: (root) CMD (root /usr/bin/php5.6
-q /var/www/example.com/cr.php)
If I execute the part after the username 'root' at the bash prompt it does work.
This is apache2 running on Debian. I don't know if this makes any difference but the PHP file is using curl to call an external API that sends an SMS.
You should use this format in cron file to make it work:
*/15 * * * * /usr/bin/php5.6 /var/www/example.com/cr.php
And the best way is to put this in script and add as first line in the script command:
. /root/.bash_profile
to make the environment as it is in command line

Crontab is not working... Tried all possible solutions

I am trying to run an automated tasks from php script which I couldn't run. For testing purpose, I created test.php and still nothing is working.
These are the lines I executed in the flow
crontab -e
This opened nano with
#.............hint text were here
#.............hint text were here
* * * * * /usr/local/bin/php -f test.php
then I restarted the crontab
sudo service cron restart
confirmed the cron is working using
pgrep cron
and got the result. Also
/usr/local/bin/php test.php
also gave me Hello World in the terminal
Test.php
!#/usr/local/bin/php
<?php
echo "Hello World";
?>
The cronjob is also confirmed by executing
crontab -l
Tried to set permissions too
chmod +x test.php
chmod 755 test.php
chmod 600 test.php
Looking for a support to make it happen. Thanks.
Chances are your crontab is working but you just don't see the output because by default cron mails the standard and error output to the owner of the cron job. So what you can do is redirect them to a file and check it out manually.
You can do that by editing the crontab entry so that it looks like this:
* * * * * /usr/local/bin/php -f test.php > /tmp/log.log 2>&1
And then in a minute you can check it out to make sure your job is actually working.
I hope this helps.
EDIT
As it turned out in the comments of the question there was actually an error /bin/sh: 1: usr/local/bin/php: not found recorded in the log.log file which was fixed by just using php
Try doing the following
*/1 * * * * [username] /usr/local/bin/php -f test.php
e.g. */1 * * * * root /usr/local/bin/php -f test.php

Elastic Beanstalk - Cron job is running but not executing

This is my very first time running a cron job on Elastic Beanstalk (EB). After deploying my code, it seems the cron job is created and running but the PHP script is not executing correctly. Here's my set-up.
In my .ebextensions folder I have a file called 01run.config.
container_commands:
01_remove_old_cron_jobs:
command: "crontab -r || exit 0"
02_cronjobs:
command: "cat .ebextensions/cron_jobs.txt > /etc/cron.d/cron_job && chmod 644 /etc/cron.d/cron_job"
leader_only: true
In my .ebextensions folder I also have a cron_jobs.txt file. Please note that I have an line break at the end of this file as instructed by another stackoverflow post. In my example below I am running the command as ec2-user but I also tried root.
* * * * * ec2-user /usr/bin/php -q /var/app/current/tests/cron.php
After deploying my code, I can see that the file /etc/cron.d/cron_job has been created. I can also see the cron job running every minute when I run sudo tail /var/log/cron.
[ec2-user#ip-xxx-xxx-xxx-xxx ~]$ sudo tail /var/log/cron
Apr 13 12:54:53 ip-xxx-xxx-xxx-xxx crontab[26093]: (root) DELETE (root)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx crond[1230]: (*system*) RELOAD (/etc/cron.d/cron_job)
Apr 13 12:55:01 ip-xxx-xxx-xxx-xxx CROND[26128]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Apr 13 12:56:01 ip-xxx-xxx-xxx-xxx CROND[26139]: (ec2-user) CMD (/usr/bin/php -q /var/app/current/tests/cron.php)
Within /var/app/current/tests/cron.php I have some code that adds a row to a MySQL database (hosted on RDS). But nothing is being added to the database.
I then tried running the cron command directly through my terminal window:
$ /usr/bin/php -q /var/app/current/tests/cron.php
And it runs without error and adds the record to the database. I am logged in as ec2-user in terminal.
Have I missed something? Or is my cron job code set-up incorrectly?
I had a similar problem with a php script that was trying to access an AWS RDS database. Is your php script getting the database details with $_SERVER['RDS_xxxx']? If so, those RDS_xxxx variables don't exist in the environment when the php script is run by cron.
In order to fix this, I added the variables to the beginning of the cron file:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * php /path/to/my/script.php
Login via SSH and check if generated cron job file/etc/cron.d/cron_job have unix line ending i.e. ASCII text not win i.e. ASCII text, with CRLF line terminators.
To check the line ending refer the answer here.
Note: If you have windows line ending then you will have to convert the line ending of file .ebextensions/cron_jobs.txt, for that you can use dos2unix or similar program.
I had a similar problem with my RDS_ variables on AWS, I followed this discussion and it works.
This was my cronjob before:
RDS_HOSTNAME=<my_database_hostname>
RDS_PORT=<my_database_port>
RDS_USERNAME=<my_database_username>
RDS_PASSWORD=<my_database_password>
RDS_DB_NAME=<my_database_name>
* * * * * cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And changed to this:
* * * * * . /opt/elasticbeanstalk/support/envvars cd /var/app/current && bin/cake notifications send_push >> /var/tmp/notifications.log 2>&1
And now I can access them like: $_SERVER['RDS_HOSTNAME']

Problem running a small script as cron job

I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.

Categories