I try to run PHP scripts on my debian server, everyday at midnight & 1 minute.
So I created the crontab file in admin, with
crontab -e
who seems to be in
/tmp/crontab.ky3Q3F/crontab
And I put the line
01 00 * * * /usr/bin/php5 /var/www/MySite/cronTB.php
Bellow the comments.
The goal is to run cronTB.php (with PHP) everyday at 0h01.
Is my syntax correct ?
Is the correct file to edit, to the correct path ?
Is the CRON daemon still runing after I run it with:
/etc/init.d/cron start
even if I close the ssh connexion ?
Thanks for help
Related
I have a cron job set up to write a local json file. It should run every hour and over write
previous file. but it is not running unless I run it manually via the cli. Here is how i have set up on the crontab
00 * * * * php /var/www/cron_jobs/pixel_mapping.php >/dev/null 2>&1
the permissions of the php file are
rw-rw-r-- 1 username username 822 Apr 13 11:02 pixel_mapping.php
my user permissions are
drwxrwxr-x
If I run it as php /var/www/cron_jobs/pixel_mapping.php on the cli
it does write to the json file. any ideas or trouble shooting ideas to try and
have this done by the cron?
many thanks
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
Using Cacti, I'm trying to run the following command:
php poller.php > test.cfg
As expected I'm getting the following output :
root#SLINUX01:[/usr/local/cacti]$tail -f test.cfg
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
Waiting on 1 of 1 pollers.
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_14.rrd --template traffic_out:traffic_in 1463052230:2773678063:3900704413
05/12/2016 01:23:50 PM - POLLER: Poller[0] CACTI2RRD: /usr/bin/rrdtool update /usr/local/cacti/rra/stse01_traffic_in_15.rrd --template traffic_in:traffic_out 1463052230:1071486:1088742
05/12/2016 01:23:50 PM - SYSTEM STATS: Time:5.4128 Method:cmd.php Processes:1 Threads:N/A Hosts:3 HostsPerProcess:3 DataSources:9 RRDsProcessed:7
Loop Time is: 5.41
Sleep Time is: 294.58
Total Time is: 5.42
^C
And my manipulated has updated.
Now using the crontab, I made the following :
*/5 * * * * cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg
But when i go in the /home/cacti/test.cfg after cron execution , I got nothing, but the cron runned the script :
May 12 15:25:01 SLINUX01 CROND[19871]: (root) CMD (cacti php /usr/local/cacti/poller.php > /home/cacti/test.cfg &>/home/cacti/test.cfg)
What is the source of this problem?
Thanks in advance.
Your cron log entry only says that the command was tried. It does not guarantee that the command has been successfully started (permission problems? environment variables unset? unmounted filesystem?).
Do you use the cron table for the same user that can successfully run the command from command-line?
What are the exact paths when you run from command line?
Note that in crontab you try to invoke php without giving its full path - that may well be the culprit.
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'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