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
Related
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
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.
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
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.
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"