Andoni Martín
Andoni Martín

Reputation: 253

How can I reload a system daemon?

I need to reload Squid daemon, the solution is:

system("/etc/init.d/squid reload\n"); 

but I think that there must be a more efficient solution than using the "system" call, what C instruction should I use?

Thank you very much.

Upvotes: 2

Views: 1316

Answers (2)

thkala
thkala

Reputation: 86333

The absolutely fastest way to have Squid reload its configuration files would be to send a SIGHUP signal to the daemon using kill(). This is what squid -k reconfigure does, which in turn is what /etc/init.d/squid reload is most probably doing.

The problem with this approach is that you have to somehow discover the process ID of the squid daemon in your C code. The PID is usually stored in a text file somewhere under /var/run (/var/run/squid.pid in my case), that you can read-in - that saves you the hassle of looking through the process table, but it is still somewhat of a mess.

Considering that /etc/init.d/squid may also be performing custom operations and that you are not bound to reload the daemon every second or so, I'd say that you should go with your current solution. If you don't care about the return status of the script, you could also use the common fork() and exec() approach, which is asynchronous and, therefore, faster from your application's point of view.

Upvotes: 3

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27632

You can use fork and exec if you really need a faster solution, but since the squid init script has to be run, and Squid has to do the work, any optimizations of your C program will only give a really marginal improvement.

EDIT:

On the other hand (after having looked at the Squid manual), some daemons react to signals, and Squid seems to do so. For example, it re-read its configuration files if you send a HUP signal to it:

kill(process-id-of-the-squid-dameon, SIGHUP);

Upvotes: 2

Related Questions