/    Sign up×
Community /Pin to ProfileBookmark

PHP Script not shutting down? Or some other bug making it spawn lots of procs

Hello,

I am really desperate here. Dedicated host with tens of thousands of users of the site crashing periodically. If you can solve this I will love you forever.

My hosting povider instructed me to run the SSH command ‘ps uaxf ‘ to demonstrate that there are many instances of a single php script running and ‘free -m’ that the server is getting too low on memory.

I am new to having my own dedicated box so I’m terribly confused. They are telling me that this output means that the PHP script in question, import_photo.php, is not terminating properly. I can’t see how this could be, I have used set_time_limit(), checked for infinite loops etc, there are no database connections made, etc.

For the ps uaxf output… I am a new user and cannot do a pastebin, so here’s a sample line that is repeated a lot:

[CODE]
zane 24266 0.0 0.3 30792 15484 ? S 03:08 0:00 | _ /usr/bin/php /home/zane/public_html/startgoogleplus/api/import_photo.php[/CODE]

And the PHP Source for the import_photo.php:

[code=php]<?
set_time_limit(20);

require ‘facebook.php’;
chdir(“../”);
require_once ‘settings/settings.php’;
require_once ‘Zend/Loader.php’;
Zend_Loader::loadClass(‘Zend_Gdata’);
Zend_Loader::loadClass(‘Zend_Gdata_AuthSub’);
Zend_Loader::loadClass(‘Zend_Gdata_Photos’);
Zend_Loader::loadClass(‘Zend_Gdata_Photos_UserQuery’);
Zend_Loader::loadClass(‘Zend_Gdata_Photos_AlbumQuery’);
Zend_Loader::loadClass(‘Zend_Gdata_Photos_PhotoQuery’);
Zend_Loader::loadClass(‘Zend_Gdata_App_Extension_Category’);
session_start();

function getAuthSubHttpClient()
{
global $_SESSION, $_GET;
if (!isset($_SESSION[‘sessionToken’]) && isset($_GET[‘token’])) {
$_SESSION[‘sessionToken’] = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET[‘token’]);
}
$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION[‘sessionToken’]);
return $client;
}

$out = array(“success”=>false);

//
// Sanity check the album
//
$albumId = $_REQUEST[‘g_album’];
if(intval($albumId)<=0) {
$out[‘message’] = “Sorry, there was an error determining which album to upload to. Please completely reload this page and try again. If the problem persists, use the feedback form on the left side to let us know.”;
die(json_encode($out));
}

//
// Get the G+ user
//
try {
$client = getAuthSubHttpClient();
$user = “default”;
$photos = new Zend_Gdata_Photos($client);

$query = new Zend_Gdata_Photos_UserQuery();
$query->setUser($user->user);
$userFeed = $photos->getUserFeed(null, $query);
} catch (Exception $e) {
$out[‘error’] = “Could not log you in to Google… please clear your cache and try again.”;
die(json_encode($out));
}

//
// Resume the Facebook session
//
try {
$facebook = new Facebook($facebook_app);
$facebook->setAccessToken($_REQUEST[‘accessToken’]);
$fb_user = $facebook->getUser();//$_REQUEST[‘userid’],$_REQUEST[‘sessionKey’]);
$photo = $facebook->api($_REQUEST[‘facebook_photo_id’]);
} catch (Exception $e) {
$out[‘error’] = “Could not log you in to Facebook… please clear your cache and try again.”;
die(json_encode($out));
}

//
// Record the import to avoid creating duplicates later
//
$d = array(“facebook_photo_id”=>$_REQUEST[‘facebook_photo_id’],
“google_album_id”=>$albumId);
file_put_contents(‘temp/batch_photo_ids/’.$_REQUEST[‘facebook_photo_id’],json_encode($d));

//
// Get the Facebook photo into a temporary file path
//
$fn = “temp/”.$photo[‘id’].”.jpg”;
$url = ”;
for($x=0; $x<sizeof($photo[‘images’]); $x++)
{
if(isset($photo[‘images’][$x][‘source’]))
{
$url = $photo[‘images’][$x][‘source’];
break;
}
}
$data = @file_get_contents($url.””);
if($data==null && sizeof($data)<=0)
{
die(json_encode($out));
return;
}
if(!file_put_contents($fn,$data))
{
die(json_encode($out));
return;
}

//
// Upload the photo to Picasa
//

try {
$fd = $photos->newMediaFileSource($fn);
$fd->setContentType(‘image/jpeg’);

$entry = new Zend_Gdata_Photos_PhotoEntry();
$entry->setMediaSource($fd);
$entry->setTitle($photos->newTitle($photo[“id”]));
if(isset($photo[“name”]))
$entry->setSummary($photos->newSummary(isset($photo[“name”])?$photo[“name”]:”?”));

$albumQuery = new Zend_Gdata_Photos_AlbumQuery;
$albumQuery->setUser($user);
$albumQuery->setAlbumId($albumId);

$albumEntry = $photos->getAlbumEntry($albumQuery);

$result = $photos->insertPhotoEntry($entry, $albumEntry);
$out[‘success’] = $result ? true : false;
}
catch (Exception $e) {
$out[‘error’] = “There was an exception when trying to upload to Picasa… please clear your cache and try again.”;
die(json_encode($out));
}
if(!$out[‘success’])
{
$out[‘error’] = ‘The photo could not be uploaded to Google for an unknown reason’;
}

//
// Cleanup
//
unlink($fn);
die(json_encode($out));[/code]

Finally, free memory looks like this

[CODE]total used free shared buffers cached
Mem: 4040 3854 185 0 4 56
-/+ buffers/cache: 3794 246
Swap: 2047 553 1493[/CODE]

My host has stated:
[I][INDENT]Your server had quite a bit of iowait just before it became unresponsive. It looks like there was quite a bit of php processes with this one having 392 instances:

/usr/bin/php /home/zane/public_html/startgoogleplus/api/import_photo.php

Please let us know if we may further assist you.[/INDENT][/I]

On a (possibly) related note…
I upgraded from a shared to a dedicated hosting package days ago, which involved repointing the nameservers of the domain of course. It worked fine after a bit, but for the last 18 hours since this started my computer is connecting to the old nameservers and thus the old host again! Others do not seem to be having this problem, just me… but it is making testing damn near impossible!
The domain is StartGooglePlus.com
Old NSs are ns615.hostgator.com / ns616.hostgator.com
New NSs are ns1.inZania.com / ns2.inZania.com

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @ZaneClaes spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...