RootPhoenix
RootPhoenix

Reputation: 1767

IPC notify a process to change parameters

I have created a daemon that copies data from source to destination directory.

I have named it cpd (copy daemon). It periodically runs this cp command: cp src dest

But if I need to change the interval of this cp command execution, how should I communicate with the cpd daemon?

For example: cpd -p 120 --> where -p indicates period and 120 is in seconds.

PS: "I know how to start a background process: create a child and exit from parent process, set new session id, close inherited standard file descriptors, change working directory. The standard steps to create a daemon. I am also using syslog to log status messages after each command are executed."

Reference for daemon creation

@Paul: So i need a config file like this one below. and when I run without -d option I should signal the daemon to read this file and change its variables, behaviour accordingly. Is that right?

# Example configuration file for cpd - An test Linux daemon.
# Comments start with a # and are ignored.
# Configuration options are delimited by = and ;
# Example:
#    arg=val;
verbose_logging_enabled=true;
daemon_enabled=false;
config_file_path=/etc/cpd.conf;
source_path=/home/Documents/Source;
destination_path=/home/Documents/Destination;

Upvotes: 4

Views: 938

Answers (3)

xpa1492
xpa1492

Reputation: 1973

You could have your process listen for SIGUSR1 and SIGUSR2 (user-defined signals). On SIGUSR1 it increments the copy interval by some predefined value and on SIGUSR2 it decrements it. You do this easily:

//call at startup
signal(SIGUSR1, SIGUSR1Handler);
signal(SIGUSR2, SIGUSR2Handler);

//handlers:
void  SIGUSR1Handler(int sig)
{
    //increment copy interval
    //maybe print a message with the new value
}

void  SIGUSR2Handler(int sig)
{
    //increment copy interval
    //maybe print a message with the new value
}

This is really no different than what others have already mentioned in terms of how you can communicate with your daemon process, but adds the little practical side of not needing any external configuration.

Upvotes: 1

Norman Gray
Norman Gray

Reputation: 12514

It depends how much you want to communicate to the running daemon.

If you want to have a whole conversation, then you're going to have to think about sockets and listeners and protocols and all. That's quite a bit of work.

Your requirement sounds quite simple, though. A conventional thing to do in this case is to have your daemon install a signal handler for HUP (see sigaction or its equivalent on your unix flavour). When the program receives that signal, the handler simply re-reads a configuration file (or rather does something which causes the daemon to re-read that in some way). Thus the sequence is:

% vi .../my-daemon.config
% kill -HUP <daemon-pid>

It's common to have the daemon write its PID on startup to a file in /var/run, so that the second line would be

% kill -HUP `cat /var/run/mydaemon.pid`

If you want to be fancy and automate that, then you could add some option to the daemon which makes the configuration changes and then sends the signal (see kill(2)).

A further point is that the signals USR1 and USR2 are intended for this sort of communication. If you install signal handlers for HUP, USR1 and USR2, then that's three different types of poke you can administer to your daemon. That might be all you need.

Upvotes: 4

Humpity
Humpity

Reputation: 153

If your daemon has nothing to do most of the time, why not just get it to do a 'blocking' read () ?. i.e just wait until a file is filled with a command and then read it. The command line can just be a script that writes to the command-file.

Upvotes: 0

Related Questions