
python3을 사용하여 간단한 소켓을 만들고 데몬으로 남겨두는 systemd로 서비스를 만들려고 하는데 여러 번 시도했지만 두 경우 모두 성공하지 못했습니다. 오늘은 systemd가 나를 이겼지만 내일은 또 다른 시도의 날입니다.
섬기는 사람
import socket
host = '127.0.0.1'
port = 9999
BUFFER_SIZE = 1024
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_tcp:
socket_tcp.bind((host, port))
socket_tcp.listen(5) # Esperamos la conexión del cliente
conn, addr = socket_tcp.accept() # Establecemos la conexión con el cliente
with conn:
print('[*] Conexión establecida')
while True:
# Recibimos bytes, convertimos en str
data = conn.recv(BUFFER_SIZE)
# Verificamos que hemos recibido datos
if not data:
break
else:
print('[*] Datos recibidos: {}'.format(data.decode('utf-8')))
conn.send(data) # Hacemos echo convirtiendo de nuevo a bytes
Client
import socket
# El cliente debe tener las mismas especificaciones del servidor
host = '127.0.0.1'
port = 9999
BUFFER_SIZE = 1024 MESSAGE = 'Hola, mundo!' # Datos que queremos enviar with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_tcp:
socket_tcp.connect((host, port))
# Convertimos str a bytes
socket_tcp.send(MESSAGE.encode('utf-8'))
data = socket_tcp.recv(BUFFER_SIZE)
구성 단위 파일
sudo nano /etc/systemd/system/socket_prueba.service
sudo rm -r /etc/systemd/system/socket_prueba.service
[Unit]
Description= Server Relay System: Manager
After=multi-user.target
[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/pipenv run python /path/test_server.py
[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable socket_prueba.service
sudo systemctl start socket_prueba.service
sudo systemctl status socket_prueba.service
결과:
● socket_prueba.service - Server Relay System: Manager
Loaded: loaded (/etc/systemd/system/socket_prueba.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sat 2021-09-25 16:07:17 -05; 58min ago
Process: 25771 ExecStart=/usr/local/bin/pipenv run python
/home/path>
Main PID: 25771 (code=exited, status=2)
sep 25 16:07:17 serversaas systemd[1]: socket_prueba.service: Scheduled restart job, restart counter is >
sep 25 16:07:17 serversaas systemd[1]: Stopped Server Relay System: Manager.
sep 25 16:07:17 serversaas systemd[1]: socket_prueba.service: Start request repeated too quickly.
sep 25 16:07:17 serversaas systemd[1]: socket_prueba.service: Failed with result 'exit-code'.
sep 25 16:07:17 serversaas systemd[1]: Failed to start Server Relay System: Manager.
인텐토 2 출처:시스템과 파이썬
● socket_prueba.socket - Socket prueba
Loaded: loaded (/etc/systemd/system/socket_prueba.socket; disabled; vendor preset: enabled)
Active: failed (Result: service-start-limit-hit) since Sat 2021-09-25 17:00:47 -05; 4s ago
Triggers: ● socket_prueba.service
Listen: 127.0.0.1:9999 (Stream)
sep 25 17:00:47 vidm-OMEN systemd[1]: Listening on Socket prueba.
sep 25 17:00:47 vidm-OMEN systemd[1]: socket_prueba.socket: Failed with result 'service-start-limit-hit'.
답변1
귀하의 경우에 대한 간단한 쉘 배포 명령을 사용할 준비가 되어 있습니다. 디렉토리, 이름, 서비스 또는 스크립트 설명 등을 변경할 수 있습니다. 설명은 다음과 같습니다.
디렉토리 및 스크립트 자체 만들기
mkdir /usr/src/python-socket -p
cat > /usr/src/python-socket/python-socket.py << 'EOL'
import socket
host = '127.0.0.1'
port = 9999
BUFFER_SIZE = 1024
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_tcp:
socket_tcp.bind((host, port))
socket_tcp.listen(5) # Esperamos la conexión del cliente
conn, addr = socket_tcp.accept() # Establecemos la conexión con el cliente
with conn:
print('[*] Conexión establecida')
while True:
# Recibimos bytes, convertimos en str
data = conn.recv(BUFFER_SIZE)
# Verificamos que hemos recibido datos
if not data:
break
else:
print('[*] Datos recibidos: {}'.format(data.decode('utf-8')))
conn.send(data) # Hacemos echo convirtiendo de nuevo a bytes
EOL
systemd 서비스 생성을 위한 변수 설정
SERVICE_NAME=python-socket
SERVICE_DESCRIPTION="Test python service"
SERVICE_COMMAND="/usr/bin/python3 /usr/src/python-socket/python-socket.py"
SERVICE_WORK_DIR=/usr/src/python-socket/
SERVICE_USER=root
systemd 서비스 구성 배포
cat > /etc/systemd/system/${SERVICE_NAME}.service << EOL
[Unit]
Description=${SERVICE_DESCRIPTION}
After=multi-user.target
[Service]
Environment="FROM=SYSTEMD"
WorkingDirectory=${SERVICE_WORK_DIR}
Type=simple
User=${SERVICE_USER}
ExecStart=${SERVICE_COMMAND}
RemainAfterExit=no
Restart=always
RestartSec=2
StartLimitBurst=999999
StartLimitInterval=0
KillMode=process
[Install]
WantedBy=multi-user.target
EOL
새로운 서비스를 적용하고 시작하고 확인하세요
systemctl daemon-reload
systemctl enable ${SERVICE_NAME}
systemctl stop ${SERVICE_NAME}
systemctl start ${SERVICE_NAME}
systemctl status ${SERVICE_NAME}
결과적으로 시스템 서비스 구성은 다음과 같습니다.
[Unit]
Description=Test python service
After=multi-user.target
[Service]
Environment="FROM=SYSTEMD"
WorkingDirectory=/usr/src/python-socket/
Type=simple
User=root
ExecStart=/usr/bin/python3 /usr/src/python-socket/python-socket.py
RemainAfterExit=no
Restart=always
RestartSec=2
StartLimitBurst=999999
StartLimitInterval=0
KillMode=process
[Install]
WantedBy=multi-user.target
어디:
Environment="FROM=SYSTEMD"
- Python 스크립트에 전달하려는 경우 일부 env 변수
Type=simple
- 스크립트가 실행되는 동안 작동하는 간단한 시스템 서비스
RemainAfterExit=no
Restart=always
RestartSec=2
StartLimitBurst=999999
StartLimitInterval=0
이 매개변수는 스크립트가 지속적으로 실패할 때 시작되는 어떤 조건에서도 스크립트가 다운되는 것을 허용하지 않습니다.
KillMode=process
- 이것은 스크립트가 중지되는 방식입니다. Python 스크립트에 특별한 SIG 이벤트가 없는 경우 이는 보편적입니다.