171 lines
2.9 KiB
Markdown
171 lines
2.9 KiB
Markdown
# Instructivo: Ejecutar un servicio Python con launchd en macOS
|
|||
|
|
|
||
|
|
## Objetivo
|
||
|
|
|
||
|
|
Ejecutar un servicio de Python de forma automática utilizando `launchd`.
|
||
|
|
|
||
|
|
## Estructura sugerida
|
||
|
|
|
||
|
|
``` text
|
||
|
|
/Users/george/services/sap_sunat/
|
||
|
|
.venv/
|
||
|
|
main.py
|
||
|
|
config.ini
|
||
|
|
logs/
|
||
|
|
stdout.log
|
||
|
|
stderr.log
|
||
|
|
```
|
||
|
|
|
||
|
|
## Verificar el entorno virtual
|
||
|
|
|
||
|
|
Activar:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
source .venv/bin/activate
|
||
|
|
```
|
||
|
|
|
||
|
|
Obtener el intérprete:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
which python
|
||
|
|
```
|
||
|
|
|
||
|
|
Usar esa ruta en `ProgramArguments`.
|
||
|
|
|
||
|
|
## Archivo .plist
|
||
|
|
|
||
|
|
Ubicación recomendada para un usuario:
|
||
|
|
|
||
|
|
``` text
|
||
|
|
~/Library/LaunchAgents/com.george.sap.sunat.plist
|
||
|
|
```
|
||
|
|
|
||
|
|
Claves habituales:
|
||
|
|
|
||
|
|
- `Label`
|
||
|
|
- `ProgramArguments`
|
||
|
|
- `WorkingDirectory`
|
||
|
|
- `RunAtLoad`
|
||
|
|
- `KeepAlive`
|
||
|
|
- `StandardOutPath`
|
||
|
|
- `StandardErrorPath`
|
||
|
|
|
||
|
|
## Comentarios
|
||
|
|
|
||
|
|
Se permiten comentarios XML:
|
||
|
|
|
||
|
|
``` xml
|
||
|
|
<!-- Comentario -->
|
||
|
|
```
|
||
|
|
|
||
|
|
También se pueden comentar bloques completos de claves.
|
||
|
|
|
||
|
|
## Cargar el servicio
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl load ~/Library/LaunchAgents/com.george.sap.sunat.plist
|
||
|
|
```
|
||
|
|
|
||
|
|
En versiones recientes también:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.george.sap.sunat.plist
|
||
|
|
```
|
||
|
|
|
||
|
|
## Verificar
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl list | grep sap
|
||
|
|
```
|
||
|
|
|
||
|
|
Información detallada:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl print gui/$(id -u)/com.george.sap.sunat
|
||
|
|
```
|
||
|
|
|
||
|
|
## Detener
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl unload ~/Library/LaunchAgents/com.george.sap.sunat.plist
|
||
|
|
```
|
||
|
|
|
||
|
|
o
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
launchctl bootout gui/$(id -u) ~/Library/LaunchAgents/com.george.sap.sunat.plist
|
||
|
|
```
|
||
|
|
|
||
|
|
## Monitorización
|
||
|
|
|
||
|
|
Monitor de Actividad: - Buscar `python` o `python3`.
|
||
|
|
|
||
|
|
Terminal:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
ps -ef | grep python
|
||
|
|
```
|
||
|
|
|
||
|
|
Consumo:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
ps -o pid,rss,vsz,%cpu,command -p <PID>
|
||
|
|
```
|
||
|
|
|
||
|
|
Ver logs:
|
||
|
|
|
||
|
|
``` bash
|
||
|
|
tail -f logs/stdout.log
|
||
|
|
tail -f logs/stderr.log
|
||
|
|
```
|
||
|
|
|
||
|
|
## Recomendaciones
|
||
|
|
|
||
|
|
- Usar `logging` con rotación.
|
||
|
|
- Manejar reconexión a SAP HANA.
|
||
|
|
- Implementar reintentos para SUNAT.
|
||
|
|
- Mantener configuración en `config.ini` o `.env`.
|
||
|
|
- Capturar `SIGTERM` para cerrar conexiones correctamente.
|
||
|
|
- Si el servicio es permanente, controlar internamente la frecuencia
|
||
|
|
de ejecución (cada 10 s entre 06:00 y 13:15; cada 30 min entre 13:15
|
||
|
|
y 14:00).
|
||
|
|
|
||
|
|
|
||
|
|
## Ejemplo de archivo .plist
|
||
|
|
|
||
|
|
```xml
|
||
|
|
<?xml version="1.0" encoding="UTF-8"?>
|
||
|
|
|
||
|
|
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
|
||
|
|
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||
|
|
|
||
|
|
<plist version="1.0">
|
||
|
|
<dict>
|
||
|
|
|
||
|
|
<key>Label</key>
|
||
|
|
<string>com.george.sap.sunat</string>
|
||
|
|
|
||
|
|
<key>ProgramArguments</key>
|
||
|
|
<array>
|
||
|
|
<string>/Users/george/services/sap_sunat/.venv/bin/python</string>
|
||
|
|
<string>/Users/george/services/sap_sunat/main.py</string>
|
||
|
|
</array>
|
||
|
|
|
||
|
|
<key>WorkingDirectory</key>
|
||
|
|
<string>/Users/george/services/sap_sunat</string>
|
||
|
|
|
||
|
|
<key>RunAtLoad</key>
|
||
|
|
<true/>
|
||
|
|
|
||
|
|
<key>KeepAlive</key>
|
||
|
|
<true/>
|
||
|
|
|
||
|
|
<key>StandardOutPath</key>
|
||
|
|
<string>/Users/george/services/sap_sunat/logs/stdout.log</string>
|
||
|
|
|
||
|
|
<key>StandardErrorPath</key>
|
||
|
|
<string>/Users/george/services/sap_sunat/logs/stderr.log</string>
|
||
|
|
|
||
|
|
</dict>
|
||
|
|
</plist>
|
||
|
|
```
|