What should be given as the url to the script while adding it to cron scheduler. The script is at domain.com/scripts/script.php
PS:I am using cPanel
If you add the line
#!/usr/bin/php
to the beginning of your file (use 'which php' to find out your actual directory) and change the file mod to "executable", you should be able to run it just by calling like your second choice,
/public_html/scripts/script.php
I hope that works for you.
Here's a copy / paste out of one of the cron jobs that I run:
00 7 * * 1,2,3,4,5
/usr/local/bin/php
/home/processing/process.php
You must use the absolute path to the PHP binary as well as the absolute path to the script itself.
none of these.
but full absolute path from the root of the filesystem.
you can see that path with this code
echo __FILE__;
I had the habit of changing directory cd /var/www/vhosts/somesite.com/httdocs before running script with /usr/bin/php -f ./scriptname.php 2>&1 all in the same line on crontab.
I redirect the error output to get notified by email in case an execution error occured.
From crontab :
MAILTO=emailnotifications#mail.com
* * * * * cd /var/www/vhosts/domain.com/httpdocs/; /usr/bin/php -f testmail.php 2>&1
Related
I'm trying to make a cron file to be placed in /etc/croon. d. My problem is I don't want keep this file updated, so I'm looking for a way to get the software version dynamically from a file.
I have few other variables, but for now I think the problem is with $ (cat /software/VERSION), it works very well in shell script but not on croon.
#!/bin/bash
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
APPLICATION_ENVIRONMENT=SOME_STRING
VERSION=$(cat /software/VERSION)
HOME=/var/www/scripts/$VERSION/cron
CRON_LOG_DIR=/var/www/scripts/logs
*/2 * * * * root cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
This is the output on cron log:
(root) CMD (cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log)
(CRON) ERROR chdir failed (/srv/www/tdp/public/$VERSION/backend/cron): No such file or directory
Cron table is not a shell script! You cannot put variables there.
You have to call a script from the cron and do the logic there.
If you really have to set environment variables in cron, you can do it like this
*/2 * * * * root SHELL=/bin/bash VARIABLE=something cd $HOME & php -f $HOME/do_something.php $APPLICATION_ENVIRONMENT >> $CRON_LOG/something.log
But it might not work and you might make a mistake (I am not 100% sure I made the syntax right; it's not easy and it's not necessary).
You should put as little logic to cron as possible.
Also, you should not edit the cron file directly; use crontab -e instead, it will check if you made correct syntax.
So you should do
*/2 * * * * user /home/user/your-script.sh
and set the variables in your script. (You also shouldn't run programs as root if it's possible.)
I want to call a php script every minute using cron
i added the the below code in my crontab file
*/1 * * * * /usr/bin/php /var/www/html/test.php
but it dint give me the desired output.
Am i missing something?
Thanks in advance
You might want to check what happens when this php file runs by following
just use * instead of */1
* * * * * /usr/bin/php /var/www/html/test.php >> /var/www/html/test.txt
test.txt file will contain any error or anything else which cause your test.php to not working. and if it's not loading anything in test.txt, please double check your path to your file.
alternatively try running /usr/bin/php /var/www/html/test.php from your centos console to check whether everything ok with path to your 'php' and 'test.php' file.
I am trying to execute a php file hosted on linode with crontab.
Here's what i've done so far.
i added a line to :
/etc/crontab ('crontab -e' is used too)
And I want to execute this file every 2 mins.
*/2 * * * * /usr/bin/php /srv/www/path/to/my/php/file.php
Here's the code in my php file for testing
// Set error reporting
error_reporting(-1);
ini_set('display_errors', 'On');
ini_set('html_errors', 'On');
ini_set('allow_url_fopen', 'On');
$fh = fopen('gallery.xml', 'w+');
fwrite($fh, $_SERVER['REQUEST_TIME']);
fclose($fh);
Both the php file and xml file are with 777 permission.
If I open the php file directly in the browser, the xml file can record the variable.
But nothing happen when I used the crontab. It seems that it didn't work for me.
I am using Linode and debian 6.
Am I doing anything wrong? Please give some suggestion. Thanks.
Bryant
You may need to correctly set your working directory or use absolute paths for your fopen() as cron's default working directory is the home directory of whatever account the job's running under, so it may be ~/root or ~/yourusername (see this stackexchange question too). You may try this:
*/2 * * * * ( cd /srv/www/path/to/my/php/ ; /usr/bin/php -q file.php )
or this:
*/2 * * * * cd /srv/www/path/to/my/php/ && /usr/bin/php -q file.php
and the difference is that 2nd one will not fire PHP if cd failed which is perfectly what we want as if cd failed there will be no file.php to launch.
You can also set executable bit (i.e. chmod a+x file.php) and add this as very first line to your script:
# /usr/bin/php -q
so you'd be able to invoke your script as any other app or script (i.e. ./file.php). then your crontab entry would look:
*/2 * * * * cd /srv/www/path/to/my/php/ && ./file.php
And do not use cryptic "-1" in your error_reporting(). It tells nothing. Use E_ALL or anything that ends in valid setting and is more self explanatory than -1.
I am problem scheduling and running a script through cron job. I am on linux (ubuntu), it is a VPS.
What I am doing is I have put this line in crontab file that is here: /etc/crontab
I wrote:
*/15 * * * * www-data php /var/www/abs/phpscript.php
I have given 777 to the file and after writing above in crontab , I run command:
crontab crontab
Then after almost some time I got the mail in my /var/mail/username file that says: /bin/sh: root: not found
So I am unable to understand what is the problem.
I also run phpinfo and it shows the third variable as APACHE that probably means that PHP is running as apache module.
Please tell what can be the possible solution.
thanks in advance to every one who will try to solve my problem.
You can try also to run it using "wget -q -O"
or
*/15 * * * * lynx -dump "url" > /dev/null
Wget examples:
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' >/dev/null
If you need to post data you can use
--post-data "login=user&password=pass"
*/15 * * * * wget -O /dev/null 'http://www.mydomain.com/document.php?&user=myuser&password=mypass' --post-data 'foo=bar' >/dev/null
If you edited /etc/crontab, you should re-read the warning at the top of the file:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
Running crontab(1) on the /etc/crontab file probably contaminated the root user's crontab(5) file (the one stored in /var/spool/cron/crontabs/). I suggest running crontab -e as root to edit the crontab file, and remove all the entries that are identical to the entries from /etc/crontab. (Maybe you just contaminated your own personal crontab(5) -- if crontab -e as root didn't show anything, run crontab -e under your own personal account and see if the system-wide entries were duplicated into your own crontab(5).)
I don't know what file you ran chmod 777 on, but that was probably unnecessary. You really should set your permissions to be as strict as possible to confine the results of malicious attacks or unintentional mistakes.
You are running a crontab as a user, which means you can't specify the user in the cron.
The template you borrowed your example from was for a system (root) cron.
Remove the username and try again.
I need to send emails hourly and daily. I've tried nearly everything but it appears my crontab just won't work. If I run the scripts via a browser e.g
http://localhost/Maisha/Functions/sendhourlymails.php
my emails get sent beautifully.(I moved default website localhost to public_html.) I don't know whats wrong. I read some post in stack overflow including the executable path of php helps hence I've put the /usr/bin/php before the actual script to be cronned will work but it does not. Removing /usr/bin/php does not work. Adding php before the actual script isn't working.
I have the following entries in my crontab.
# m h dom mon dow command
0 * * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
0 0 * * * /usr/bin/php /home/maxwell/public_html/Maisha/Functions/senddailymails.php
Try to call the script via http with wget like so:
* * * * * wget http://localhost/myscript >/dev/null 2>&1
Yeh, wget is good option, also you can try to use:
0 * * * * /usr/sbin/php /usr/bin/php /home/maxwell/public_html/Maisha/Functions/sendhourlymails.php
but it could work wrong due to relative paths.
Also you should look at http://php.net/manual/en/features.commandline.php
Try to put this into your .php file
<?php
#!/usr/local/bin/php -q
//your code here
?>
Then if you include any file into this file you must use something like:
include"/var/www/../your_absolute_path_from_root_folder/connect.php";
Finnaly make sure this file has the right permissions..Try
chmod 755 /var/www/.../file.php
Then if you edit your crontab file with the following command
vi /etc/crontab
put something like
10 6 * * * root php /var/www/..path../file.php
and restart the service with this command
/etc/init.d/cron restart
you have do your job!!
Note-Tip:the php file isn't neccessery to be into public_html
folder!!