Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Last active January 1, 2024 12:01
Show Gist options
  • Save PieterScheffers/1ecd70a1bfe640afb98f3cac9630814b to your computer and use it in GitHub Desktop.
Save PieterScheffers/1ecd70a1bfe640afb98f3cac9630814b to your computer and use it in GitHub Desktop.
vncserver rc.d script for FreeBSD
#!/bin/sh
# Download this file
# cd /usr/local/etc/rc.d && fetch --no-verify-peer https://gist.githubusercontent.com/PieterScheffers/1ecd70a1bfe640afb98f3cac9630814b/raw/326033ce1c243fd7ecd018684e748234668cf9ff/vncserver
#
# Make the file executable with:
# /usr/local/etc/rc.d/vncserver (chmod +x)
#
# add to /etc/rc.conf
#
# vncserver_enable="YES"
# vncserver_user="vncserver"
# vncserver_display="1"
# PROVIDE: vncserver
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv
#
. /etc/rc.subr
name=vncserver
rcvar=vncserver_enable
VNCSERVER=/usr/local/bin/vncserver
load_rc_config $name
start_cmd="vncserver_start"
stop_cmd="vncserver_stop"
restart_cmd="vncserver_restart"
: ${vncserver_user="vncserver"}
: ${vncserver_enable="NO"}
: ${vncserver_display="1"}
: ${vncserver_depth="24"}
: ${vncserver_geometry="1280x800"}
vncserver_start()
{
CMD="$VNCSERVER -depth ${vncserver_depth} -geometry ${vncserver_geometry} -nolisten tcp :${vncserver_display}"
su -l ${vncserver_user} -c "${CMD}"
}
vncserver_stop()
{
CMD="$VNCSERVER -kill :${vncserver_display}"
su -l ${vncserver_user} -c "${CMD}"
}
run_rc_command "$1"
@PhilZ-cwm6
Copy link

Thank you a lot, feel free to look at my revision here to fix a few issues:
https://gist.github.com/PhilZ-cwm6/bdc85494725bd1c1443b4db0f7f306e4/revisions

1- support service restart command
2- fix the display number not properly set under FreeBSD: for some reason, unlike in Linux, vncserver commands needs the screen number argument before options

@joe81tx
Copy link

joe81tx commented Dec 12, 2022

# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv
Hi, might I suggest adding dbus to the end of line 17? As a new FreeBSD user I had a terrible time figuring out why vnc connetctions displayed a black screen after a reboot, but restarting the service (I use PhilZ-cwm6's version) would fix it. It might be more obvious to those more experienced but I run my machine headless (hence why this service is critical). As such I don't see the dmesg boot sequence. I've found the dmesg I can launch via ssh after the fact doesn't match 100% what's displayed on the console. At any rate, the issue is that rc.d was launching Xvnc before dbus which is why it wouldn't work after a reboot. Thank you both for this script, it was immensely helpful.

@PhilZ-cwm6
Copy link

Adding dbus at the end doesn't fix the issue if the ssh connection with the server (C2S port forwarding) is established too early
In that case, SSH connection needs to be closed and a new login established to get the display on VNC

@joe81tx
Copy link

joe81tx commented Jan 1, 2024

I wasn't using port forwarding. My issue is that upon boot the vnc service would start before dbus and thus fail. So after every reboot I would have to manually restart the vnc service. But when I added dbus to the require line the service would wait for dbus to be active, then start so everything worked automatically if I reboot my system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment