go-daemon/README.md

134 lines
5.7 KiB
Markdown
Raw Normal View History

2023-07-08 13:51:35 +03:00
# go-daemon
2023-07-24 16:44:56 +03:00
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
2023-07-26 10:26:28 +03:00
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/)
2023-07-24 16:44:56 +03:00
<br>
---
<br>
2023-07-29 13:06:36 +03:00
### Go-Daemon
Dependencies `git, go`
Debian/Pardus
```bash
2023-07-30 14:27:59 +03:00
sudo apt install git curl
sudo apt remove golang-go
2023-07-29 13:06:36 +03:00
```
2023-07-29 14:39:47 +03:00
Install latest version of Go !
2023-07-30 14:24:33 +03:00
```bash
# sudo apt update
GOVERSION=$(curl https://go.dev/VERSION?m=text)
wget "https://dl.google.com/go/$GOVERSION.linux-amd64.tar.gz"
sudo tar -C /usr/local -xzf "$GOVERSION.linux-amd64.tar.gz"
cd
echo "export PATH=\$PATH:/usr/local/go/bin" >> .bashrc
source .bashrc
go version
```
2023-07-29 13:06:36 +03:00
Build / Install with:
```bash
2023-07-29 14:59:14 +03:00
wget -qO- https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/raw/branch/main/go-daemon/install.sh | bash
2023-07-29 13:06:36 +03:00
```
2023-07-26 15:38:52 +03:00
### C-Daemon
Dependencies `git, go, gcc`
Debian/Pardus
2023-07-26 15:39:48 +03:00
```bash
sudo apt install git golang-go gcc
```
2023-07-26 15:38:52 +03:00
2023-07-27 13:54:15 +03:00
Build / Install with:
2023-07-29 11:37:34 +03:00
```bash
2023-07-29 14:59:14 +03:00
wget -qO- https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/raw/branch/main/c-daemon/tmp-install.sh | bash
2023-07-29 11:37:34 +03:00
```
2023-07-27 13:54:15 +03:00
2023-07-29 13:06:36 +03:00
2023-07-27 13:54:15 +03:00
<!-- How to **build** c-daemon + go processes:
2023-07-26 13:52:50 +03:00
```bash
git clone https://git.aliberksandikci.com.tr/liderahenk/go-daemon
cd go-daemon/
2023-07-26 14:19:15 +03:00
go build -o c-daemon/bin/ c-daemon/go-files/info.go
2023-07-26 14:09:38 +03:00
gcc -o c-daemon/bin/test c-daemon/daemon/test.c c-daemon/daemon/daemon.c
2023-07-26 15:29:40 +03:00
sudo ln -sv "$(pwd)"/c-daemon/bin/info /info
2023-07-26 14:16:15 +03:00
```
How to **start** c-daemon + go processes:
```bash
2023-07-26 14:22:39 +03:00
cd c-daemon/bin/
./test
2023-07-27 13:54:15 +03:00
``` -->
2023-07-26 13:52:50 +03:00
<br>
---
<br>
<details><summary>Notes</summary>
2023-07-24 16:44:56 +03:00
### 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
2023-07-26 10:26:28 +03:00
- 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
2023-07-26 10:38:37 +03:00
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>
2023-07-26 11:09:03 +03:00
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!
2023-07-26 10:38:37 +03:00
2023-07-26 11:09:03 +03:00
After compiling our both c files (https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/21df9b77fac68aeae40b09df7f9f5e0135f31e0d/c-daemon/daemon.c, https://git.aliberksandikci.com.tr/Liderahenk/go-daemon/src/commit/21df9b77fac68aeae40b09df7f9f5e0135f31e0d/c-daemon/test.c) with command `gcc -o test test.c daemon.c` run code with `./test` and do NOT enter any other key.
Examine the status before _daemon starts with `ps -C test -o "pid ppid pgid sid tty stat command"` command and you will see PID, Parent PID, STAT and other useful info.
STAT field, you see that your process is running but waiting for an off-schedule event to occur which will cause it to run in the **foreground** (we don't want this in daemons)
| Abbreviation | Meaning |
| ------------ | ------- |
| `S` | Waiting asleep for an event to happen, `interruptible sleep (waiting for an event to complete)` |
| `T` | `stopped, either by a job control signal or because it is being traced.` |
| `s` | `is a session leader` |
| `l` | `is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)` |
| `+` | `is in the foreground process group`|
| `<` | `high-priority (not nice to other users)` |
run `man ps` for more info.
You will see that our process is now a member of the foreground process group `S+`. and parent process is probably a shell (bash,zsh,fish etc.) See parent process with command `ps -jp PPID` (replace PPID with parent process id you get above).
Now return to the terminal where you are running your application (./test) and press **Enter** to invoke the **_daemon** function. Then look at the process information on the other terminal again.
2023-07-26 11:31:34 +03:00
First of all, you can say that the new subprocess is running in the **background** since you do not see the **+** character in the **STAT** field. You can now see that the parent process of your process is the systemd process (or other init in use)
2023-07-26 13:52:50 +03:00
I'm too lazy to continue this readme(about c-daemon). just continue in [original website](https://www.makeuseof.com/create-daemons-on-linux/) or just review last versions of c files
</detailsc>