Skip to content

Service file

Romano edited this page Dec 29, 2023 · 1 revision

To create a service file

Let's start by creating a basic systemd service file for your dnsseed program. We'll include essential directives to ensure the program starts on boot and restarts in case of failure.

  1. Create the Service File: Open a new file in the /etc/systemd/system/ directory. Let's name it dnsseed.service. You can use a text editor like nano or vim. For instance, you can create the file with the command:

    sudo nano /etc/systemd/system/dnsseed.service
  2. Basic Structure of the Service File: Here's a basic template for your service file:

    [Unit]
    Description=Dnsseed Service
    After=network.target
    
    [Service]
    User=username
    ExecStart=/path/to/dnsseed -h seed.viacoin.net -n seeder-1.viacoin.net -m [email protected]
    Restart=on-failure
    # Optional security directives:
    # ProtectSystem=strict
    # PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target

    Replace username with the non-root user you want to run the service as, and /path/to/dnsseed with the full path to your dnsseed binary.

  3. Explanation of Directives:

    • [Unit] section: Basic metadata about the service.
    • Description: A brief description of the service.
    • After: Ensures the service starts after the network is up.
    • [Service] section: How the service should behave.
    • User: The user under which the service will run.
    • ExecStart: The command to start the service.
    • Restart: Defines the service's restart behavior.
    • [Install] section: Defines how the service is installed.
    • WantedBy: The default target under which the service should start.
  4. Enable and Start the Service: After saving the file, enable the service to start on boot with:

    sudo systemctl enable dnsseed.service

    Then start the service with:

    sudo systemctl start dnsseed.service
  5. Check the Status: Finally, check the status of your service:

    sudo systemctl status dnsseed.service

Example of a file

[Unit]
Description=DNSseed Service
After=network.target

[Service]
User=root
ExecStart=/root/bitcoin-seeder/dnsseed -h seed.viacoin.net -n seeder-1.viacoin.net -m [email protected]
Restart=on-failure
StandardOutput=append:/var/log/dnsseed.log
StandardError=inherit
# Optional security directives:
# ProtectSystem=strict
# PrivateTmp=true

[Install]
WantedBy=multi-user.target

Logging

Adding logging to your systemd service is a good practice as it helps monitor and troubleshoot. Systemd has built-in logging capabilities via the journaling system, but you can also configure your service to write logs to a specific file. Here’s how you can enhance your service file with logging capabilities:

  1. Using Systemd's Journaling: By default, the standard output (stdout) and standard error (stderr) of a systemd service are sent to systemd's journal. You can view these logs using the journalctl command. For example:

    sudo journalctl -u dnsseed.service

    This command will show you all the logs for your dnsseed service.

  2. Redirecting to a Specific Log File: You can redirect the output if you prefer to have logs in a separate file. Modify the [Service] section of your dnsseed.service file like this:

    [Service]
    ExecStart=/path/to/dnsseed -h seed.viacoin.net -n seeder-1.viacoin.net -m [email protected]
    Restart=on-failure
    StandardOutput=append:/var/log/dnsseed.log
    StandardError=inherit
    • StandardOutput=append:/var/log/dnsseed.log: This line directs the standard output of your service to a file named dnsseed.log in the /var/log directory. append ensures that logs are added to the end of the file, not overwriting previous content.
    • StandardError=inherit: This line makes sure that standard error messages are handled in the same way as standard output.
  3. Setting up Log Rotation: If you redirect logs to a file, it's important to implement log rotation to prevent the log file from becoming too large. You can use a tool like logrotate for this. Typically, logrotate is already installed and configured on most Linux distributions. You must add a configuration for your dnsseed.log file in the /etc/logrotate.d/ directory.

  4. Reloading and Restarting the Service: After making changes to your service file, reload the systemd daemon:

    sudo systemctl daemon-reload

    Then restart your service:

    sudo systemctl restart dnsseed.service
  5. Checking Log File: You can check your log file with:

    cat /var/log/dnsseed.log

    or monitor it in real-time with:

    tail -f /var/log/dnsseed.log

Ensure the directory and log file you specify for logging have the appropriate permissions and are writable by the user under which the service is running. If you're running the service as root, there shouldn't be any permission issues with writing to /var/log.

Clone this wiki locally