Periodic task executed on an MySQL table

Web hosting discussion, programming, and shared and dedicated servers.
5 posts Page 1 of 1
by rky » Thu Jun 15, 2017 5:53 pm
I have a database with a table of "Notifications" populated by a .php page located in a sub-directory of a domain I have hosted by Inserting, updating, deleting etc of this Notifications table is working fine. Each Notification has a expiration DateTime field, and a Y/N field indicating whether or not it has been acknowledged. Now I need to have a task that periodically (every 3 to 5 minutes or so), checks if a Notification has either expired, or been acknowledged, and if not tries to send it again to the appropriate device(s). I was first going to do this with an EvTimer from PHP, and there would be one timer for each notification, and would exit after the Notification expires or is acknowledged. Unfortunately the extension is not available by default, I read the Sonic wiki about using a custom php.ini, and I built the ev extension from source, and tried to load it from a custom php.ini, but when I put the .htaccess file in the sub directory of the server (where the .php pages that updates the Notifications table is I could not access any pages in that directory.

I then thought having a cron jop run a PHP file and iterate over the Notifications table and re-send any that had not expired or been acknowledged, but I found that PHP files run from a shell terminal (as I assume would be the case when using a cron job), do not have MySQL, which kills what I'm trying to do.


I was also confused by some of the instructions in the wiki:

It says to make the symbolic link to PHP5 in "~/public_cgi/php5"
and to copy the php.ini file to the same directory.
(I did not have a "public_cgi" directory in my home directory so I made one,
I also tried creating the link, and copying the php.ini file to the WWW_Pages
of my home directory neither of these worked)

Then the instructions say " In your .htaccess file replace the reference to /cgi-bin/php5 to be the one you just created in your own cgi directory, i.e."

and gives the following example:

AddHandler php-cgi .php
Action php-cgi /cgi-bin/USERNAME/php5

I tried replacing "cgi-bin" with "public_cgi" and "WWW_Pages" neither of which worked.

Soooo .... I either need some more specific/correct examples of how to use a custom php.ini (asuming I can load a custom extension ( that way), or a way I can run a php file which can execute mysqli commands from a cron job.

Robert Young
by joemuller » Fri Jun 16, 2017 7:07 pm
There's a much easier way to make changes to the existing php.ini if you're using version 5.3 or later - .user.ini. ( ... r-user.php)

Drop a new file named .user.ini in the main (root) directory for your site, then add any options you'd like using the same syntax/formatting as php.ini. With the exception of a few system-level settings, they'll override the default settings on our servers.

In general, I recommend keeping the PHP handler bit in the top of your .htaccess like so:

Code: Select all

Action php-cgi /cgi-bin/php55
AddHandler php-cgi .php

The old linking of things to the CGI directory was only needed on old versions of PHP (5.2 and older). I'll make sure our team updates the documentation to include the .user.ini method.
I'm a proud employee of! :-)
by joemuller » Fri Jun 16, 2017 7:11 pm
Adding new extensions isn't going to work on our web server, mostly for security reasons. That said, you're welcome to use *crontab* on to launch your PHP script at regular intervals.

Use the following so your code is run using PHP 5.3 rather than the default interpreter:

Code: Select all

I'm a proud employee of! :-)
by rky » Mon Jun 19, 2017 2:15 pm
Okay, I put a user.ini file in the base directory, and you are correct it will not load an extension. So I guess loading an EVTimer is just not possible. So I opened a terminal, to, changed to the base directory {/nfs/WWW_pages/rky/ and tried to run my PHP script with the command:
php -f myfile.php

I received the following error:
PHP Fatal error: Cannot instantiate non-existent class: mysqli in /nfs/WWW_pages/rky/ on line 11

As indicated by the title of this post, I need to perform a task periodically on a MySQL table, being able to periodically run a task via a cron job that can't access a MySQL database isn't of much help. This is a job I'm being paid for by a client, I need to solve this problem, and I've done everything I know to do, please help !
by joemuller » Mon Jun 19, 2017 2:34 pm
The mysqli error is because the default PHP interpreter on the shell server is PHP 4.4.7. You need to be running PHP 5 or later to use mysqli - try giving the full path for now (this can be omitted by adding the directory to your $PATH, but crontab still needs the full path specified):

Code: Select all

/opt/php53/bin/php -f FILENAME.PHP

You shouldn't need to manually load extensions - they're already enabled. (See phpinfo() for details on what's available as well as if your overrides are in effect.) Make sure your user.ini file has a leading dot (.) like so:

Code: Select all

I'm a proud employee of! :-)
5 posts Page 1 of 1

Who is online

In total there are 7 users online :: 0 registered, 0 hidden and 7 guests (based on users active over the past 5 minutes)
Most users ever online was 999 on Mon May 10, 2021 1:02 am

Users browsing this forum: No registered users and 7 guests