how to allow CRON to access directory or files? - php

I've added a cron job to the crontab:
* * * * * /usr/bin/php /var/d1/directory/dir2/script.php
It's a script that needs to run everyday. But for now it's running every minute.
I can see that the job is added to the crontab and even saving a record to database. But, it isn't saving an excel file to the directory. But I try to call directly or paste the url and navigate to the script.php using a browser, it generates the file (so it's working).
So my question is how to allow cron to access a directly?

Related

PHP: Create a cron file in a directory owned by root? (Running as www-data)

I'm trying to add a new file to the /etc/cron.d/ directory on my ubuntu server, so that a new cron script is added/run when a new user is added. The user is created via a web UI, so the PHP code is executed by user www-data, but since the /etc/cron.d/ directory is owned by root I cannot write there.
I do have control of the whole server, but I do not want to permanently change /etc/cron.d/ to be owned by www-data for security reasons.
Ideally I'd like to do it all within a single PHP file, but I have tried using external bash scripts to navigate permissions, but without luck. Essentially, this is what I'd like to make work:
$filename = "something.cron";
file_put_contents("/etc/cron.d/".$filename, "* * * * * /usr/bin/php -f /another/file/to/execute.php");
What's the best way to go about this? My apologies if it's been asked/answered already, I searched and found a dozen similar problems, but nothing quite matching and since it's been over 30 minutes, I figured I could ask for help.
You need to set Cron job using exec command and can't set using PHP
exec('echo -e "`crontab -l`\n30 11 * * * /path/to/script" | crontab -');
This will run the script at 11:30 every day.
Check out this tutorial as well:
http://code.tutsplus.com/tutorials/managing-cron-jobs-with-php-2--net-19428

How to call a cronjob file in ubuntu

I have a cron file which is located in /var/www/html/mysite/cron/all.cronjobs
How can I call this file? The file contains of cron tasks
17 1 * * * /usr/bin/php /var/www/html/mysite/cron/file1.php
23 1 * * * /usr/bin/php /var/www/html/mysite/cron/file2.php
...
Should I call this file inside cron crontab -e ? Or should I set another cron to be called?
Any help please.
Is your cron file a list of cron jobs you'd like to add (a crontab)?
If so you could put the file in /etc/cron.d/ (or symlink it there), though be aware that this means it'll run as root.
To replace a users crontab with yours you can do
crontab /var/www/html/mysite/cron/all.cronjobs
I'm assuming that your all.crobjobs file is formatted like a crontab, so one or more jobs defined, one per line with the time/period definitions.
You can't "call" this file - it's not executable as it is. The contents of it need to be added to your user's crontab, using crontab -e as you suggest. Just copy and paste in the contents of all.cronjobs and save it.

How to add a cron task in his properly

I have this folder:
/etc/cron.hourly
how can I add a simple task, for example:
php somefile.php
how to add a file that will execute it every hour inside that folder, using crontab, centos OS
You can add a text file in that folder containing something like:
#!/usr/local/bin/php
<?php
include '/path/to/somefile.php';
?>
Where the first line contains the path to your php executable. After that you can use php like you would normally do.
Just keep in mind that the user running the script is probably not the same user that executes the php when you open a script from the browser.
Type 'crontab -e' at the command and add the following. This will execute every hour.
0 * * * * php -e somefile.php

What location is a Cron called from?

I have created a simple PHP script that is executed by a CRON daemon on my remote webserver. Every few minutes, the script looks up some JSON data on another website, and stores the results in a file in the same folder as itself.
However, it seems that the file that is used when navigating to the page in the browser, a different file is used than when the cron is executed.
In other words: When going here:
www.example.com/cronscripts/my_script.php the file /home/user_xxx/domains/example.com/public_html/cronscripts/datafile.json is used.
However, when calling the script using the following cron statement:
/usr/local/bin/php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
a file on a different location is used. Is this file in the /usr/local/bin/php folder in the Linux server? If so, I can't access it by hand because I can only access the /home/user_xxx/ folder.
Could someone tell me where the file ends up when using Cronjobs?
This seems to be a path mismatch for the location, you are most probably using a relative path somewhere for the json file.
I think you should schedule your cronjob as following and see if that works
* * * * * cd /home/user_xxx/domains/example.com/public_html/cronscripts && /usr/local/bin/php my_script.php
You should use the absolute path to your script on the server i.e. /var/www/script.php
Try to use instead
*/1 * * * * php /home/user_xxx/domains/example.com/public_html/cronscripts/my_script.php
Using chdir() to ensure you are starting in the proper directory may also help.
Just a side note: please always use #!/bin/env php
(see http://seancoates.com/blogs/use-env for more info)

Running Cronjob from a specific directory

I created a php script for generating RSS feeds which is to eventually be run via a Cronjob.
All the php files and the resulting RSS xml will be within a sub folder in a website. The php script runs fine on my local dev if I use terminal or the browser while within the same directory on my local development machine.
e.g. php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
works fine as does navigating to the dorss.php file in Chrome.
The CronJob has executed though with errors related to the fact that it cannot find the files specified with require_once() which are located in the same folder as rss or in a subfolder of that.
Long story short I need to have the Cronjob run from within the same directory as the dorss.php file so it can reference the include files correctly.
My knowledge on setting up a cronjob is VERY limited so I wanted to ask if this is at all possible (Change directory before running command) to do this on the same command line of the crontab or if not how can it be achieved please?
The current cronjob command is
0 0,6,12,18 * * * /usr/bin/php /var/www/vhosts/clientname/stagingsite/rss/dorss.php
TIA
John
You can change directory to the one you want and then run the php from that directory by doing this:
cd /var/www/vhosts/clientname/stagingsite/rss/
&&
/usr/bin/php dorss.php
It's easier than creating bash scripts, here is the result:
0 0,6,12,18 * * * cd /var/www/vhosts/clientname/stagingsite/rss/ && /usr/bin/php dorss.php
I don't know if there's a better solution, but I would suggest to write a bash script that changes to the correct directory before executing the PHP file, and use cron to execute this script. E.g:
#!/bin/bash
cd /Library/WebServer/Documents/clientname/siteroot/rss/
php dorss.php
Or even just:
#!/bin/bash
php /Library/WebServer/Documents/clientname/siteroot/rss/dorss.php
Save it somewhere and use cron to run that.

Categories