So, i have a script that calls 2 other scripts that back up devices.
Problem is, If i call the main script manually that opens up the other two in a screen session, everything works fine.
The 2 scripts it calls basically sends data to an sql server via a php script.
IE
I have the line
$link = mysql_connect("server ip", "username", "password", "database");
Running manually (Which runs from my user directory) it works fine.
If i have the cron job run, it will run fine to a point when it gets to the php section of the script it gets the error
PHP Fatal error: Call to undefined function mysql_connect() in /export/home/myname/my_Scripts/php_script_its_running
So, being that it works correctly if i manually run it, and not when i run it via cron, I am assuming it is either running a different php instance due to it being run by cron or something oddball like that? or, idk, not 100% familiar with cron jobs yet.
So while i was debugging it more, it looks to be using a different version of php than what my user account is using, 5.1 comparred to 5.4
Not sure if there is an easy way to change it to use my user accounts php setup? Unfortunately i have no root access to the box.
Answer
So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash
PATH = /usr/kerberos/bin:/app/php-5.4.3/bin
Answer So, i ran a working script, and added phpinfo(), grabbed the path information and added that to the cronjob so it would overwrite the defaults.
so the top of the cronjob file looks something like
SHELL=/bin/bash PATH = /usr/kerberos/bin:/app/php-5.4.3/bin
Related
I have an Apache server that I'm hosting from.
I have a php page that sends emails based on some script that looks like this:
<?php
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't know
the relative file paths otherwise.
require_once 'core/init.php';
$db = DB::getInstance();
$company = new Company(1);
require 'added-assets/plugins/phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
if($company->find_yesterday_appts(1)) {
.... email based on query....
From cpanel I have the cron job set to run every day at 12.
0 12 * * * php -q public_html/personnellonline/email_yesterdays_appts.php
core/init.php contains my connection string.
There are no errors I was told on the server, but no emails are ever sent when the cron runs. If I go to the page directly though then the query runs and emails are sent!
I once solved this issue by adding:
chdir(dirname(__FILE__)); //need this line so cron works! cron doesn't
know the relative file paths otherwise.
But not I'm back to square one. Any thoughts on what could be the issue?
When you say "I go to the page directly", how exactly are you getting things to work? Does running the PHP command you showed us work without using cron?
If you are loading the page through a web server, that is not the same as executing the file through PHP as you are doing in your cron command. If your script needs to be run through the web server, try using curl to load the URL that works in your cron command.
The only other thing I could think of, if running the PHP command actually works when not run through cron, would be that you may need a specific user to execute your cron command.
GracefulRestart, I changed the command to use CURL which I never used or heard of. It works now, thank you. I'm learning about CURL now as I read online.
ArtisticPhoenix, you are correct. I should move this outside the public area of my domain. I will do this!
The following works now:
curl -s "https://www.example.com/email_yesterdays_appts.php"
Thank you for your comments and help.
I am trying to build a small custom task scheduler. Basically the idea is I have cron run my process script, which looks in the database and finds any scheduled tasks that are ready to run, and runs them. So I think the best way to do this would be to try to launch the tasks "in the background" by way of shell_exec and using > /dev/null, which I understand makes it so the initial script (the process script) doesn't wait for the task scripts to complete.
So first, if there is a better way to achieve this, I'm open to suggestions. Though note I am on php 5.3 so there may be some options in 5.4 and up that I don't have access to :(
However here's the question at hand:
I am testing on WAMP on my windows machine and I am trying to make a call that looks like this:
shell_exec("php $path$base_url$querystring > output_test.txt 2>&1 &");
$path is the full windows path to the script
$base_url is the base url of the script I am calling
$querystring is of course the query string being passed to the task script
I am also outputting to output_test.txt which creates such file in same directory, where I get the following error:
Could not open input file:
C:\xampp\htdocs\email\batch_email_send_u2u.php?dealer=7
Yes I realize the path references an xampp installation, but that is not the issue - all the wamp files are executing from there and everything else has worked like this for years - it was just set up this way to support a legacy setup.
It seems to me shell_exec is locating and running php, it's just that it can't open the referenced script. Can't figure out why.
Also I need to eventually get this working on a real linux server so any advice on how to make that happen would be greatly appreciated!
Found a solution! Special thanks to dan08 for getting me set on the right path.
Ultimately I found the answer in this thread: Pass variable to php script running from command line
I ended up using the argv[] array as described in that post and with a little tweak to the script I'm calling it works like a champ now.
I have a php script and want to run it on an schedule. I am using local web server on windows (WAMP server) and need a way to run my_script.php every 10 min.
How to run a cron job on a PHP script, on localhost in windows?
recently I had sort of problems to run a cron job on a php script on localhost (WAMP server) in windows 7, when I was on a test to chronically fetch some links from www out there.
By the way I am sharing this for anyone that is on the same thing.
You will need a shellscript to run chronically, using Windows Task Scheduler. Also you will need a batch script (script.bat) to call the php.exe and run your php script (here called as my_process.php)
shellscript.vbs
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "C:\path\to\script\script.bat" & Chr(34), 0
Set WinScriptHost = Nothing
script.bat
"C:\wamp\bin\php\php5.4.12\php.exe" -f "C:\wamp\www\website\my_process.php"
Now, we are ready to set the Windows Task Scheduler to run shellscript.vbs at the required time interval:
Open Task Scheduler from windows Start menu
Go to Action menu and hit Create Task...
in General tab, fill the Name and Description fields as you want
in Triggers tab, hit New button.
from Begin the Task dropdown, select On a schedule and choose Daily
from Advanced settings section, select Repeat task every as you want and set for a duration on Indefinitely.
on Actions tab, from Action dropdown, select Start a program.
on the Program\script box, enter path to shellscript.vbs like C:\path\to\shellscript.vbs.
leave Add argumentts (optional) section empty.
in Start in (optional) box, enter parent directory of shellscript.vbs like C:\path\to\.
Hit upvote on this tutorial :) Have fun.
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
https://www.drupal.org/node/31506
If you use answer from Trix and get same problem as Metafaniel:
I have a problem, with this procedure, the task is beinge executed,
however my php script it's not running, I got the "Open with"
dialogue. If I see the properties of my task, it states: ActionName
C:\Windows\system32\OpenWith.exe even if I set it to the vbs file as
you suggested. What am I doing wrong?
you should use this:
Instead "enter path to shellscript.vbs like C:\path\to\shellscript.vbs." use "C:\Windows\System32\wscript.exe"
Instead "leave Add argumentts (optional) section empty" use (with quotes):
"C:\path\to\shellscript.vbs"
After many failed attempts at using Windows Task Scheduler this is the method that worked for me on my Windows 10 Professional machine. I just skipped adding a .vbs and .bat file and posted the PATH to the script file directly.
Under "Actions > Settings" Where it gives the option to browse for a "Program/Script" that's where I put the full PATH AND Filename to the script I wanted to run WITHOUT quotes.
C:\Server\Scripts\script.py
Then in the same area under "Actions > Settings" right below the text box where you just entered your "Program/Script", it has the area that says "Start in (optional)". That's where I put the PATH to the program that runs the script (ie: python.exe in this instance). But I did not specify "python.exe" directly I used the PATH ONLY without quotes like shown below.
C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64
If you installed Python directly without Visual Studio it will be under a different path probably something like "C:\Program Files\Python\Python37" or something similar if I remember correctly...
Little bit late but just for someone who don't want it to be lengthy and don't want to waste time in testing (as the aim is to host it to server), can do one thing by adding http content refresh in the html meta tag of that php file and add the required timer to it and run it in the browser.
NOTE: You have to make the system remain open untill you don't get the required output.
Again I am saying this is not the perfect and proper way but ya can save someone's configuration setting time.
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?
I have a PHP script that calls a .bat file using system(). The output is written to the screen and I derive some values from parsing this output. This is running on windows 2003 IIS server. PHP v5.2.0
Specifically I am using this script to launch an Amazon EC2 instance and assign an IP address to it. It has worked great for me so far but recently the problem started.
Here is the code
$resultBatTemp = system("cmd /C C:\Inetpub\ec2\my_batch_file_to_launch_instance.bat");
$resultBat = (string)$resultBatTemp;
$instanceId = substr($resultBat, 9, 10);
...
Once I have this instace Id I can run another batch file that calls associates an ip address with this instance. It would appear that the instance does get launched but I never get the output on the screen.
For some reason this has all stopped working, the page freezes and never refreshes. I also need to completely exit safari or mozilla otherwise all pages from the website fail to load. Only when I relaunch the browser can i view the website again. I've connected to the webserver that hosts these scripts and checked PHP error log but nothing shows there. I've opened a DOS prompt and entered the code from the bat file that way and it connects to amazon and launches the instance fine. Ive isolated this bit of code and removed the system command and the rest of the script runs fine, so it appears that the hold up is with outputting the results of the bat file.
Recently I have purchased a new domain name for the site so this script is running from this domain. Might this cause the problem?
thanks
------------------------------------------------UPDATE-----------------------------------------------
Well hope this helps someone, I didnt find out what was wrong but created a new PHP file with a simple system command that called a .bat file, and a non-existent .bat file expecting to get an error back but nothing - just the usual hang for ages. So I restarted IIS and this fixed the problem. Dont know what was wrong but that did the trick.
Maybe first check what the system() call returns. According to documentation it will return FALSE in case of failure. Also, including your my_batch_file_to_launch_instance.bat in the question might help in solving it.
Try using the passthru function
Also make sure that all your commands are safe use escapeshellarg() or escapeshellcmd() to ensure that users cannot trick the system into executing arbitrary commands.