Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to write pid file #2659

Closed
nandlab opened this issue Oct 18, 2022 · 2 comments
Closed

Unable to write pid file #2659

nandlab opened this issue Oct 18, 2022 · 2 comments

Comments

@nandlab
Copy link

nandlab commented Oct 18, 2022

Linux distribution: Devuan GNU/Linux 4 (chimaera) x86_64
Init system: sysvinit

Mosquitto version: 2.0.11

/etc/mosquitto/mosquitto.conf:

# Place your local configuration in /etc/mosquitto/conf.d/
#
# A full description of the configuration file is at
# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

pid_file /run/mosquitto/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log

include_dir /etc/mosquitto/conf.d

When I type sudo mosquitto -c /etc/mosquitto/mosquitto.conf it says Unable to write pid file and does not start.

Apparently it does not have the required permissions to create the /run/mosquitto directory. If I create it manually it works, but the directory disappears after reboot.

@ptjm
Copy link

ptjm commented Oct 19, 2022

As of version 2.0, mosquitto drops down from root to the run-time user before the pid file is created. As far as I know, all you can do is create the pid file in advance and change ownership to mosquitto's run-time user before starting mosquitto.

@nandlab
Copy link
Author

nandlab commented Oct 31, 2022

I have changed /etc/init.d/mosquitto as follows:

#!/bin/sh

### BEGIN INIT INFO
# Provides:		mosquitto
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	mosquitto MQTT v3.1 message broker
# Description:
#  This is a message broker that supports version 3.1 of the MQ Telemetry
#  Transport (MQTT) protocol.
#
#  MQTT provides a method of carrying out messaging using a publish/subscribe
#  model. It is lightweight, both in terms of bandwidth usage and ease of
#  implementation. This makes it particularly useful at the edge of the network
#  where a sensor or other simple device may be implemented using an arduino for
#  example.
### END INIT INFO

set -e

PIDDIR=/run/mosquitto
PIDFILE="${PIDDIR}/mosquitto.pid"
DAEMON=/usr/sbin/mosquitto

# /etc/init.d/mosquitto: start and stop the mosquitto MQTT message broker

test -x ${DAEMON} || exit 0

umask 022

. /lib/lsb/init-functions

# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

create_pid_dir() {
    mkdir -p "$PIDDIR"
    chown mosquitto: "$PIDDIR"
}

case "$1" in
  start)
	if init_is_upstart; then
	    exit 1
	fi
	create_pid_dir
	log_daemon_msg "Starting network daemon" "mosquitto"
	if start-stop-daemon -u mosquitto -c mosquitto --start --quiet --oknodo --background --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;
  stop)
	if init_is_upstart; then
	    exit 0
	fi
	log_daemon_msg "Stopping network daemon" "mosquitto"
	if start-stop-daemon -u mosquitto --stop --quiet --oknodo --pidfile ${PIDFILE}; then
	    log_end_msg 0
	    rm -f ${PIDFILE}
	else
	    log_end_msg 1
	fi
	;;


  reload|force-reload)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Reloading network daemon configuration" "mosquitto"
        if start-stop-daemon -u mosquitto --stop --signal HUP --quiet --oknodo --pidfile $PIDFILE; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
	;;

  restart)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Restarting network daemon" "mosquitto"
	if start-stop-daemon -u mosquitto --stop --quiet --oknodo --retry 30 --pidfile ${PIDFILE}; then
	    rm -f ${PIDFILE}
	fi
	create_pid_dir
	if start-stop-daemon -c mosquitto -u mosquitto --start --quiet --oknodo --background --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;

  try-restart)
	if init_is_upstart; then
	    exit 1
	fi
	log_daemon_msg "Restarting Mosquitto message broker" "mosquitto"
	set +e
	start-stop-daemon -u mosquitto --stop --quiet --retry 30 --pidfile ${PIDFILE}
	RET="$?"
	set -e
	case $RET in
	    0)
		# old daemon stopped
		rm -f ${PIDFILE}
		if start-stop-daemon -c mosquitto -u mosquitto --start --quiet --oknodo --background --pidfile ${PIDFILE} --exec ${DAEMON} -- -c /etc/mosquitto/mosquitto.conf ; then
		    log_end_msg 0
		else
		    log_end_msg 1
		fi
		;;
	    1)
		# daemon not running
		log_progress_msg "(not running)"
		log_end_msg 0
		;;
	    *)
		# failed to stop
		log_progress_msg "(failed to stop)"
		log_end_msg 1
		;;
	esac
	;;

  status)
	if init_is_upstart; then
	    exit 1
	fi
	status_of_proc -p ${PIDFILE} ${DAEMON} mosquitto && exit 0 || exit $?
	;;

  *)
	log_action_msg "Usage: /etc/init.d/mosquitto {start|stop|reload|force-reload|restart|try-restart|status}"
	exit 1
esac

exit 0

Now it works.

@nandlab nandlab closed this as completed Oct 31, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants