To create a production-like systemctl service for running your binary with the required environment variable, follow these steps:
1. Move the Binary to a System Path
Move your redpanda-console binary to a system directory, such as /usr/local/bin:
sudo mv ./redpanda-console /usr/local/bin/
sudo chmod +x /usr/local/bin/redpanda-console2. Create a Systemd Service File
Create a new service file for redpanda-console. Use your preferred editor, e.g., nano or vim:
sudo nano /etc/systemd/system/redpanda-console.serviceAdd the following content to the file:
[Unit]
Description=Redpanda Console Service
After=network.target
[Service]
Environment="KAFKA_BROKERS=kafka.x.com:9092"
ExecStart=/usr/local/bin/redpanda-console
Restart=always
User=redpanda
Group=redpanda
WorkingDirectory=/usr/local/bin
[Install]
WantedBy=multi-user.targetExplanation of Key Fields:
Environment: Sets the environment variableKAFKA_BROKERS.ExecStart: The command to run the binary.Restart: Ensures the service restarts automatically if it fails.UserandGroup: Use a specific user for security (redpandain this case). Create this user if it doesn’t exist.WorkingDirectory: Sets the working directory.
3. Create a User for the Service
For security, create a dedicated redpanda user:
sudo useradd -r -s /bin/false redpanda
sudo chown redpanda:redpanda /usr/local/bin/redpanda-console4. Reload Systemd and Start the Service
Reload systemd to recognize the new service file, then enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable redpanda-console
sudo systemctl start redpanda-console5. Check Service Status
Verify that the service is running correctly:
sudo systemctl status redpanda-consoleIf everything is configured correctly, you should see the service active and running.
6. Logs and Troubleshooting
To view logs for the service, use:
journalctl -u redpanda-console -fThis setup ensures redpanda-console runs in a production-like environment, managed by systemctl. Let me know if you encounter any issues!