Skip to content

Instantly share code, notes, and snippets.

@dlangille
Last active May 9, 2020 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlangille/a3f42f197c7a0d2c19e7a3af6aba6b3f to your computer and use it in GitHub Desktop.
Save dlangille/a3f42f197c7a0d2c19e7a3af6aba6b3f to your computer and use it in GitHub Desktop.
Why is the home directory = / when running from daemon(8)?
I used librenms, but it works with any user:
$ grep librenms /etc/passwd
librenms:*:249:249:LibreNMS pseudo-user:/var/db/librenms/home:/bin/sh
NOTE: I'm taking a different approach which does not rely upon a HOME directory.
place this in /usr/local/etc/rc.d/mytest
#!/bin/sh
. /etc/rc.subr
name=mytest
rcvar="${name}_enable"
load_rc_config $name
: ${mytest_enable:="NO"}
: ${librenms_user:="librenms"}
: ${librenms_pid:="/var/run/librenms/librenms-extra.pid"}
pidfile="$librenms_pid"
procname="/usr/local/bin/python3.7"
command="/usr/sbin/daemon"
# we neeed $procname here - that's what rc.d looks for to maintain (start/stop/restart)
command_args="-T $name -p $pidfile -u $librenms_user $procname /usr/home/dan/tmp/home.py"
run_rc_command "$1"
Adjust the file location in mytest above:
$ cat ~/tmp/home.py
#!/usr/local/bin/python3
#print os.getenv("HOME")
from os.path import expanduser
home = expanduser("~")
print(home)
import os
import pwd
print(pwd.getpwuid( os.getuid() ).pw_name)
if 'HOME' in os.environ:
print('HOME environment variable is already defined. Value =', os.environ['HOME'])
else:
print('HOME environment variable is not defined.')
$ sudo service mytest onestart
Starting mytest.
Then in /var/log/messages:
May 9 21:25:20 librenms mytest[65380]: /
May 9 21:25:20 librenms mytest[65380]: librenms
May 9 21:25:20 librenms mytest[65380]: HOME environment variable is already defined. Value = /
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment