Mua hàng tại Link Shopee hoặc Hotline 0345-148-136.

Write a rc.d script in FreeBSD for a go program

I’ve just install MailyGo from source on FreeBSD and this is what I leaned.

Build go program from source on FreeBSD was easy. You just need to install golang, build it then copy to /usr/local/bin

# pkg install go
# git clone https://git.jlel.se/jlelse/MailyGo.git
# cd MailyGo
# go build -o mailygo
# cp mailygo /usr/local/bin

I wrote some rc.d scripts for FreeBSD before but it was not work for some golang program.

The problem is some golang program doesn’t have daemon mode option when you want to run it.

So this is where daemon(8) is useful, you can run any program in daemon mode with daemon(8).

The syntax is simple:

# /usr/sbin/daemon -o '/var/log/mailygo.log' -p '/var/run/mailygo.pid' -u 'mailygo' -- /usr/local/bin/mailygo

You can read on FreeBSD manual page. This script will run mailygo program in daemon by `mailygo` user, write a logfile, with a pidfile.

I wanted to pass some environment variables to this daemon process, then I made a precmd.

My full rc.d script for mailygo bellow:

root@mailygo:~ # cat /usr/local/etc/rc.d/mailygo
#!/bin/sh

# PROVIDE: mailygo
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# /usr/local/etc/rc.d/mailygo
# https://github.com/DenBeke/mailygo

. /etc/rc.subr

name=mailygo
rcvar=mailygo_enable
load_rc_config ${name}

: ${mailygo_enable:="NO"}
: ${mailygo_smtp_user:="[email protected]"}
: ${mailygo_smtp_pass:="SuperSecret"}
: ${mailygo_smtp_host:="smtp.example.com"}
: ${mailygo_smtp_port:="587"}
: ${mailygo_email_from:="[email protected]"}
: ${mailygo_email_to:="[email protected]"}
: ${mailygo_allowed_to:="[email protected],[email protected]"}
: ${mailygo_port:="8080"}
: ${mailygo_honeypots:="_t_email"}
: ${mailygo_google_api_key:=""}
: ${mailygo_blacklist:="gambling,casino"}
: ${mailygo_logfile:="/var/log/mailygo.log"}
: ${mailygo_pidfile:="/var/run/mailygo.pid"}
: ${mailygo_username:="mailygo"}
mailygo_group=${mailygo_group:-$mailygo_user}

export_variable()
{
	_var="mailygo_$(echo $1 | tr A-Z a-z)"
	eval _val="\$${_var}"
	[ -z "${_val}" ] || export "${1}"="${_val}"
}

export_variables()
{
	for _v in $@; do
		export_variable "${_v}"
	done
}

mailygo_precmd()
{
    # Check if user exist
    if id -u $mailygo_username > /dev/null 2>&1; then
            echo "User found, it's OK"
    else
            echo "User not found, create one"
            pw useradd -n "${mailygo_username}" -u 1000  -m
    fi
	export_variables SMTP_USER SMTP_PASS SMTP_HOST SMTP_PORT EMAIL_FROM EMAIL_TO ALLOWED_TO PORT HONEYPOTS GOOGLE_API_KEY BLACKLIST
}

pidfile="${mailygo_pidfile}"
procname="/usr/local/bin/mailygo"
command="/usr/sbin/daemon"
command_args="-o '${mailygo_logfile}' -p '${pidfile}' -u '${mailygo_username}' -t '${desc}' -- ${procname}"
start_precmd="mailygo_precmd"

run_rc_command "$1"

Some of my FreeBSD rc.d scripts can be found here.


Comments

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *