Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3318456
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:42:29+00:00 2026-05-17T22:42:29+00:00

i have written a daemon to fetch some stuff from mysql and make some

  • 0

i have written a daemon to fetch some stuff from mysql and make some curl requests based on the info from mysql. since i’m fluent in php i’ve written this daemon in php using System_Daemon from pear.

this works fine but i’m curious about the best approach for connecting to mysql. feels weird to create a new mysql connection every couple of seconds, should i try a persistent connection? any other input? keeping potential memory leaks to a minimum is of the essence…

cleaned up the script, attached below. removed the mysql stuff for now, using a dummy array to keep this unbiased:

#!/usr/bin/php -q
<?php
require_once "System/Daemon.php";

System_Daemon::setOption("appName", "smsq");
System_Daemon::start();

$runningOkay = true;

while(!System_Daemon::isDying() && $runningOkay){

    $runningOkay = true;
    if (!$runningOkay) {
        System_Daemon::err('smsq() produced an error, '.
            'so this will be my last run');
    }

    $messages = get_outgoing();
    $messages = call_api($messages);
    #print_r($messages);

    System_Daemon::iterate(2);
}

System_Daemon::stop();  

function get_outgoing(){ # get 10 rows from a mysql table
    # dummycode, this should come from mysql
    for($i=0;$i<5;$i++){
        $message->msisdn = '070910507'.$i;
        $message->text = 'nr'.$i;
        $messages[] = $message;
        unset($message);
    }
    return $messages;
}

function call_api($messages=array()){
    if(count($messages)<=0){
        return false;
    }else{
        foreach($messages as $message){
            $message->curlhandle = curl_init();
            curl_setopt($message->curlhandle,CURLOPT_URL,'http://yadayada.com/date.php?text='.$message->text);
            curl_setopt($message->curlhandle,CURLOPT_HEADER,0);
            curl_setopt($message->curlhandle,CURLOPT_RETURNTRANSFER,1);
        }
        $mh = curl_multi_init();
        foreach($messages as $message){
            curl_multi_add_handle($mh,$message->curlhandle);
        }
        $running = null;
        do{
            curl_multi_exec($mh,$running);
         }while($running > 0);
        foreach($messages as $message){
            $message->api_response = curl_multi_getcontent($message->curlhandle);
            curl_multi_remove_handle($mh,$message->curlhandle);
            unset($message->curlhandle);
        }
        curl_multi_close($mh);
    }
    return $messages;
} 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:42:29+00:00Added an answer on May 17, 2026 at 10:42 pm

    Technically if it’s a daemon, it runs in background and doesn’t stop until you ask it to. There’s is no need to use a persistent connection in that case, and even, you probably shouldn’t. I’d expect the connection to close when I kill the daemon.

    Basically, you should open a connection on startup, and close it on shutdown, and that’s about it. However, you should put some error trapping in there in case the connection drops unexpectedly while the daemon is running, so it either shutdowns gracefully (by logging a connection drop somewhere) or have it retry a reconnection later.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long procedure I have written in node.js, but I'd like the
I have a small hobby project where I'm writing a 'wrapper'-daemon in linux, in
I have written few test scripts using JUnit 4 and Selenium. I have added
I have written a small Asp.net application using Entity Framework. A Stored Procedure accepts
I have written an application called Abacus and released it as free software. Now
I have written a Linq to CRM query using CRM 2011 RC (v5) LINQ-to-CRM
I have written following code for the client of RMI. But getting java.rmi.ConnectException: Connection
I have a gateway application that comes up with a login dialog and then
I am trying to write a simple chat server which sends commands from ajax
I have writen a fastcGI application using C and C++ I have a free

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.