Systemctl

From campisano.org
Jump to navigation Jump to search

Note, using systemd


  • Show service status
systemctl --type service --all --no-legend --no-pager
watch -d systemctl status cron -o short-is
  • Example background daemon service
cat > /etc/systemd/system/grafana-agent.service << EOF
[Unit]
Description=Grafana Cloud Agent

[Service]
User=grafana-agent
ExecStart=/srv/local/grafana/bin/agent-linux-amd64 --config.file=/srv/local/grafana/etc/agent-config.yaml
Restart=always

[Install]
WantedBy=multi-user.target
EOF
  • Example non-background init script service

example script iptables_SSHlimit.sh

chmod 0750 iptables_SSHlimit.sh
mv iptables_SSHlimit.sh /etc/init.d

cat > /etc/systemd/system/iptables_SSHlimit.service << EOF
[Unit]
Description=Firewall rules to limit ssh incoming connections

[Service]
Type=simple
RemainAfterExit=yes
ExecStart=/etc/init.d/iptables_SSHlimit.sh start
ExecStop=/etc/init.d/iptables_SSHlimit.sh stop

[Install]
# Installs a hook to use this unit file when the system boots or shuts down
WantedBy=multi-user.target
EOF
  • Enable and start the service
# reload service files
systemctl daemon-reload
# enable service at boot
systemctl enable <SERVICENAME>.service
# start manually for this time
systemctl start <SERVICENAME>.service
# check service status
systemctl status <SERVICENAME>.service
# to check service logs
journalctl -u <SERVICENAME>.service