Create service method under Linux
1. Create a new service file
In Linux system, each service needs a corresponding service file, which is saved in the "/etc/systemd/system" directory.
You can create a new service file in this directory, such as "".
2. Write the content of the service file
In the "" file, you can use a text editor to add the following:
[Unit] Description=My run test Service After= [Service] ExecStart=/root/ User=root Group=root Restart=always [Install] WantedBy=
in:
Description in [Unit] represents the description of the service, After specifying which other services should be started before the service is started;
ExecStart in [Service] represents the service start command. User and Group specify the user and group to which the service runs. Restart indicates whether the service should automatically restart after a failure;
WantedBy in [Install] specifies which target the service should be started.
In general:
Just focus on the ExecStart item, and need to be modified to the path and name of the corresponding script
3. Enable service
Service can be enabled via the following command:
systemctl enable
This command will automatically link the Service file to /etc/systemd/system/ directory
It should be noted that this command needs to be executed using root permissions
4. Start and stop service
Use the following command to start and stop the service:
systemctl start systemctl stop
The service can be automatically started when the system starts. You can set it using the following command:
systemctl enable
The above is how to create a Linux service.
This method is used to create a service, which allows scripts to run automatically after the Linux system is powered on, but does not rely on the /etc// file
Summarize
These are just personal experience. I hope you can give me a reference and I hope you can support me more.