Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass arguments to Linux daemon/service

Tags:

c

linux

daemon

I have created a Linux daemon (in C language) to send certain information via UDP to another computer. It requires of course the remote IP address and the port number. I store this daemon in /usr/local/bin/ and I also created a script in /etc/init.d/ to start|stop|restart the daemon.

So far, the IP address and the port number are passed to the daemon, directly by the script. By example, the start() part of the script looks like this:

start() {
  /usr/local/bin/lvsload_udp_s 192.168.122.25 47239
}

So, when the remote IP and/or port number changes, I have to modify my script, instead of modifying some configuration file. It is a bad practice, I know.

What is the best way of passing the arguments to my daemon? Thanks

like image 502
marcocamejo Avatar asked Jul 04 '12 22:07

marcocamejo


1 Answers

Why do you think command line parameters are bad?

Configuration files are additional work, since you need to parse them. And going with your example, modifying a configuration file = modifying one file. Modifying a script = modifying one file. Doesn't seem like much of a difference, when you only have a small number of arguments. You can even stick the parameters into variables at the top of the script, which makes it almost like a config file :-) Some scripts even source such "variable setting scripts" so it really looks like a configuration file.

If you can find a reason why command line parameters are bad, then there's a good chance that you'll also have an idea what to use instead. If, on the other hand, you can't even explain why the command line parameters are bad, then there's probably nothing wrong with using them...

like image 59
Christian Stieber Avatar answered Sep 22 '22 11:09

Christian Stieber