How to call a cronjob file in ubuntu - php

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.

Related

how to allow CRON to access directory or files?

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?

Add new cronjob to crontab using PHP

I understand that people have asked this before but it is still unclear to me and their examples did not work. I want to be able to add a new cronjob to the crontab through a php script that I wrote. The php script concatenates a string which will represent the cronjob. For example:
*/1 * * * * php /example/path/sample.php
I want to add the line above to the end of my crontab. I understand you can edit a crontab using crontab -e, but I want to do this all through PHP. If the crontab is just a text file, I know how to write to it, but after searching online the crontab can be in several directories? I found examples where it is in /tmp and also in /var. If I want to append to a crontab and have that cronjob automatically run, where is the crontab located? I am using Debian linux.
Thanks
with crontab -e you write directly to crontab when you are in terminal... if you want to use a file to load to your crontab (and this is the only way if you want to do this in php) you must use crontab -l in terminal again to load the new file.
i dont know any way to manipulate terminal using php.
so create a file in php with the crontab commands, store it wherever you want and load it from the terminal

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

running php file with crontab

I have the following problem:
I'm using the latest crontab version with win 9x/nt
I put the crontab file, the exe file and the log file into the root of my website (aruba linux)
I put the following code inside the crontab file
0 24 * * * php example.php
In the example.php I make an insert
I've tried to do the operation going to the page and it's working fine.
However the crontab doesn't start, even modifying the minutes and hours like * *. In the log file there's written nothing.
I've read that the "php" voice represents the folder in which php is located.
How can know where is it on my website?
Do have to ask to the service of the server or what?
00 * * * * /usr/local/bin/php /home/username/myscript.php
The first path is the location of your php binary
The second path is the location of your php script.
Just specify the absolute path for example.php.
Otherwise crontab will never find it to execute.
To find it, you can use echo __FILE__; so, you'll get the full path for script.
Try the absolute path of the php file, and the absolute path of the php command :
0 24 * * * /usr/bin/php /full/path/to/example.php
if /usr/bin/php is not working, try which php and replace /usr/bin/php with the result of this command .
the default folder for crontab is set in the /etc/crontab file in :
HOME=/path/
and you can change it to your default web root, this way you can use just example.php, but if only you have root access .

getting php file to work with crontab

I have a php file that I can run from the browser and it works perfect. I tried to set up a cron job to run the php file, but am obviously missing something.
Since this php file was originally ran from the browser, it is uploaded to the /var/www folder. I made a copy of it in a new folder called /var/cron. I made this folder just to test. I will probably put it in another folder, but for now it is in this folder.
Here is what I did. After copying the php file to the /var/cron folder, I ran the crontab -e command to edit the crontab file. My cron job looks like this:
00,30,59 * * * * /var/cron/download.php
I have tried changing permissions by using chmod 755 download.php
that didn't do anything.
I have tried /usr/bin/wget -q /var/cron/download.php
this didn't do anything either.
What should I do?
If you add
#!/usr/bin/php
as the first line of the php file, you can run it from the command line (as long as it has appropriate permissions. You can test run by going to the directory and typing
./download.php
I'm a little surprised amccausl's approach didn't work for you.
Have you tried changing your crontab so it looks like this:
00,30,59 * * * * /usr/bin/php /var/cron/download.php
This assumes /usr/bin/php is where php lives on your server.
(You may also need to install a "cli" package for PHP, eg. Ubuntu/Debian's php5-cli.)
Try with full URL like :
wget -O - -q "http://www.domain.com/cron.php"

Categories