Executing daemon process on server - c++

I have one deamon process which listen to the request from user and respond back.
While working on local system I execute it on terminal ./daemon. When user make request ./client from php page(executed by shell_exex() command) daemon process respond which some results. This is ok.
now I want to place this on ftp server. Php page whichc execute daemon process on button click event.
How could I make daemon process to keep listening on server continously? see daemon is c++ executable file.
One this is everytime I first execute shell_exec(daemon) but then purpose is lost. I want some way daemon process continously keep listening for the request!

Use daemon(), it does exactly what you want.
If this function is not available on your system, take a look at this tutorial which explains you how to rewrite the function.

Related

sending IP address of machine to server

I need to send a simple request to one of my server on defined intervals, let's say one every 2 seconds, in order to tell my server my machine's IP address(since I've dynamic one). I'm currently doing it in a while loop with a delay for std::system call for curl command with --silent option and redirecting rest to /dev/null. Something like this -
curl -s 'http://example.com' > /dev/null
The server currently parses the request and finds out the required IP address from it. Is there any other way to do this?
Another alternative would be sending a simple UDP datagram packet. The server can obtain the sender's address, upon receipt, equally well.
That requires a little bit less overhead than establishing a TCP connection. Of course, UDP offers no guarantee of deliverability, and an occasional UDP datagram would be lost; but since you're pinging the server regularly that should be fine. More importantly, however, is that a UDP sender's IP address is trivially forged. Whether or not this is an issue for your application is something that only you can determine.
If you're going to stick with TCP, one thing you can do is to establish a socket connection yourself. Let's examine what executing system(), just for the purpose of having curl do a dummy connection, involves:
forking a new child process.
the child process executing /bin/bash, passing it the command to parse.
Bash reading $HOME/.bashrc, and executing any commands in there. If your shell is something other than bash, the shell will have its own equivalent of a startup script to execute.
The shell forking a new child process.
The child process executing curl.
The loader finding all of the libraries that curl requires, and opening them.
curl now executes. Only then, after all this work, there will be code running to open a socket and attempt to connect to the remote host.
Steps 1 through 6 can be trivially skipped, simply by creating a socket, and connecting, yourself. It's not rocket science. socket(), connect(), close(), that's it. This task does not require HTTP. The server process only needs to socket(), bind(), listen(), and accept(), and then obtain the IP address from the connection. Done.

Developing a Mac OS Daemon and IPC

I have developed a dummy Launch Daemon that keeps writing something to the console(syslog) every 5 minutes. Now, I want to write an application that can communicate with this service. By communicating I mean that the user should be able to input the logging frequency(time). For eg, if the service is logging 'Hello world' every 5 minutes, the user should be able to change it to something else (say 2 mins) and the change should be reflected. Any idea on how I should proceed for developing the application and facilitate interprocess communication between the daemon and the application? Thanks.
There are several ways:
Have a config file for your application that contains the logging frequency and any other parameters you need. The daemon then parses the file on startup to get its parameters. The daemon also creates a SIGHUP handler, and when it receives a SIGHUP it re-reads the values from the config file. The part that the user interacts with then just gets new parameters from the user, edits them into the config file and sends a kill -HUP to the daemon's process id.
The daemon creates a second thread that creates a socket and listens for new parameters, when any arrive, the thread updates variables shared with its main thread which then continues with the new values. The part that interacts with the user then asks the user for new parameters and sends them to the agreed port - you can use nc or netcat to get started and then later code it in C++.

Django + mod_wsgi + apache2 - child process XXX still did not exit, sending a SIGTERM

I am getting intermittent errors -
child process XXX still did not exit, sending a SIGTERM.. and then a SIGKILL. It occurs intermittently and the web page hangs.
I was not using Daemon process..but now I am, still the problem exists..
Also I have some Error opening file for reading: Permission Denied.
Please can someone help?
I am new to this forum, so sorry if that has been answered before.
If you were not using daemon mode of mod_wsgi, that would imply that Apache must have been restarted at the time that initial message was displayed.
What is occurring is that in trying to do a restart, Apache sends a SIGTERM to its child processes. If they do not exit by their own accord it will send SIGTERM again at 1 second intervals and finally send it a SIGKILL after 3 seconds. The message is warning you of the latter and that it force killed the process.
The issue now is what is causing the process to not shutdown promptly. There could be various reasons for this.
Using an extension module for Python which doesn't work in sub interpreters properly which is deadlocking and hanging the process, preventing it from shutting down. http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Python_Simplified_GIL_State_API
Use of background threads in the Python web application which have not been set as being daemon threads properly with the result they are then blocking process shutdown.
Your web application is simply trying to do too much on process shutdown somehow and not completing within the time limit.
Even if using daemon mode you will likely see this behaviour as it implements a similar shutdown timeout, albeit that the timeout is configurable for daemon mode.
Anyway, force use of the main Python interpreter as explained in the documentation link above
As to the permissions issue, read:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Access_Rights_Of_Apache_User
http://code.google.com/p/modwsgi/wiki/ApplicationIssues#Application_Working_Directory
In short, ensure access permissions are correct of files/directories you need to access and ensure you are always using absolute path names when accessing the file system.

Controlling Gunicorn in a new ssh session

I am using Gunicorn to power Django application on a remote server (ubuntu), to which I connect by ssh. Once Gunicorn has started the status log pops up showing you what is going on and such. However when I close my ssh session and reconnect later on I cant seem to reopen the process without killing Gunicorn and rebooting the server.
Not sure if i understand your issue correctly...
When running django/gunicorn usually it is helpful to use some tools to control the processes. One really good option to do so is the use of supervisord:
http://docs.gunicorn.org/en/latest/deploy.html#supervisor
If you just want to run processes directly and being able to (dis-)connect - generally screen is a good option.
It allows you to to disconnect an ssh-session while leaving your 'virtual?' terminals running.
Just re-ssh to your server and reconnect using:
screen -xr

API to control Linux daemon

What I need is the possibility to control a Linux daemon though some sort of API, for example check if a certain daemon is running, start/stop/restart it, etc.
Is there any Linux library that provides this functionality?
You could also use D-Bus or SNMP. However, most daemons just write their PID to some file under /var/run/ and accept the SIGTERM signal to stop, and the SIGHUP signal to reload their configuration files (usually under /etc/).
Notice that if you adopt the usual convention that your daemon program mydprog is writing its pid in /var/run/mydprog.pid some other program could read that pid there and check, using kill(2) with a 0 signal, that the daemon process is running. You might also access to some pseudo-files under /proc/1234/ (where 1234 is the daemon's pid), notably /proc/1234/status, see proc(5) for more.
You can also design your daemon so that it answers, e.g. using some JSONRPC protocol on some unix(7) or tcp(7) socket, to some queries by giving status information. You might consider using some HTTP protocol thru some HTTP server library like libonion, or any other message passing or remote procedure call protocol.
short answer is no.
Some daemons might have an api but that will be specific for that daemon.
You can run /etc/init.d/<daemon_name> start|stop|status to start stop or get the status most daemons