I host a couple of websites which have live twitter feeds. My set up means that each website has a tweets.php file that will get the tweets using twitter OAUTH and then write it to a text file. I then use a javascript file to get the JSON from the raw text (.txt) file.
Until I learnt about cron jobs I was refreshing the tweets.php files myself as part of my day-to-day admin.
However, I've set up my cron jobs and they don't seem to be executing the php file or writing to the text file (both have 777 permissions on them).
Furthermore, I have both a cron file created using crontab -e but there is also a directory called cron.hourly. As I wasn't sure which one to use I have put the cron jobs in both but they don't seem to be executing. Here are my files:
#daily /etc/webmin/mysql/backup.pl --all
0,5,10,15,20,25,30,35,40,45,50,55 * * * * /etc/webmin/status/monitor.pl
0 * * * * /var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php
0 * * * * /var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php
And the cron file (called 'twitter') within the cron.hourly directory is:
/var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php
/var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php
Now I've checked the cron logs and they seem to be running the jobs hourly but the tweets aren't updating and neither is the raw text file.
08:00:01 ip-10-142-232-156 CROND[25986]: (root) CMD (/var/www/html/websitetwo.co.uk/wp-content/themes/websitetwo/tweets.php)
Jun 5 08:00:01 ip-10-142-232-156 CROND[25987]: (root) CMD (/etc/webmin/status/monitor.pl)
Jun 5 08:00:01 ip-10-142-232-156 CROND[25988]: (root) CMD (/var/www/html/websiteone.co.uk/wp-content/themes/websiteone/tweets.php)
Jun 5 08:01:01 ip-10-142-232-156 CROND[26014]: (root) CMD (run-parts /etc/cron.hourly)
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26014]: starting 0anacron
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26023]: finished 0anacron
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26014]: starting twitter
Jun 5 08:01:01 ip-10-142-232-156 run-parts(/etc/cron.hourly)[26033]: finished twitter
Apologies if I'm missing something obvious but scheduled task automation is a new area for me.
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 a php file to execute every minute but when I am running it on command line it executes as expected, but when set to cron job it does not run. Below mentioned are things which I have checked:
cron service
service crond status
crond (pid 183872) is running...
Checked logs of cron execution :
May 21 00:27:01 spartans CROND[194123]: (root) CMD (/usr/bin/php /home/dev/my.php)
May 21 00:27:01 e2e CROND[194122]: (root) MAIL (mailed 84 bytes of output but got status 0x004b#012)
CronJob list in crontab -e
*/1 * * * * /usr/bin/php /home/dev/my.php
My request is to help me in finding error where things are getting wrong. I have started cron service couple of times.
I have the following set as a cron job:
*/1 * * * * /usr/bin/php /var/www/html/recalls/php/savesjson.php
I checked the status and I get this back.
Mar 10 16:29:01 big-hoster CRON[30417]: (root) CMD (/usr/bin/php /var/www/html/recalls/php/savesjson.php)
Mar 10 16:29:02 big-hoster CRON[30416]: pam_unix(cron:session): session closed for user root
The savesjson.php file should execute and basically save a new JSON file (current.json) in the same directory. If I just run this file (by going to the URL), it works perfectly. However, my cron isn't actually updating the current.json file.
current.json has permissions as follows:
-rwxr--r-- 1 root root 0 Mar 10 16:17 /var/www/html/recalls/php/current.json
savesjson.php has permissions as follows:
-rwxr-xr-x 1 www-data www-data 213 Mar 8 14:11 /var/www/html/recalls/php/savesjson.php
What am I missing here?
UPDATE: If I try to execute from the CLI, like so:
# /usr/bin/php /var/www/html/recalls/php/savesjson.php
It appears to execute, but the script doesn't do what it's supposed to (create new file) and there are no errors in my PHP logs. However, the PHP does work if I go directly to the URL (example.com/php/savesjson.php).
after a week of trying i dont know what else to do.
I have a simple php script that is on my webserver called getpoem.php
The script opens up a website pulls its content and saves it to a poem.txt on the server
<?php
//File to extract the Poem of the day
$homepage = file_get_contents('http://SomeWebsite.com/today.php');
$poemALL = substr($homepage,strpos($homepage,"<p>"),strlen($homepage));
.
. // extracting the poem and saving it to $poemFinish
.
file_put_contents("poem.txt", $poemFinish); ?>
so this is a fairly simple script (it works fine if i manually execute it). This script should be executed with the www-data user with its cron so i opend up cron with this command and entered the command it should run
sudo crontab -u www-data -e
0 3 * * * php /var/www/html/getpoem.php
to avoid any permission problems i gave the getpoem.php and poem.txt rwx rights like this (i know i should change it when its live but this is just to test)
-rwxrwxrwx 1 www-data www-data 1189 Aug 17 15:07 getpoem.php
-rwxrwxrwx 1 www-data www-data 1335 Aug 17 15:07 poem.txt
So this is the setup, but it will not execute.
What i did so far changing the "php" to /usr/bin/php to secure that cron know what php is.
Next thing i did was making shure that cron was running so i changed the cronjob to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /var/www/html/test.txt
wich again did nothing .... so i changed it to
2 * * * */usr/bin/php /var/www/html/getpoem.php | > /tmp/test.txt
that did not run the php file but created a empty (-.-") file called test.txt in my tmp dir.
So i think the problem must be someware in the acces rights of my www-data user. It's just very wierd because all my webcontent (php files, webapp usw.) are also owned by the www-data user and they run smoothly.
Do i need to grand extra privileges for the cron of www-data ?
When running php from cron, your working directory usually isn't the directory the file is located in. When writing a file, it might try to write the file poem.txt relative to / which usually isn't writable.
So you either set the working directory or you should use "absolute" paths. So for example:
file_put_contents(__DIR__.'/poem.txt');
where __DIR__ is a magic constant that contains the directory where the current file is in.
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']