[TOC]
- Title: How to Autostart Apps on Your Server
- Review Date: Fri, Apr 12, 2024
- url: -
How to Autostart Apps on Your Server
we can try systemd --user
modules
First of all, write the service in the ~/.config/systemd/user/
folder
|
|
Note that Type=forking
is important if you want to maintain the session, especially when you want to maintain a tmux
Understanding Type=forking
in systemd
In systemd, the Type=
option in the service unit file specifies the service’s process start-up behavior, influencing how systemd manages and tracks the service’s main process. For services that start by forking off a child process (which continues running after the parent exits), Type=forking
is the appropriate choice. This is a common pattern for traditional daemons that double-fork to detach themselves from the controlling terminal and run in the background.
When Type=forking
is set, systemd expects the service to:
- Start a parent process.
- Fork a child process from the parent.
- Have the parent process exit.
- Continue running in the background with the child process.
Systemd then tracks the child process as the main service process. This is useful for services that need to perform some initial setup in the parent process before running the service logic in the background.
Why Type=forking
Works with tmux
tmux
commands like tmux new-session -d
start a new session in detached mode and then immediately exit. This behavior is somewhat similar to forking, in that the command that starts the session exits quickly, but the session itself (and any processes running within it) continues in the background, independent of the shell it was started from.
Here’s why setting Type=forking
helps in your systemd unit file for tmux
:
- Initial Process Exit: The
tmux
command used inExecStart
exits after creating the detached session. WithType=simple
(the default ifType=
is not specified), systemd expects the service’s main process to stay active. When thetmux
command exits, systemd thinks the service has stopped. - Background Continuation: By using
Type=forking
, you signal to systemd that the exiting of the initial process (thetmux
command) is expected behavior and that it should consider the background continuation of the session as the service running successfully. Systemd then treats the service as active even after thetmux
command itself has exited, as long as there’s a background process associated with it.
Important Considerations
While Type=forking
helps keep the tmux
session active under systemd control, it’s important to note a few considerations:
- Process Tracking: Systemd might have difficulty tracking the exact process of the
tmux
session as the service’s main process sincetmux
doesn’t behave like a traditional daemon (it doesn’t provide a PID file for the child process). Systemd relies on the PID of the last background process, but in the case oftmux
, this is managed internally bytmux
. - Service Management: Actions like stopping or restarting the service rely on correctly identifying the process to manage. Ensure your
ExecStop
command correctly terminates thetmux
session.
Using Type=forking
in this way is a bit unconventional but can be effective for specific use cases like yours. It’s a clever solution to integrate tmux
session management within the systemd framework, especially when you need to ensure a tmux
session starts automatically and persists in the background.
Reload systemd daemon
|
|
Check the log
|
|
Manually start the service
systemctl --user restart mytorchserve.service
User systemctl and User runtime directory
Solution: Using sudo loginctl enable-linger $USER
makes systemd
keep the user’s systemd --user
process running even after the user logs out. This is crucial for running background services under that user without requiring them to be continuously logged in.
monitor by loginctl list-users
Issue: If XDG_RUNTIME_DIR
isn’t set in crontab
or other auto task-scheduling scripts, then tools like systemctl --user
might fail because they cannot find the correct runtime directory to communicate with systemd --user
.
Solution: export XDG_RUNTIME_DIR=/run/user/$(id -u);
Add-on: Cron service
|
|
|
|
Tmux activate server example
|
|
Relocate the docker lib
- Edit docker service configuration with: sudo vi /lib/systemd/system/docker.service
- Locate the Exec line: ExecStart=/usr/bin/dockerd -H fd:// –containerd=/run/containerd/containerd.sock
- Add a parameter that points docker to the new directory: ExecStart=/usr/bin/dockerd –data-root /second_drive/docker -H fd:// –containerd=/run/containerd/containerd.sock
|
|