Sample script
[Unit] # Mainly service description Description=test # Simple description service After=network.target # Describes the service category, indicating that the service needs to be started after the network service is started Before=xxx.service # Indicates that some services need to be started Before they are started. The After and Before fields only involve the start order and do not involve dependencies. [Service] # Core area Type=forking # Indicates the background operation mode. User=user # Set the user for the service to run Group=user # Set user groups for service operation KillMode=control-group # Defines how systemd stops services PIDFile=/usr/local/test/test.pid # Absolute path to store PID Restart=no # Define the restart mode of systemd after the service process exits. The default is not to restart ExecStart=/usr/local/test/bin/startup.sh # The service startup command requires an absolute path PrivateTmp=true # Indicates that a separate temporary space is allocated to the service [Install] WantedBy=multi-user.target # Multi user
[Unit] module – service description
Option | Description |
---|---|
Description | Short description of Unit |
Documentation | Reference document URI list |
Before, After | Start sequence of Units |
Requires | The dependency required for this Unit to start. If the dependency is "deactivated" or "failed", this Unit will be deactivated |
Wants | Configure weaker dependencies than "Requires" – if the listed Unit does not start successfully, the start of the Unit has no impact |
Conflicts | If a Unit is already set on other units, "start the former" will "stop the latter" - and vice versa (mutually exclusive) |
[Service] module – core area
Option | Description |
---|---|
Type | Configure the startup type of the Unit process that affects the "ExecStart" function and related options: +simple – by default, the process starting from ExecStart is the main process of the service +forking – the process starting from "ExecStart" generates a child process, which is the main process of the service (after startup, the parent process exits) +oneshot – similar to "simple", but the process exits before starting subsequent Units +dbus – similar to "simple", but subsequent units are started only after the main process obtains the D-Bus name +Notify – similar to "simple", but subsequent units will not be started until a notification message is sent through the "sd_notify() function" +idle – similar to "simple", the actual execution of the service binary will be delayed until all jobs are completed, avoiding the mixing of "state output" and "Shell output of the service" |
ExecStart | Specify the command or script to execute when starting the device +ExecStartPre and ExecStartPost specify custom commands to execute before and after ExecStart +"Tpye=oneshot" can specify multiple custom commands and then execute them sequentially |
ExecStop | Specifies the "Unit" command to stop when executing |
ExecReload | Specifies the command or script to execute when reloading the Unit |
Restart | After starting this selection, the service will "restart" after "process exit", but the systemctl command will execute "clean stop" |
RemainAfterExit | If set to True, the service is considered active even if all processes exit Default: False – this is particularly useful if "Type=oneshot" is set |
# Type: - simple((default)-- with ExecStart Field starts the main process - forking -- ExecStart Field with fork() Start in mode. At this time, the parent process will exit, and the child process is called the main process (running in the background). [generally set to forking] - oneshot -- be similar to simple ,Single execution only once, systemd Will wait for him to complete the execution before performing other services - dubs -- be similar to simple ,But I'll wait D-Bus Start after signal - notify -- be similar to simple ,After the start-up, a notification signal will be sent, and then systemd Restart other services - idle -- be similar to simple ,The service will not be started until other tasks are completed # EnvironmentFile: - Specify the profile, and "Conjunction number(-)" Combined use -- Exceptions that do not exist in the configuration file can be avoided # Environment: followed by multiple different Shell variables - Environment=ES_DATA=/usr/local/elasticsearch/data - Environment=ES_LOG=/usr/local/elasticsearch/log - Environment=ES_PID=/usr/local/elasticsearch/data/elasticsearch.pid - EnvironmentFile=-/usr/local/elasticsearch/xxxx.file # Conjunction sign -- "(-)": - Before all startup settings, the added variable fields can be added "Conjunction number" - express "Suppress errors",When an error occurs, it does not affect the execution of other commands - "EnvironmentFile=-/usr/local/elasticsearch/xxxx.file" -- When the file does not exist, no exception will be thrown # Type of KillMode: - control-group((default) -- All child processes in the current control group will be killed - process -- Kill only the main process - mixed -- The main process will receive "SIGTERM signal",Child process received "SIGKILL" signal - none -- No process will be killed, just the execution of the service stop command # Type of Restart: - no((default) -- No operation after exit - on-success -- Only when you exit normally (exit status code 0), will you restart - on-failure -- Abnormal exit and restart, including signal termination, timeout, etc - on-abnormal -- only "Terminated by signal" or "overtime",Will restart - on-abort -- only "When a signal not captured is received and terminated",Will restart - on-watchdog -- Timeout exit before restart - always -- Ignore "Why quit",Will restart # For daemons, on failure is recommended # RestartSec field: - express systemd Time to wait before restarting the service: RestartSec: 30 (Company: s) # Various Exec * fields: - Exec* The following commands are only accepted "Instruction parameters.." Format, not received "<> | &" And other special characters, many bash Syntax is not supported, if you want to support bash Syntax, need to be set Type=oneshot - ExecStart -- Command to execute when starting the service - ExecReload -- Command to execute when restarting the service - ExecStop -- Command to execute when the service is stopped - ExecStartPre -- Commands to execute before starting the service - ExecStopPost -- Command to execute after stopping the service # WantedBy field: - multi-user.target -- Multi user command line status, this setting is very important - graphical.target -- Graphical user status, dependent on multi-user.target
[Install] module
Option | Description |
---|---|
Alias | Spaces separate the list of Unit additional names. Most commands (excluding "systemctl", "systemctl enable") can use aliases instead of the actual Unit name |
RequiredBy,WantedBy | When the listed service is started, the service will also be started Reference: "wants requirements", "Unit]" |
Also | Specifies the list of units to be enabled or disabled together with Unit when the user runs systemctl enable or systemctl disable |