Creating a service on OpenBSD

Recently I deployed ssh-chat and Betula on my server, but I didn’t install them as a service. I just have a crontab with the following entry:

@reboot tmux new-session -d '/path/to/service'

It’s been working for me, but it can be annoying sometimes when I want to quickly stop and start the program. To make it easier for me, I made the following in /etc/rc.d/.

Configuration for ssh-chat

  1. Copy this file to /etc/rc.d and make sure it’s executable.

    #!/bin/ksh
    
    daemon="/usr/local/bin/ssh-chat"
    daemon_logger="daemon.info"
    daemon_flags="--verbose --bind ':PORT' --identity PRIVATE_KEY --admin=ADMIN_FILE --motd=MOTD_FILE"
    daemon_user="USER"
    
    . /etc/rc.d/rc.subr
    
    rc_bg=YES
    rc_reload=NO
    
    rc_cmd $1
    
    # nano /etc/rc.d/ssh_chat
    # chmod +x /etc/rc.d/ssh_chat
    
  2. Point the variable daemon to the executable, e.g., /usr/local/bin/ssh-chat

    You can move or link the executable to a more standard place as well.

    # ln -s ~/ssh-chat/ssh-chat /usr/local/bin/ssh-chat
    
  3. Modify the values in daemon_flags and daemon_user based on your liking.

    Alternatively, you can delete that line and set the flags using rcctl instead:

    # rcctl set ssh_chat flags --verbose --bind [...] --identity [...]
    
  4. Create the user that will run this service (see man useradd or man adduser)

    Set the proper permissions for files and directories as well that you want the daemon to access.

    # useradd -m chat
    # mkdir /var/chat
    # chown chat:chat /var/chat
    
  5. Enable and start the service

    # rcctl enable ssh_chat
    # rcctl start ssh_chat
    
  6. Check the logs in /var/log/daemon

    # tail -f /var/log/daemon
    

Configuration for Betula

  1. Copy this file to /etc/rc.d and make sure it’s executable.

    #!/bin/ksh
    
    daemon="/usr/local/bin/betula"
    daemon_flags="/var/betula/links.betula"
    daemon_user="betula"
    
    . /etc/rc.d/rc.subr
    
    rc_bg=YES
    rc_reload=NO
    
    rc_cmd $1
    
    # nano /etc/rc.d/betula
    # chmod +x /etc/rc.d/betula
    
  2. Point the variable daemon to the executable, e.g., /usr/local/bin/betula

    # ln -s $GOPATH/bin/betula /usr/local/bin/betula
    
  3. Modify the values in daemon_flags and daemon_user based on your liking.

    Alternatively, you can delete that line and set the flags using rcctl instead:

    # rcctl set betula flags /var/betula/links.betula
    
  4. Create the user that will run this service (see man useradd or man adduser)

    # useradd -m betula
    # mkdir /var/betula
    # chown betula:betula /var/betula
    
  5. Enable and start the service

    # rcctl enable betula
    # rcctl start betula
    
  6. Check the logs in /var/log/daemon

    # tail -f /var/log/daemon