Is it possible to pass input to a running service or daemon?
On Linux, all running processes have a special directory under /proc
containing information and hooks into the process. Each subdirectory of /proc
is the PID of a running process. So if you know the PID of a particular process you can get information about it. E.g.:
$ sleep 100 & ls /proc/$!
...
cmdline
...
cwd
environ
exe
fd
fdinfo
...
status
...
Of note is the fd
directory, which contains all the file descriptors associated with the process. 0
, 1
, and 2
exist for (almost?) all processes, and 0
is the default stdin. So writing to /proc/$PID/fd/0
will write to that process' stdin.
A more robust alternative is to set up a named pipe connected to your process' stdin; then you can write to that pipe and the process will read it without needing to rely on the /proc
file system.
See also Writing to stdin of background process on ServerFault.
Pass and parse arguments to running/online systemd daemon
As Vollfeiw suggested in their comment on your question, you really probably want to make your own interface within your application for doing this.
I really don't think this is basically ever a good design choice for updating internal state to an application - but if you are extremely intent on being able to use the systemctl
utility to update the state of your program, one mechanism you could take advantage of is UNIX signalling.
- implement a signal handler within your program (ways of doing this vary by language and framework, if you're not familiar go find some tutorials). On UNIX, there are two signals reserved for you to use however you see fit - SIGUSR1 and SIGUSR2. To use any other signals would also work but would violate conventions, so you probably shouldn't do it.
- thereafter, you can use the
--signal
flag to systemctl to alert the program that it should do something - UNIX signals can't carry payloads, so you are going to have to get the program to know how to load the data it wants some other way (eg write to a file prior to sending the signal, maybe).
Honestly, at this point you are basically at functionally the same level of complexity as exposing a web socket or something like that. Not to beat a dead horse, but you should therefore probably just bite the bullet and make a web interface. :) BUT you are technically able to get what you want done this way, too.
If you want to be able to do everything locally and not have people use curl or a browser or whatever to do their configuration, you could write your own CLI client for managing your daemon, for which you'd probably want to use UNIX sockets (as opposed to TCP or UDP sockets for example) as your fundamental communication technique. This is a pattern in a few daemon projects out there (seeing the daemon part of a service ending with "d," for "daemon", and then the client part of the service, which is for controlling the daemon, ending with "c" or "ctl," for "control". One example of a service which does this is the NTP service chrony, which uses chronyd to manage your computer's timekeeping and provides a chronyc CLI interface to the user for telling chronyd to do things differently.
Most programmers are going to find it easier to expose a web interface.
how to pass arguments to Linux daemon/service
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...
Java Command Line when daemon is running
No, you can't modify the arguments passed after the application started.
The array used to retreive the parameters is populated when it starts and cannot be altered.
If the application is a server, you should be able to implement a CLI rather easily with a simple thread waiting for input.
services.sh: line 2: $'\r': command not found services.sh: line 74: syntax error: unexpected end of file
you have $1 in your script but when you run script you do not have any input to your script
Receive arguments in php script running as service
So as far as I can tell this isn't really possible the way I was hoping. The 2 solutions I can use are setting an outside variable (in the DB or flat file) and checking for it periodically, or since this is actually listening for a UDP broadcast I can just send out a UDP with the information I would like to pass to the script. While neither of these are particularly elegant, they both work.
On a side note, don't build UDP servers in PHP. This was really just a proof of concept, but is horribly inefficient. While using php to handle the data in the broadcast seems to work well, it is definitely preferable to use Python or Java for the listener itself and pass the data to a script.
Related Topics
Ldd Shows Varied Addresses on X86 Linux
What Is The Maximum Number of Subdirectories Allowed in Ext4
How to Log Messages from an ASP.NET Core Application to a Specific File on Linux
Swift on Linux: Make Very First Step Work
How I Install Specific Fonts on My Aws Ec2 Instance
Can Perf-Stat Results Be Generated from a Perf.Data File
Tar Command Not Found in Dockerfile
How to Remove Warning: Link.Res Contains Output Sections; Did You Forget -T
Why Doesn't Time() from Time.H Have a Syscall to Sys_Time
How to Create a Core File for My Crashed Program
Command-Line Fulltext Indexing
Drop/Rewrite/Generate Keyboard Events Under Linux
How to Use Ptrace(2) to Change Behaviour of Syscalls
Unix Cut Except Last Two Tokens
Pyinstaller on 32-Bit Linux - Importerror: The 'six' Package Is Required