I am trying to make a cron job via my websites cpanel. I have talked to the support services and they gave me this command to run to execute a php file on my website.
/usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php
This doesnt seem to work though, I have also set the minute interval to every minute using */1.
I am using the code below inside of the file update.php:
<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');
error_reporting(E_ALL);
require('minequery.class.php');
include('../config/config.php');
$date = date("D-M-Y");
$file = fopen("UPDATE_LOG($date).txt", 'w');
$query = mysql_query("SELECT * FROM servers") or die(mysql_error());
if($query) {
while($row = mysql_fetch_array($query)) {
$ip = $row['ip'];
$port = $row['port'];
$name = $row['name'];
$string = "[UPDATE LOG]: $date - Updated Server $name \n";
fwrite($file, $string);
print("[UPDATE LOG] Updated Server $name </br>");
}
mail("duncan#mymcstatus.net","UPDATED","Server has updated","From: Mymcstatus");
} else {
print("Cant query");
}
?>
When I go to update.php manually through the web browser that code works, but nothing seems to happen with the Cronjob. What am I missing or doing wrong?
There are a few things that could be going on here. The first is to check the file permissions of update.php and make sure it is executable. The cron may be executing as a different user that doesn't have permission to execute update.php. The 2nd thing you can try is including this as the very first line of update.php with no whitespace before it.
#!/usr/local/bin/php
Usually cron jobs aren't run from the same directory where your PHP lives so it's possible that it is running but the output file is being created elsewhere. Try changing the output file path to be the full path to the file, i.e:
$file = fopen("/home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/UPDATE_LOG($date).txt", 'w');
With the help of the above comments I managed to fix the file paths but it also came down to the command. I had put the wrong user path in here is the command that works.
/usr/local/bin/php -q /home/mymcstat/domains/mymcstatus.net/public_html/redesign/scripts/update.php
Thanks for the help
It's always a good idea to cd to your script's path. This way you don't need any change in the name of the include and require files and any other file names engaging in file operations. Your cronjob command could look like this:
cd /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/; /usr/local/bin/php -q /home/mymcstatus/domains/mymcstatus.net/public_html/redesign/scripts/update.php
You don't need to supply any absolute file paths this way.
Related
I am trying to create a server within my Android phone. I am unable to execute any shell script from my PHP code.
Here's the code:
//index.php
<?php
$output=shell_exec("sdcard/htdocs/myscript.sh 2>&1");
if(!$output){
echo "Failed";
}else{
echo $output;
}
?>
//myscript.sh
cd sdcard/htdocs/images
ls -t1 | head -n 1
The script works fine within terminal emulator. I also tried changing permissions of the script file but that didn't work. I don't know if it requires superuser permissions to execute shell scripts within PHP code.
The whole code is used to return the filename of the last file created in the images directory.
Need suggestions to make this code work.Is there any other way to perform the required job?
Make sure what you have run
chmod a+x sdcard/htdocs/myscript.sh
on your file.
Also $output is not a boolean.
Your code looks fine. Superuser permission is not necessary for script execution. You should turn on PHP error output or check the PHP error log file. I bet you find the reason there. If not, recheck the directory, and file permissions:
./index.php
./sdcard/htdocs/myscript.sh
./sdcard/htdocs/images
sdcard and sdcard/htdocs require executable persmissions. sdcard/htdocs/images requires executable and read permission (ls in myscript.sh), and so does sdcard/htdocs/myscript.sh. But I guess it's something else because permission errors should be displayed (2>&1).
Edit
You can find the last modified file with PHP, no need to run another process. Take one of these two:
$images = glob('sdcard/htdocs/images/*');
$images = array_combine(array_map('filemtime', $images), $images);
asort($images);
echo $lastModifiedImage = end($images);
Or with some fewer array operations:
$images = glob('sdcard/htdocs/images/*');
array_reduce($images, function($previous, $element) use (&$found) {
$mtime = filemtime($element);
$found = $previous < $mtime ? $found : $element;
return $previous < $mtime ? $mtime : $previous;
}, 0);
echo $found;
sdcard and sdcard/htdocs require executable persmissions. sdcard/htdocs/images requires executable and read permission (ls in myscript.sh), and so does sdcard/htdocs/myscript.sh. But I guess it's something else because permission errors should be displayed (2>&1).
Probably FAT !
Below is my code,
test.php
<?php
$prefixnew = "testnew";
$file1 = fopen("testnew.txt","w");
$content_wr_g = "Test sitest"
fwrite($file1,$content_wr_g);
fclose($file1);
?>
test.bat
#ECHO off
C:\wamp\bin\php\php5.3.13\php.exe -f "C:\...\test.php"
popd
UPDATE
When i run (test.php) directly from browser it is working properly. if i run from scheduled tasks, it is not working.
I know very well about task scheduler steps to create a task. When i run set of update queries or delete queries means, it working nicely.
When i have code like fopen(), fwrite() etc it is not working
Instead of using relative path, use absolute path to the file:
$file1 = fopen(__DIR__.'/testnew.txt', 'w');
I assume you are using >= PHP 5.3, if not try:
$file1 = fopen(dirname(__DIR__).'/testnew.txt', 'w');
I've been trying to run a php file using window task scheduler. I've tried using the .bat file, but it won't work.
Here's what I've done:
I've created a task "Download Image"
I've created a .bat file, and the content is:
"C:\xampp\php\php.exe" -f "D:\server\newxml\download.php";
The php file that I want to run is:
ini_set('max_execution_time', 300);
$doc = simplexml_load_file('xml_edit_feeds.xml');
foreach ($doc->xpath("//item") as $item) {
$name = $item->productname;
$realname = preg_replace('/\s/', '',$name);
$url = $item->thumbnail_url;
$img = 'D:/server/newxml/imagethumbnail/'.$realname.'.png';
$filename = 'D:server/newxml/imagethumbnail/'.$realname.'.png';
file_put_contents($img, file_get_contents($url));
}
?>
This is the screen shoot of the windows task scheduler:
Is there something wrong from what I did? Thank you for your help
I didn't try this before, but I think you should use a browser exe program to open your webpage instead of php.exe.
Eureka! I got the answer to this, so, I what I've done is:
Set the Program/Script to: C:\xampp\php\php.exe
Set the arguments to: D:\path\to\php.php (In my case D:\server\newxml\download.php)
Set the start-in to the folder that contains download.php (php file that you want to run). In my case: D:\server\newxml\
I have a problem that i can't to shell_exec(), but it just won't work. Is there a problem whit shell_exec() or it just cant run myfile.sh somehow..
If i run code in shell it works fine. My php wont run in safe mode, i did check it from php.ini file and i did check disable_functions aswell.
My php code returns right now just null. And i did try also for testing purpose :
$result = exec('myfile.sh', $output, $status);
i got :
array(0){}int(127)
Both files have chmod 775 read and execute premissions.
I have file message.php what looks like
<?php
$user = "username";
$pass = "pass";
$from = "from who";
$to = "1234567";
$msg = "test message";
$host="https://web.myurl.ee:portnr/etc/etc";
$handle = curl_init($host);
curl_setopt($handle, CURLOPT_NOBODY, true);
curl_exec($handle);
$res = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($res >= 400){
exit;
}else if($res < 200){
exit;
}else{
if(function_exists('shell_exec') && !ini_get('safe_mode')){
$file = 'myfile.sh';
if(file_exists($file)){
$result = shell_exec($file ."$user $pass $from $to $msg");
var_dump($result);
}else{
echo "No files";
}
}else{
echo "check status";
}
}
?>
and myfile.sh file looks like
#!/bin/sh
if [$# -ne 5 ]; then
echo"jahas"
exit 1
fi
etc...
What i miss here, any tips what to look or what to do for next.
In code can be some mistypes, i didn't copy paste it.
Two things to check here:
First determine if exec and shell_exec are disabled in php.ini
disable_functions = exec
exex() accepts commands and no files if i am not mistaken so instead of simply adding the file name you should try:
exec(dirname(FILE) . '/myfile.sh');
Using the right directory and actually passing a command and not a file name.
You can use either an absolute path or a relative path. For relative paths, the current directory is usually the PHP files's. If you can't access your scripts even with an absolute path, there could be a problem with filesystem access rights. Keep in mind PHP scripts are typically executed as the web server's (system) user account.
Your shell scripts should be stored somewhere near or inside your application tree, so that you can grant acces to them to your PHP scripts (i.e. to your web server), without compromising too much your file system security.
I'm trying to run this simple script:
<?php
$PHP_PATH = "c:\Program Files (x86)\PHP\\";
$CLIENTPATH = dirname(__FILE__)."\Client.php";
$SERVER = "http://localhost:8080/mobile/Server";
$handle = popen("C:\WINDOWS\system32\cmd /C start ".$PHP_PATH." -f ".$CLIENTPATH." ".$SERVER, 'r');
?>
But I always get this Windows messagebox error:
Windows cannot find c:\program. Please make sure you typed the name correctly and then try again.
Searching on google I also find this thread about this error, but the meassures are I litlle drastic I guess.
So the problem its in my code ? Or there can be something else wrong ?
Thanks.
You need to escape the whitespaces in $PHP_PATH = "c:\Program Files (x86)\PHP\\";!