Question

code working via apache, not on command line; how to fix

Asked by: DoppyNL

Hi,

I've got some code that is to process images in the background. I'm making a cron-job for this wich I will be running from the command line.
The code works just fine when I request the script via de web (via Apache), but it crashes when I run it from the command line.

The crash happens on this line:
$this->original_handle = imagecreatefromjpeg($this->original);
Where I believe that the function imagecreatefromjpeg is the problem.

I think the problem is caused by lack of memory, when requested from the web I use the attached function to increase the memory in PHP to be able to process the image.
I don't use this function on the command line, because when I do it crashes on the line I marked.

How can I adjust my code so it also works on the command line?
Why doesn't it work on the command line?

The last is also interesting for me, as I got a similar problem for another cronjob that fails to send out emails from the command line.

<?php
	static private function increase_memory($image_info)
	{
			$MB = 1048576;
			$K64 = 65536;
			$tweakfactor = 1.7;
			$memory_needed = round(	(	$image_info[0] * $image_info[1]
												* $image_info['bits']
												* $image_info['channels'] / 8
												+ $K64
											)
											* $tweakfactor);
			$memory_limit = ini_get('memory_limit');
			$memory_limit_new = $memory_limit + ceil((memory_get_usage() + $memory_needed - $memory_limit) / $MB); // <- Command line crashes here
			ini_set('memory_limit', $memory_limit_new . 'M');
	}

                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2008-02-14 at 00:29:25ID23162311
Tags

zend

,

php

,

5

,

command line

,

memory jpg jpeg image

Topic

PHP Scripting Language

Participating Experts
2
Points
200
Comments
26

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. apache counter?
    does anyone know how to get an apache counter going using apache server? i think that i just have to put a line in my index.html to reference the counter but im not sure thanks
  2. cronning a PHP script
    I have built a reminders system that queries a MySQL table and emails out reminders accordingly. To get the system into production, I need to run the script each morning. PHP is compiled into Apache as a module, therefore the script cannot be an executable. I have tried cro...
  3. Apache modules
    Hello I've checked the past posts but could not find an answer to this. I have apache installed on a Solaris 8 x86 box that came with the OS. I am running Apache/1.3.26. I have Apache started and working. When I type the following ‘httpd –l’ I get: Compiled-in modules: ...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: ygouthamPosted on 2008-02-14 at 00:36:08ID: 20891824

the php.ini file has a maximum limit to set the memory used by a particular script.  increase the value to a suitably high value to run your script and you should be done

 

by: ygouthamPosted on 2008-02-14 at 00:37:50ID: 20891829

/etc/php.ini file

 memory_limit = 8M      ; Maximum amount of memory a script may consume (8MB)

change the line to a higher value (have it at a safe limit though)

 

by: DoppyNLPosted on 2008-02-14 at 00:40:23ID: 20891837

The setting in php.ini is currently 64M. It has been that for quite some time now, some running website have a lower local setting.
I would think that 64M would be enough to resize a small image (1024x768) to 2 smaller images than that.

 

by: ygouthamPosted on 2008-02-14 at 00:45:21ID: 20891867

then probably the time limit is hitting your script.  change the max_execution_time and the memory_limit variable accordingly.

 

by: DoppyNLPosted on 2008-02-14 at 00:52:10ID: 20891895

The script crashes on the command line, there is no time limit when running a php script from the command line. The script works fine when requested via Apache, but it is a cronjob.
See: http://nl2.php.net/commandline

Also, I've verified that the script actually crashes on the lines I pointed out. Simply by putting a print-command just before and after. Only the one before is displayed.

 

by: ygouthamPosted on 2008-02-14 at 04:24:52ID: 20892685

then probably the time limit is hitting your script.  change the max_execution_time and the memory_limit variable accordingly.

 

by: nplibPosted on 2008-02-14 at 06:36:21ID: 20893559

apache and the command line use different php.ini files, search your computer for another one.

 

by: DoppyNLPosted on 2008-02-14 at 07:48:37ID: 20894265

I can find 3 php.ini files, one of them is a backup and isn't used.
The other 2 have the memory_limit setting set to 64M and 32M; both of them I think should be enough to process a file.
Alltough it could be I need even more (even though the files are relatively small).

The code snippet I posted earlyer I use to increase the memory for resizing an image when accessing the script via the web, it works fine there. But when I use that code on the command line, it crashes on the marked line. Perhaps that points to a problem ??

 

by: nplibPosted on 2008-02-14 at 07:51:51ID: 20894304

how do you know it's due to memory?

is there any error message?

I would make a copy of the apache php.ini and place it where the other one is.

 

by: DoppyNLPosted on 2008-02-14 at 07:58:37ID: 20894388

I had a similar problem in the past, where the script crashed when the jpg-function was called; this was solved by increasing the memory with the function I posted earlyer.

Now I run the same code from the command line.
When I use the function to increase the memory, the function itself crashes.
When I bypass the function on the command line it crashes on the jpg-function.

The same script works fine when requested via apache.

Unfortunatly I can't simply copy the ini file over the other file, as that might impact a running system to much and may create some big problems.

As far as I know there isn't an error message, the script simply stopt. I've got an error handler in place wich sends an email when an error occurs, and I'm not getting an email.
Where and how does PHP "dump" it's error messages when run from the command line? I'll look into retrieving some error message tomorrow; if anyone has a suggestion on how to retrieve that in the mean time, please post it; saves me a lot of time.

 

by: nplibPosted on 2008-02-14 at 08:01:48ID: 20894419

as far as I know, it just shows it on screen.
You have many command line php apps running that could be affected that badly by replacing the PHP.ini file with the apache one?

I would try just to see if it works.

It may solve all your problems.

just make a backup first.

 

by: DoppyNLPosted on 2008-02-15 at 01:00:00ID: 20900218

Yes, there are multiple processes running in the background, just trying to copy the php.ini file is not an option, this could have a big impact on those processes wich is simply not acceptable on a production server.

The other php.ini file is related to the plesk-admin-panel and is used for the secure webserver that is needed for that. So, it won't affect the command line settings.

 

by: DoppyNLPosted on 2008-02-15 at 01:05:46ID: 20900237

STDERR doesn't spit out an error message as well, I redirected that output to a file, but nothing showed up.

 

by: nplibPosted on 2008-02-15 at 06:31:52ID: 20901939

it could be a permission problem.

trying running the command from the command line with root privs.

sudo php .....

 

by: DoppyNLPosted on 2008-02-15 at 06:34:15ID: 20901971

Good suggestion, alltough the script is allready being run with root privaleges.
I should have mentioned that...

 

by: nplibPosted on 2008-02-15 at 06:39:09ID: 20902026

try making another php file and have it look like this.
see you get any result

<?php
echo memory_get_usage();
?>

                                              
1:
2:
3:

Select allOpen in new window

 

by: DoppyNLPosted on 2008-02-15 at 06:42:50ID: 20902080

I added line 2 at the beginnen of the script I'm trying to fix.
It crashes on the line I added:

echo memory_get_usage();

When I comment out that line, the script continues until the problem above happens.

 

by: DoppyNLPosted on 2008-02-15 at 06:45:33ID: 20902110

From the manual:
5.2.1         Compiling with --enable-memory-limit is no longer required for this function to exist.

We aren't running this PHP-version yet; perhaps that is a problem, allthough php is compiled with the parameter.
I will be looking into upgrading to a up-to-date version of PHP on monday.

 

by: nplibPosted on 2008-02-15 at 06:45:47ID: 20902111

ok, now try this.

<?php
if( !function_exists('memory_get_usage') )
{
    function memory_get_usage()
    {
        //If its Windows
        //Tested on Win XP Pro SP2. Should work on Win 2003 Server too
        //Doesn't work for 2000
        //If you need it to work for 2000 look at http://us2.php.net/manual/en/function.memory-get-usage.php#54642
        if ( substr(PHP_OS,0,3) == 'WIN')
        {
               if ( substr( PHP_OS, 0, 3 ) == 'WIN' )
                {
                    $output = array();
                    exec( 'tasklist /FI "PID eq ' . getmypid() . '" /FO LIST', $output );
       
                    return preg_replace( '/[\D]/', '', $output[5] ) * 1024;
                }
        }else
        {
            //We now assume the OS is UNIX
            //Tested on Mac OS X 10.4.6 and Linux Red Hat Enterprise 4
            //This should work on most UNIX systems
            $pid = getmypid();
            exec("ps -eo%mem,rss,pid | grep $pid", $output);
            $output = explode("  ", $output[0]);
            //rss is given in 1024 byte units
            return $output[1] * 1024;
        }
    }
}
echo memory_get_usage();
?>

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:

Select allOpen in new window

 

by: nplibPosted on 2008-02-15 at 06:47:48ID: 20902136

if that doesn't work, tben yeah, upgrade php

 

by: DoppyNLPosted on 2008-02-18 at 06:09:59ID: 20920006

Haven't had the time to look into this yet.
We're working on another problem with a database that is a little overloaded and causes a system te crash. Need to fix that first.

I'll be back! don't worry!

 

by: DoppyNLPosted on 2008-02-19 at 06:12:45ID: 20928385

Function works like a charm, I can get the memory usage at the moment.

Turns out the call "ini_get('memory_limit');" also doesn't return a value.
I tried to cope with this by using setting the limit variable manually in my increase function, I even tried it setting it to 0.

But the whole script still crashes still at
$this->original_handle = imagecreatefromjpeg($this->original);

 

by: DoppyNLPosted on 2008-02-19 at 06:14:06ID: 20928395

Perhaps the call "ini_set('memory_limit', $memory_limit_new . 'M');" is also not working?

 

by: DoppyNLPosted on 2008-02-20 at 02:11:46ID: 20936340

I tried out a script I attached.

When I run this as root I get the following:
string(3) "64M"
string(3) "64M"

When I run this as non-root I get the following:
string(2) "8M"
string(4) "100M"

When I run this via apache I get the following:
string(3) "64M"
string(4) "100M"

Seems that I'm unable to change the memory limit when running as root.

<?php
var_dump(ini_get('memory_limit')); 
print("\r\n");
ini_set('memory_limit', '100M');
var_dump(ini_get('memory_limit')); 

                                              
1:
2:
3:
4:
5:

Select allOpen in new window

 

by: nplibPosted on 2008-02-20 at 07:32:25ID: 20938708

thats odd.

 

by: DoppyNLPosted on 2008-03-04 at 02:38:45ID: 21039718

PHP version has been upgraded.
Resulted in me having to update some code throughout the software, but all that was done with relative ease.

The script now works fine and does exactly what it needs to do. I think some bug fixes in PHP where the actual cause for fixing this problem. Alltough the suggested function is in use at the moment, otherwise it would fail the moment the function would be used.

Some other minor problems have also been resolved automagicly by the upgrade. I can recommend an upgrade any time ;-).

Now for the database crash to be solved! But that's a different ballgame.

points awarded to the suggested function, as it was definatly needed.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...