Click here to get back home

do static variables in cron jobs cause any problems, like memory leaks?

 HomeNewsGroups | Search | About
 comp.lang.php    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
do static variables in cron jobs cause any problems, like memory leaks? Lawrence Krubner 06-02-2008
Get Chitika Premium
Posted by Lawrence Krubner on June 2, 2008, 3:50 pm
Please log in for more thread options

Do any problems come up when using a static variable in a cron job?
Assuming the cron job is called every 5 minutes for one year. Assume
I've got an array that stores the names of which users are logged in to
a particular website at the current moment. Would it cause me trouble if
I made this array static? If it would cause trouble, what's the easiest
way to persist the data? I could use a database, but that seems heavy
handed for a 20 line cron script (mostly because I'd have to create a
database table).





Posted by Jerry Stuckle on June 2, 2008, 3:57 pm
Please log in for more thread options
Lawrence Krubner wrote:
>
> Do any problems come up when using a static variable in a cron job?
> Assuming the cron job is called every 5 minutes for one year. Assume
> I've got an array that stores the names of which users are logged in to
> a particular website at the current moment. Would it cause me trouble if
> I made this array static? If it would cause trouble, what's the easiest
> way to persist the data? I could use a database, but that seems heavy
> handed for a 20 line cron script (mostly because I'd have to create a
> database table).
>
>
>
>

Static variables are still only available to the script in which they
are static. You can't share them across processes (different cron jobs).

If you need persistent data, you need to store it outside of the script,
i.e. in a database.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Posted by Rik Wasmus on June 2, 2008, 3:59 pm
Please log in for more thread options
On Mon, 02 Jun 2008 21:50:37 +0200, Lawrence Krubner
> Do any problems come up when using a static variable in a cron job?
> Assuming the cron job is called every 5 minutes for one year. Assume
> I've got an array that stores the names of which users are logged in to
> a particular website at the current moment. Would it cause me trouble if
> I made this array static? If it would cause trouble, what's the easiest
> way to persist the data? I could use a database, but that seems heavy
> handed for a 20 line cron script (mostly because I'd have to create a
> database table).

Static in PHP does not really have that meaning. It will do very little
except be more reachable across scopes. It will still be as gone as any
normal variable as soon as the script ends, and every instance of the
running script will have it's own static variable, not linked to any of
the others.

A database seems logical, if you have no real database server available or
you think it's overkill, you could choose SQLite. For more daemon like
qualities, with fleeting, very fast changing data, but accessable across
the board, I've had much fun with shmop. Don't think that's what you want
though.
--
Rik Wasmus
...spamrun finished

Posted by C. (http://symcbean.blogspot.c on June 3, 2008, 7:19 am
Please log in for more thread options
> Do any problems come up when using a static variable in a cron job?
> Assuming the cron job is called every 5 minutes for one year. Assume
> I've got an array that stores the names of which users are logged in to
> a particular website at the current moment. Would it cause me trouble if
> I made this array static? If it would cause trouble, what's the easiest
> way to persist the data? I could use a database, but that seems heavy
> handed for a 20 line cron script (mostly because I'd have to create a
> database table).

Counting the users 'logged in to a ... website' is not nearly as
simple as it sounds - I find it hard to believe you do it in 20 lines
of code. The fastest route to the goal would be to write your session
handler using a DBMS backend.

C.

Posted by Lawrence Krubner on June 5, 2008, 4:27 pm
Please log in for more thread options
>> Do any problems come up when using a static variable in a cron job?
>> Assuming the cron job is called every 5 minutes for one year. Assume
>> I've got an array that stores the names of which users are logged in to
>> a particular website at the current moment. Would it cause me trouble if
>> I made this array static? If it would cause trouble, what's the easiest
>> way to persist the data? I could use a database, but that seems heavy
>> handed for a 20 line cron script (mostly because I'd have to create a
>> database table).
>
> Counting the users 'logged in to a ... website' is not nearly as
> simple as it sounds - I find it hard to believe you do it in 20 lines
> of code. The fastest route to the goal would be to write your session
> handler using a DBMS backend.


Sorry, I was simplifying somewhat. I'm actually counting all users who
are logged into an Ajax-driven chat room. I decided to build an array of
the usernames, then serialize() the array, then store it as a file,
then, next time the script was called, open the file and unserialize()
the array. The cron is merely for administrative purposes, to alert the
site owner when someone new has entered the chat room.

The script that gets the usernames and serializes the array and stores
it as a file is 30 lines of code, the script that sends the alert to the
site owner is 43 lines of code.






Similar ThreadsPosted
Testing For Memory Leaks March 18, 2008, 5:19 pm
PHP and cron jobs March 14, 2006, 12:03 pm
Cron Jobs March 18, 2006, 8:41 pm
cron jobs May 17, 2007, 5:13 pm
Cron jobs / tabs / tasks and more December 28, 2004, 5:27 pm
Cron jobs / tabs / tasks and more December 29, 2004, 4:03 am
Multiple jobs from single cron. September 23, 2006, 5:08 pm
Re: my cron jobs can not use SELECT INTO OUTFILE syntax? February 8, 2008, 8:24 am
Re: my cron jobs can not use SELECT INTO OUTFILE syntax? February 8, 2008, 11:21 am
Cron jobs alternative, Yahoo Web Hosting May 19, 2008, 11:18 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap