44 lines
2.4 KiB
Markdown
44 lines
2.4 KiB
Markdown
# go-daemon
|
|
|
|
A Linux daemon runs continuously written in golang. Refer to [ahenk-docs/dev](https://git.aliberksandikci.com.tr/Liderahenk/ahenk-docs/src/branch/main/dev/resources.md#daemon-linux) for resources
|
|
|
|
This repo will be include two separate daemons. One of them is written with C language for understanding native daemon processes [c-daemon](c-daemon/) . Other one is written in go for implement same deamon concepts to go language and using with native performance [go-daemon](go-daemon/)
|
|
<br>
|
|
|
|
---
|
|
|
|
<br>
|
|
|
|
|
|
### Summary - [MakeUseOf](https://www.makeuseof.com/create-daemons-on-linux/)
|
|
|
|
Usually daemons start on system startup and run continuously until the system shuts down. They do NOT send messages to the console or screen in any way.
|
|
|
|
It is not mandatory but daemon processes are usually named to end with the letter **d**
|
|
|
|
- Initial operations, such as reading configuration files or obtaining necessary system resources, must be performed before the process becomes a daemon
|
|
|
|
- A background running process is created with init as its parent process. For this purpose, a sub-process is forked from the init process first, and then the upper process is terminated with exit
|
|
|
|
- A new session should open by calling the setsid function, and the process should be disconnected from the terminal
|
|
|
|
- All open file descriptors inherited from the parent process are closed
|
|
|
|
- Standard input, output, and error messages are redirected to `/dev/null`
|
|
|
|
- The working directory of the process must change
|
|
|
|
The operating system groups processes into session and process groups. Each session consists of process groups.
|
|
|
|
Processes receive their inputs and send their outputs to **controlling terminal**. A controlling terminal is associated with only **one** session at a time
|
|
|
|
A session and process groups in it have identification (**ID**) numbers; these identification numbers are the process identification numbers (**PID**) of the session and process group leaders. A child process shares the same group as its parent process
|
|
|
|
<br>
|
|
|
|
#### Creating a Daemon Process
|
|
<sub>c-daemon</sub>
|
|
|
|
To create a demon process, we need a background process whose parent process is init. In the code [_daemon](https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/ca61ee4ac26ae3855d41581baba2b44dbabc0cd3/c-daemon/daemon.c#L10) creates a child process and then kils the parent process. In this case, our new process will be a subprocess of init and will continue to run in background!
|
|
|