I have 2 .php files in my application - book.php and weather.php. I create a file named "runscript" in /.openshift/cron/minutely. This file contents:
#!/bin/bash
php -f $OPENSHIFT_REPO_DIR/weather.php
This script send me message to phone every minute, it's OK.
Then I replace to:
php -f $OPENSHIFT_REPO_DIR/book.php
This script MUST send me message too, but nothing is happing. But if I just run this script by my webbrowser (go to the http://xxx-xxxxxxx.rhcloud.com/book.php) so I got my message. How is it possible? Magic?
Did you miss the #!/bin/bash part? That's needed to run the shell script.
For why your cron job is not executing, check the cron logs on OpenShift. You can find them at ~/app-root/logs/cron_*.log when you SSH into your gear.
Make sure your cron job is execuable with chmod, and has the shebang line as #gnaanaa says. Also check if you have one of the .openshift/cron/minutely/jobs.{allow,deny} files as they may cause cron to skip your job. (See the cron README for more information.)
And after your cron job is working, you can get rid of the wrapper script runscript and have cron call book.php directly. To do so, place book.php directly into .openshift/cron/minutely, make it executable, and add this shebang to it:
#!/usr/bin/env php
Hope this helps.
I use openshift aswell and executed a php file with a cron aswell.
#!/bin/bash
php ${OPENSHIFT_REPO_DIR}index.php
This executes the script normally at first sight. However no output was produced. The problem was, that all the required php files couldnt be loaded because the working directory was not the same as it would be when loaded by the webserver. Setting the working directoy in the php script itself will prevent this error and makes the script perfectly executable by the cron.
This should help some people to get their script running.
Related
I have a PHP script creating a MySQL dump, working perfectly when launched from the browser. It calls mysqldump through the exec() function.
I scheduled this PHP script as root in the crontab (on an Ubuntu VPS). It fails with the following error message :
sh: 1: cannot create ./backups/db_20220522.sql: Directory nonexistent
I have set_include_path('/var/www/mysite.com/'); at the beginning of my PHP script. The script and the "backups" directory are in there.
What am I missing ?
Thanks a lot for your help
You need the specify the full path to your backup directory in your script .
Cron runs from a different directory.
eg
/home/username/domains/domain.com/public_html/backup
I have a situation where I need to call a batch file from a php script... however this batch file needs to run as admin in order to work.
My solution was to create a shortcut to the batch file and check the box to run as admin from the shortcut... however I can't get php to call the shortcut.
I have tried:
exec("C:/path/movefiles_admin.lnk")
and
system("cmd /c C:/path/movefiles_admin.lnk");
Neither of which work. Any suggestions?
Try this:
exec("START C:/path/movefiles_admin.lnk");
START Starts a separate Command Prompt window to run a specified program or command.
You can run nonexecutable files through their file association by typing the name of the file as a command
If your PHP has issues executing shortcut to batch file, try executing simple read and write actions to a test.txt file. (To check whether you have PHP running in safe mode).
If it doesnt do these basic actions then you have some configuration issues.
If a program is started with exec function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.
Please refer this link for your version of PHP: manual
I need your help here.
I wrote one PERL script for PHP application which needs to be run for every 5 mins.
This script will call PHP program, which will fetch data from MySQL DB and will generate a excel report and will mail those reports to specific users.
Every thing seems to be fine when I ran this script manually with the command (perl reports.pl).
But when I set this Perl in a cron tab, nothing works and reports are not getting generated.
Details: perl script path /opt/app/deweb/web/EDI/Microsoft/reports.pl
this script will call PHP program (/opt/app/deweb/web/EDI/Microsoft/reports.php)
content of script
#!/usr/local/bin/perl
use Net::FTP;
use File::Copy;
use POSIX;
#errorreport = `php /opt/app/deweb/web/EDI/Microsoft/reports.php`;
print "#errorreport\n";
exit;
It is working perfectly when running Manually using command - perl reports.pl
No results, when set in CRON:
*/5 7-19 * * * /usr/local/bin/perl /opt/app/deweb/web/EDI/Microsoft/reports.pl
Please note that this crontab is under super user account named webserv and my login is having access to edit under this super user account.
I'm editing this cron tab using command :: sudo -u webserv crontab -e
I would check the following:
Does it run using sudo -u webserv perl reports.pl? If not, fix the problem for the webserv user (permissions or whatever) and it should work via cron too.
Does which perl using your login give you /usr/local/bin/perl? If not, change the path to Perl in crontab to what you got in which perl to fix the problem.
I found myself to be in the same situtation. After trying to find out the reason, I am almost sure about the reason this happens. Crontab does not have the same environment variables as you when running the script. You must be sure about paths. Try for example run your script like /perl-path /path-to-perl-script/script.pl outside the parent directory of the script and I am almost sure that your programm will not find some files. And as you call one php script from the perl script, it's possible to have the same problem with paths to your php script too.
So the solution is to use absolute paths and no relative.
Also at your perl script don't use php but /full-path-to-php for example:
#errorreport = /usr/bin/php /opt/app/deweb/web/EDI/Microsoft/reports.php;
Description: I am not very familiar with using a lot of bash/shell. I currently have a cron tab set up on an Ubuntu server that runs a Shell script. The Shell script then is suppose to run a PHP script, however, instead I am getting the following error message:
Could not open input file: wscript.php
At the top of my shell script I have written #!/bin/bash
Then the shell script itself I am passing a bash variable to PHP script.
while read bashvariable
do
php wscript.php "$bashvariable"
done
Keep in mind when I run this this shell script manually the script executes and fires correctly.
At the top of wscript.php I have placed in #!/usr/local/bin/php.
wscript.php has an include file of wscript-add.php
I have attempted to change the permission of all files to 777 and I haven't had any luck on getting the cron tab to run correctly.
Below is what my cron tab looks like:
*/2 * * * * sh /var/www/website/wcron/wcron.sh
My Question: What could cause my PHP file to not fire correctly when used by cron? Do I need specific file permissions on each file to run correctly?
You need to specify full path to php script as when cron runs, it uses different current directory.
I have a windows server, and I' trying to use scheduled tasks. When I create the job and try to run it through the interface, it says it works, but when I look at the DB it shows me that nothing happend. This is what I placed in the Path to executable file
"D:\Parallels\Plesk\Additional\PleskPHP5\php.exe" -f "D:\inetpub\vhosts\fab-offers.com\httpdocs\cms-sites\vipstore\users\index.php"
Check for:
Is the script working if you run it manualy?
Does php error logs contain anything related to the script?
Does the script contain paths like $_SERVER['DOCUMENT_ROOT'] that it won't have when run not from browser?