Page 1 of 1

Periodic task executed on an MySQL table

Posted: Thu Jun 15, 2017 5:53 pm
by rky
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 Sonic.net. 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 ev.so 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 (ev.so) that way), or a way I can run a php file which can execute mysqli commands from a cron job.

Thanks,
Robert Young

Re: Periodic task executed on an MySQL table

Posted: Fri Jun 16, 2017 7:07 pm
by joemuller
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. (http://php.net/manual/en/configuration. ... 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.

Re: Periodic task executed on an MySQL table

Posted: Fri Jun 16, 2017 7:11 pm
by joemuller
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 shell.sonic.net 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

/opt/php53/bin/php FILENAME_WITH_FULL_PATH_GOES_HERE

Re: Periodic task executed on an MySQL table

Posted: Mon Jun 19, 2017 2:15 pm
by rky
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 shell.sonic.net, changed to the base directory {/nfs/WWW_pages/rky/mydomain.com/test/rpi_ht 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/mydomain.com/test/rpi_ht/Connect.php 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 !

Re: Periodic task executed on an MySQL table

Posted: Mon Jun 19, 2017 2:34 pm
by joemuller
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

.user.ini