Vlastna Systemd sluzba a casovac
Example: systemd Service File
[Unit]
Description=My Custom App
After=network.target
Requires=network.target
[Service]
Type=simple
ExecStart=/usr/bin/myapp --flag1 --flag2
WorkingDirectory=/opt/myapp
User=www-data
Group=www-data
Environment="ENV_VAR=value"
EnvironmentFile=/etc/myapp.env
Restart=on-failure
RestartSec=5s
TimeoutStartSec=30
TimeoutStopSec=10
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
systemd Type Options
- simple – Default. Runs ExecStart directly.
- forking – For daemons that fork (e.g., Apache, MySQL).
- oneshot – For scripts that run and exit (used with timers).
- notify – App notifies systemd when ready.
- idle – Runs when the system is idle.
Example: systemd Timer
mytask.service
[Unit]
Description=Run my scheduled task
[Service]
Type=oneshot
ExecStart=/usr/local/bin/my-script.sh
mytask.timer
[Unit]
Description=Run mytask.service every 15 minutes
[Timer]
OnBootSec=5min
OnUnitActiveSec=15min
Unit=mytask.service
[Install]
WantedBy=timers.target
Environment File Example
/etc/myapp.env
DB_HOST=localhost
DB_PORT=3306
Useful Commands
systemctl enable --now mytask.timer
systemctl list-timers
journalctl -u myapp.service
systemctl daemon-reload
Príspevok pridaný dňa: 17.02.2025 od užívatela: Dodoslav.