## Switch directory
cd /etc/systemd/system
## Write service
sudo vim xxx.service
## Content
## Description: Description of this service, write whatever you want
## After: Specifies that the service starts after the network service starts, ensuring that the network is available
## WorkingDirectory: The directory where the .py file is located
## ExecStart: Which editor to run the file with
## Restart: If the service terminates unexpectedly, it will always restart automatically
## WantedBy: Specifies that the service is enabled in the system multi-user mode
## If you need to modify it, just modify WorkingDirectory, ExecStart, and Description
[Unit]
Description=Emotion Analysis Service
After=network.target
[Service]
User=root
WorkingDirectory=/opt/emotion/project
ExecStart=/opt/emotion/bin/python /opt/emotion/project/app.py
Restart=always
[Install]
WantedBy=multi-user.target
## Run (run as root, add sudo)
sudo systemctl start xxx.service/xxx (two ways, you can use .service or not)
## Set to start running
sudo systemctl enable xxx.service/xxx
4. View
## View status 2 sudo systemctl status xxx.service/xxx 3 4 ## View log 5 sudo journalctl -u "xxx" -f
This is the gradio interface I deployed on the virtual machine. It shows active, indicating successful operation.
5. Multiple Services
In case I need to deploy many services on the virtual machine, my virtual machine has three gradio interfaces deployed, so I need to write three services. This is easy to do, just copy and paste, but starting services is very cumbersome, which highlights the role of shell scripts