Running a php script in crontab - php

I'm trying to run a php script every day at 01:00 UTC. I have the line
00 01 * * * /usr/bin/php -f /var/www/html/test.php
in my crontab, but for some reason it's not running my script. I know the script itself works because I've tested it. It just won't run in the crontab. I've also tried using
00 01 * * * php /var/www/html/test.php
but that didn't work either. I've added #!/usr/bin/php to the very beginning of the php script I am trying to run. I've also tried a -q instead of -f. Nothing has worked, and I'm not sure what I can do to fix this. Is there some other method I should try if this still doesn't work?

Make sure your paths are correct. Try this syntax:
cd /var/www/html; php -q -c ./ test.php
The -q flag suppresses any header() from being written to standard out. This forces your script to be executed with the php-cgi binary instead of the command-line version.
The -c flag prevents the OS from changing directories since you do that with cd.

What I ended up doing was writing a ksh script that exported more paths and then ran the php script. This worked.
export ORACLE_HOME=/opt/oracle/product/11.2.0/client
export LD_LIBRARY_PATH=/opt/oracle/product/11.2.0/client/lib/
export PATH=$PATH:/opt/oracle/product/11.2.0/client/bin
php /var/www/html/test.php

having a local
web server running, you may call
http://localhost/test.php
from your crontab

Related

How to plan job in php script via exec and 'at'

I try to plan one-time job with 'at' command. There is next code in script:
$cmd = 'echo "/usr/bin/php '.$script_dir.$script_name.' '.$args.'"|/usr/bin/at "'.$time.'" 2>&1';
exec($cmd, $output , $exit_code);
When I run this command from script it adds the job to the schelude. This I see by the line in logs job 103 at Thu Sep 3 15:08:00 2015 (same text contains $output). But then nothing happens in specified time like at ignores the job. And there are no error messages in logs.
When I run same command with same args from command line on server it scheludes the job and than runs it at specified time.
I found out that when I try to plan a job via php script it runs under apache user. I tried to run next in command line on server:
sudo -u apache echo "/usr/bin/php /var/www/pant/data/www/pant.com/scripts/Run.php firstarg secondarg "|/usr/bin/at "16:00 03.09.2015"
It works correct too. I checked sudoers and have added apache user with NOPASSWD privileges. Script Run.php has execute rights.
at.deny is empty. at.allow does not exist.
So question is: why 'at' does not run command given via php script (exec) but runs same command in command line? How to run it?
Thanks to all.
I found by chance answer at stackexchange.com:
The "problem" is typically PHP is intended to run as module in a webserver. You may need to install the commandline version of php before you can run php scripts from the commandline

php executable file not running

I've looked at how composer is set up to run a php script by calling composer without issuing php manually and I tried to do this myself but it isn't working. I know this is probably asked before but I can't formulate a question to find it.
I have my file
#!/usr/bin/php
<?php
echo 'teststring' . PHP_EOL;
when I issue test in the directory the file is located I get nothing, but if I do php test then I get "teststring" echoed out. The question is - how do I set it up so that I can execute the file directly?
I have given x permissions on the file.
Here is a sample output:
x#y:~/www/html/dev/Project$ ls
...
-rwxrwxr-x 1 www-data www-data 53 сеп 22 12:34 test
...
x#y:~/www/html/dev/Project$ test
x#y:~/www/html/dev/Project$ php test
teststring
x#y:~/www/html/dev/Project$ which php
/usr/bin/php
x#y:~/www/html/dev/Project$
You should try to locate php executable by using which instead of whereis like so
$ which php
... this command only searches for executables, while whereis tries to guess useful links, you can read more [here] (https://superuser.com/questions/40301/which-whereis-differences)
Then just run it like so
$ ./teststring
You can read more about executing shell scripts here
thegeekstuff
tldp.org

PHP to exec casperjs/phantomjs script

I'm having trouble using PHP to execute a casperjs script:
<?php
putenv("PHANTOMJS_EXECUTABLE=/usr/local/bin/phantomjs");
var_dump(exec("echo \$PATH"));
exec("/usr/local/bin/casperjs hello.js website.com 2>&1",$output);
var_dump($output);
Which results in the following output:
string(43) "/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:."
array(1) {
[0]=>
string(36) "env: node: No such file or directory"
}
The only stackoverflow posts I could find hinted that there's a problem with my paths, and that maybe the PHP user can't access what it needs.
I have also tried the following: sudo ln -s /usr/bin/nodejs /usr/bin/node
Does anyone know what I would need to do or change for this error to resolve?
Thanks
My guess is you have something, somewhere, that assumes node is installed.
First, are you running php from the commandline? I.e. as php test.php in a bash shell. If so, you can run the commands, below, as they are. If through a web server the environment can be different. I'd start with making a phpinfo(); script, and then run the troubleshooting commands through shell_exec() commands. But, as that is a pain, I'd get it working from the commandline first, and only mess around with this if the behaviour is different when run through a web server. (BTW, if you are running from a cron job, again, the environment can be slightly different. But only worry about this if it works from commandline but does not work from cron.)
Troubleshoot hello.js
The easy one. Make sure your script does not refer to node anywhere. Also remember you cannot use node modules. So look for require() commands that should not be there.
Troubleshoot your bash shell
Run printenv | grep -i node to see if anything is there. But when PHP runs a shell command, some other files get run too. So check what is in /etc/profile and ~/.bash_profile . Also check /etc/profile.d/, /etc/bashrc and ~/.bashrc. You're basically looking for anything that mentions node.
Troubleshoot phantomjs/casperjs
How did you install phantomjs and casperjs? Are the actual binaries under /usr/local/bin, or symlinks, or are they bash scripts to the . E.g. on my machine:
cd /usr/local/bin
ls -l casperjs phantomjs
gives:
lrwxrwxrwx 1 darren darren 36 Apr 29 2014 casperjs -> /usr/local/src/casperjs/bin/casperjs
lrwxrwxrwx 1 darren darren 57 Apr 29 2014 phantomjs -> /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
And then to check each file:
head /usr/local/src/casperjs/bin/casperjs
head /usr/local/src/phantomjs-1.9.7-linux-x86_64/bin/phantomjs
The first tells me casper is actually a python script #!/usr/bin/env python, while the second fills the screen with junk, telling me it is a binary executable.

Crontab and PHP

I have this crontab script on my CentOS server:
30 09 * * * /usr/bin/php - f /path/to/my/file.php > /same/path/error.log
This script is needed in order to create - daily - some pdf files in the subfolder pdf/.
I tried many options (php with or without path, with or without -f, etc.) I tried to change permission for the files in the subfolder (becouse these files are overwrited everyday).
But when I execute the script, here is the error.log:
TCPDF ERROR: Unable to create output file: pdf/myfile.pdf
But whe i run this script from shell, using
php file.php
or directly from browser, this is working.
Please, some advice on this issue? Thanks in advance.
Cpanel: wget -O - -q -t 1 http://www.happiweb.net/2014/02/thiet-ke-website-bang-wordpress-gia-re.html
http://domain.com/a.php: change to your URL

Adding php script to cron

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

Categories