here is what I want to do: I want to make an 'executable-like' application running on a web server, with a doSomething() function. On web application startup, the doSomething() function connects as a client to another MQTT server through web sockets, subscribes there, and stores the incoming messages to a database. I don't care about the content of the doSomethihg() function. I can do this. I am only interested in making a deamon app in a web server. This application will run either in glassfish (java) or apache (php). I will deploy this application in openshift o another similar PAAS, so it must be a web app, no a standalone one. Plus, I suppose I don't have the ability to execute shell scripts. I only found a way to do this with java; Make a servlet and configure it to run at application startup (use load-on-startup in web.xml file). I don't know if this is the most efficient, and I didn't find anything for php. Thanks in advance!
PHP doesn't have an 'application startup' per se, but it sounds like you may want a cron job. You can create a cron job to run every so many minutes, and it will launch its function without any user interaction. Setting a conditional flag in a cron job would ensure that the code only ever runs once.
Alternatively, you can execute a function whenever a particular page is loaded by a visitor. This function could set a flag (as a $_SESSION
variable) that checks whether or not the function has already run:
<?php
session_start();
if(!isset($_SESSION['done'])) {
doSomething();
}
function doSomething() {
// Do something
$_SESSION['done'] = 'yes';
}
?>
That way you can get the function to trigger once per visitor if need be.
Hope this helps! :)