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
Related
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
I hava a php-email (phpmailer 5.2.14) script which work fine when I run it in bash:
pi#schnickschnack: php /var/www/html/email.php
when i run this script with cron (sudo crontab -e):
*/1 * * * * root php /var/www/html/email.php
syslog says...
Jan 22 08:53:01 Schnickschnack CRON[4482]: (root) CMD (root php /var/www/html/email.php)
...but I get no mail.
I have another php-script which works fine with crontab. this script inserts values from phpmodbus into a mysql-db...
does anyone have a hint why the mail-script does not work with cron?
try
* * * * * php /var/www/html/email.php
otherwise, cron tries to execute the command "root", which is not a command.
As you are running by cron, all your usual $PATH and ENV are not available.
So CRON has no idea where to find "php".
Depending on your install - determine location of the PHP bin:
which php
use the resulting path in your cronjob. eg:
*/1 * * * * /bin/php /var/www/html/email.php
** Unless intended, dont leave the email.php script where it could be run "unintentionally" by anyone simple hitting the webserver. email.php is certainly on script kiddies hit list.
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']
I have a cronjob that runs the following:
* * * * * php /path/to/phpfile.php >> /cronlog.txt
when I run the php file in bash everything works, but when the cronjob runs it, one command fails:
shell_exec("redis-cli ping"); and returns an error that sh: 1: redis-cli: not found
Does anyone know why the cron user using PHP shell_exec would not be able to use the redis-cli command?
update
git diff /env_term.txt /env_cron.txt
-SHELL=/bin/bash
-TERM=screen
-SSH_CLIENT=*************
-SSH_TTY=/dev/pts/0
-USER=root
-LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.
-TERMCAP= { a bunch of giberish }
-PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
-MAIL=/var/mail/root
-STY=*************
-PWD=*************
-LANG=en_US.UTF-8
-HOME=/root
-SHLVL=2
LANGUAGE=en_US:en
+HOME=/root
LOGNAME=root
-WINDOW=2
-SSH_CONNECTION=*************
-LESSOPEN=| /usr/bin/lesspipe %s
-LESSCLOSE=/usr/bin/lesspipe %s %s
-_=/usr/bin/env
+PATH=/usr/bin:/bin
+LANG=en_US.UTF-8
+SHELL=/bin/sh
+PWD=*************
Did you check if your PATH variable is the same when cron is called.
A quick check is to add a dummy cron job to output the current environment variables passed to cron:
* * * * * env > /tmp/env.out
And then compare this output with when you run the env command from the terminal
try this solution:
cat cronjob
* * * * * php /path/to/phpfile.php >> /path/to/cronlog.txt
Then:
chmod +x cronjob
/etc/init.d/crond start #redhat based servers like centos
/etc/init.d/cron start #debian based servers like ubuntu
crontab cronjob
You can give absolute path of redis-cli to avoid any such issue related to environment variable.
I'm trying to set a cronjob to run every 20 minutes.
This works manually:
php /srv/www/mysite.co.uk/public_html/PP/Make_XML.php LONDON
I tried to use "crontab -e" and set it even to every 20 minutes with:
*/20 * * * * php /srv/www/mysite.co.uk/public_html/PP/Make_XML.php LONDON
it was saved to /tmp/crontab.0xYhei9m/crontab
And it doesn't seem to work.
What's wrong here?
EDIT:
Current stats:
*. cron is up and running:
root 31855 1 0 08:39 ? 00:00:00 /usr/sbin/cron
*. Running "crontab -l" shows:
*/20 * * * * /usr/bin/php /srv/www/mysite.co.uk/public_html/PP/Make_XML.php
LONDON
And still no go. Again manually running the script works just fine.
is the cron daemon even running?
it was saved to /tmp/crontab.0xYhei9m/crontab
Yes - that's the file you just edited - its NOT the file crond reads to fire jobs. Crontab whould then read this file, install the updated crontab in the location where crond looks for it and notify crond it needs to process the file.
Have you checked:
crond is running?
your uid is allowed to schedule cron jobs (usually via /etc/cron.allow / /etc/cron.deny)
that the script really isn't being started by cron and failing due to a permissions error?
that the version of crond you are using support $PATH and can find the executable?
In case this is already online, try using wget instead of php, with the url instead of the path, ie.:
*/20 * * * * wget http://YOUR_IP/~YOUR_USER/PATH/Make_XML.php
or
*/20 * * * * wget http://mysite.co.uk/PP/Make_XML.php
First make sure the url works , just by opening it with a browser
Hope it helps!
Crontab doesn't know anything about PATH variable. So use absolute path to your php (/usr/bin/php for e.g.)/ You can run command which php to find your php path