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 create PID file #1950

Closed
joekerna opened this issue Dec 14, 2020 · 33 comments
Closed

Unable to create PID file #1950

joekerna opened this issue Dec 14, 2020 · 33 comments
Milestone

Comments

@joekerna
Copy link

Since upgrading to Mosquitto 2.0.2 I get the following error upon starting Mosquitto:

mosquitto -c mosquitto.conf
1607938526: Loading config file /etc/mosquitto/conf.d/default.conf
2020-12-14T10:35:26: Error: Unable to write pid file.

My conf file is as follows:

# 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 /var/run/mosquitto.pid

persistence true
persistence_location /var/lib/mosquitto/

log_dest file /var/log/mosquitto/mosquitto.log
log_timestamp_format %Y-%m-%dT%H:%M:%S
log_type all

include_dir /etc/mosquitto/conf.d

Is this a bug or a misconfiguration on my side?

The system is:
Ubuntu 20.04

@tehbra1n
Copy link

I am experiencing the same issue. Removing the pid_file line from mosquitto.conf allows it to start but it will not accept connections from anything but localhost.

executing

sudo chown mosquitto:mosquitto /var/run/mosquitto

And restarting mosquitto works and allows the broker to create the pid file, however the broker will still not accept connections from outside localhost.,

This was a working broker set up prior to upgrading to 2.0.2

@tehbra1n
Copy link

I am experiencing the same issue. Removing the pid_file line from mosquitto.conf allows it to start but it will not accept connections from anything but localhost.

executing

sudo chown mosquitto:mosquitto /var/run/mosquitto

And restarting mosquitto works and allows the broker to create the pid file, however the broker will still not accept connections from outside localhost.,

This was a working broker set up prior to upgrading to 2.0.2

Solving my own problem, the update to 2.0.2 has new config requirements. I added a file custom.conf to /etc/mosquitto/conf.d with only:

allow_anonymous true
listener 1883

This seems to emulate the older 1.x style of operation. I'll figure out the newer security options later.

@vampywiz17
Copy link

vampywiz17 commented Dec 14, 2020

https://mosquitto.org/documentation/migrating-to-2-0/

I also comment it the pid file line and create a listener.conf in conf.d. Now is working like before.

Only one question. It cause any problem that i not use the pid file?

@peterchs
Copy link
Contributor

peterchs commented Dec 14, 2020

I corrected this by

  1. Edit /etc/mosquitto/mosquitto.conf and change pid file path line like so
    pid_file /var/run/mosquitto/mosquitto.pid

  2. Altering /lib/systemd/system/mosquitto.service and adding new lines after the other ExecStartPre lines:

ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Then run:
systemctl daemon-reload

And then restarted service:
systemctl restart mosquitto.service

Should start fine, confirm with:
systemctl status mosquitto.service

Ubuntu 18.04.5 LTS, 20 may be slightly different, looks like the conf is different path anyway

@joekerna
Copy link
Author

I've managed to get it working too.
I've manually created a directory /var/run/mosquitto with ownership to user mosquitto

In mosquitto.conf I set
pid_file /var/run/mosquitto/mosquitto.pid

Now it's working. Does that work for everybody else?

@rpiper
Copy link

rpiper commented Dec 15, 2020

I corrected this by

  1. Edit /etc/mosquitto/mosquitto.conf and change pid file path line like so
    pid_file /var/run/mosquitto/mosquitto.pid
  2. Altering /lib/systemd/system/mosquitto.service and adding new lines after the other ExecStartPre lines:
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Then run:
systemctl daemon-reload

And then restarted service:
systemctl restart mosquitto.service

Should start fine, confirm with:
systemctl status mosquitto.service

Ubuntu 18.04.5 LTS, 20 may be slightly different, looks like the conf is different path anyway

I had the exact same problem, and this fixes it perfectly!

@ralight
Copy link
Contributor

ralight commented Dec 15, 2020

If you're using the Ubuntu PPA packages then this should now be fixed automatically. It's been a bit tricky getting the websockets library updated for 16.04 and 18.04, but they should all be ok again.

@vampywiz17
Copy link

vampywiz17 commented Dec 15, 2020

I corrected this by

  1. Edit /etc/mosquitto/mosquitto.conf and change pid file path line like so
    pid_file /var/run/mosquitto/mosquitto.pid
  2. Altering /lib/systemd/system/mosquitto.service and adding new lines after the other ExecStartPre lines:
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Then run:
systemctl daemon-reload
And then restarted service:
systemctl restart mosquitto.service
Should start fine, confirm with:
systemctl status mosquitto.service
Ubuntu 18.04.5 LTS, 20 may be slightly different, looks like the conf is different path anyway

I had the exact same problem, and this fixes it perfectly!

I do this, seems to be fixed.

It possible to fix it here?

https://github.com/eclipse/mosquitto/tree/master/service/systemd

@janh
Copy link

janh commented Dec 16, 2020

If you're using the Ubuntu PPA packages then this should now be fixed automatically.

It is still not working for me on Ubuntu 18.04. /var/run is on a tmpfs, so /var/run/mosquitto does not exist at all after a reboot. The change to the systemd unit mentioned in #1950 (comment) fixes this.

@peterchs
Copy link
Contributor

peterchs commented Dec 16, 2020

If you're using the Ubuntu PPA packages then this should now be fixed automatically.

It is still not working for me on Ubuntu 18.04. /var/run is on a tmpfs, so /var/run/mosquitto does not exist at all after a reboot. The change to the systemd unit mentioned in #1950 (comment) fixes this.

Same here.

Took the latest package, rebooted and the package update removed my mosquitto.service changes from #1950 (comment), as a result it wasnt able to write the pid file again (folder didnt exist after the reboot) and didnt start. The package install prompted for conf change ok, but chose to retain my existing conf and not take the package/dist version. Nothing prompted regarding mosquitto.service it was overwritten.

I'll raise a PR to update the systemd scripts with my changes mentioned in the comment.

@Raul-7-7
Copy link

@peteakalad Using Ubuntu 20.04 (server) followed you instructions, worked like a charm even after reboot.
Thank you very much :)

@ralight ralight added this to the 2.0.3 milestone Dec 17, 2020
@peterchs
Copy link
Contributor

apt upgraded to Ubuntu 18.04.5 mosquitto package v2.0.3 this morning.

Mosquitto systemd script still has the ExecStartPre lines to create the mosquitto /var/run folder and set permissions.

Rebooted and still all good, mosquitto started fine.

Looks like this issue is now resolved, thanks for updating package @ralight

@wilsonmfg
Copy link

I also had an issue updating on ubuntu, but just a simple apt update on 16.04, no distro version change.

/var/run/mosquitto was created and permission set correctly, but my conf file had "pid_file /var/run/mosquitto.pid", which there not permission to write to. I don't recall why my pid file was set this way as it's been there a couple years, but changing this fixed the issue. posting here in case it helps others with another possible cause of the error.

@ralight
Copy link
Contributor

ralight commented Feb 4, 2021

I fixed this in later versions and as there have been no more reports, I assume that fix worked fine and I can close this issue. Thanks for the comments everyone.

@ralight ralight closed this as completed Feb 4, 2021
@lordraiden
Copy link

I just got this problem in my docker that got updated tonight
eclipse-mosquitto:latest
imagen

@defcon84
Copy link

defcon84 commented Feb 5, 2021

same here with eclipse-mosquitto:latest tonight.
went back to 1.6.13 for now.

@Knapoc
Copy link

Knapoc commented Feb 5, 2021

you probably have a pid-file set in your mosquitto config, but if you enter your container and look for it, it's probably not set anyway. At least that's what I saw in my 1.6-container. I just uncommented the config line for now.

@defcon84
Copy link

defcon84 commented Feb 5, 2021

yeah commenting the the config line did it (not uncommenting :p)

@ralight
Copy link
Contributor

ralight commented Feb 5, 2021

Other things that may affect you now you are 2.0: https://mosquitto.org/documentation/migrating-to-2-0/

@lordraiden
Copy link

lordraiden commented Feb 5, 2021

But should we comment the pid in the configuration file, is there a clean workaround, or is a bug and this need to be fixed?

@ralight
Copy link
Contributor

ralight commented Feb 5, 2021

@lordraiden Unless you are making use of the pid file for a specific reason there is need for it, so you are free to comment it out.

@pascalsaul
Copy link

I changed the configuration to:

#pid_file /var/run/mosquitto.pid
#port 1883
listener 1883
allow_anonymous true

I'm running two mosquitto's on the same docker host.

@arzaman
Copy link

arzaman commented Feb 9, 2021

Hi
I still have the issue related to "unable to create PID file"
I upgrade on my ubuntu server 18.04 mosquitto from release 1.6.3 to last release using following command

sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto

but when I try to restart the process I got an error

this is what reports sytemctl status

mosquitto.service - Mosquitto MQTT Broker
   Loaded: loaded (/lib/systemd/system/mosquitto.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2021-02-09 22:10:02 UTC; 1min 8s ago
     Docs: man:mosquitto.conf(5)
           man:mosquitto(8)
  Process: 3823 ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf (code=exited
  Process: 3822 ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto (code=exited, status=
  Process: 3821 ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto (code=exited, status=0
  Process: 3814 ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto (code=exited, status=
  Process: 3812 ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto (code=exited, status=0
 Main PID: 3823 (code=exited, status=1/FAILURE)

Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Main process exited, code=ex
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Failed with result 'exit-cod
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: Failed to start Mosquitto MQTT Broker.
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Service hold-off time over,
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Scheduled restart job, resta
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: Stopped Mosquitto MQTT Broker.
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Start request repeated too q
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Failed with result 'exit-cod
Feb 09 22:10:02 ip-172-31-36-63 systemd[1]: Failed to start Mosquitto MQTT Broker.

this is what reports journalctl -u mosquitto /var/run/mosquitto

Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: Failed to start Mosquitto MQTT Broker.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Service hold-off time over,
 scheduling restart.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Scheduled restart job, rest
art counter is at 5.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: Stopped Mosquitto MQTT Broker.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Start request repeated too
quickly.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: mosquitto.service: Failed with result 'exit-co
de'.
Feb 09 22:15:02 ip-172-31-36-63 systemd[1]: Failed to start Mosquitto MQTT Broker.

If I try to start manually mosqutitto logged as ubuntu default user
mosquitto /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf I got this error

Error: Unable to write pid file.
If I run with sudo privileges seems ok but I got above errors

Notice that
I have modified the conf file adding the new path
pid_file /var/run/mosquitto/mosquitto.pid

My /lib/systemd/system/mosquitto.service file contains following lines

ExecStartPre=/bin/mkdir -m 740 -p /var/log/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/log/mosquitto
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

I try then to remove the package, to delete the PID file and LOG file, to re-install all from scratch but with no result
I change permission to PID and LOG file to root but no results
Littele bit in panic mode I try to rollback to my older version but I was not able to do

any hepl to exit this situation (also with simple roll back) is appreciated !!

br
Davide

@arzaman
Copy link

arzaman commented Feb 10, 2021

I some way I succeed to fix the PID file issue... now mosquitto starts but I'm struggling with web socket and SSL that does not work any more
I check for any suggestions and in case open a new thread
Davide

@sunilcp1
Copy link

I changed the configuration to:

#pid_file /var/run/mosquitto.pid
#port 1883
listener 1883
allow_anonymous true

I'm running two mosquitto's on the same docker host.

#pid_file /var/run/mosquitto.pid
#port 1883
listener 1883
allow_anonymous false

Also works

@sunilcp1
Copy link

Now, it doesnt require pid file

@jamess5
Copy link

jamess5 commented Mar 26, 2021

I corrected this by

  1. Edit /etc/mosquitto/mosquitto.conf and change pid file path line like so
    pid_file /var/run/mosquitto/mosquitto.pid
  2. Altering /lib/systemd/system/mosquitto.service and adding new lines after the other ExecStartPre lines:
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Then run:
systemctl daemon-reload

And then restarted service:
systemctl restart mosquitto.service

Should start fine, confirm with:
systemctl status mosquitto.service

Ubuntu 18.04.5 LTS, 20 may be slightly different, looks like the conf is different path anyway

Hey, I'm having the issue too - however when I tried to edit the /lib/systemd/system/mosquitto.service - it says it cannot find that service.

When I run sudo systemctl status mosquitto it says "Unit mosquitto.service could not be found."

Do you have any suggestions?

@MulMic
Copy link

MulMic commented Apr 22, 2021

I corrected this by

  1. Edit /etc/mosquitto/mosquitto.conf and change pid file path line like so
    pid_file /var/run/mosquitto/mosquitto.pid
  2. Altering /lib/systemd/system/mosquitto.service and adding new lines after the other ExecStartPre lines:
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto: /var/run/mosquitto

Then run:
systemctl daemon-reload
And then restarted service:
systemctl restart mosquitto.service
Should start fine, confirm with:
systemctl status mosquitto.service
Ubuntu 18.04.5 LTS, 20 may be slightly different, looks like the conf is different path anyway

Hey, I'm having the issue too - however when I tried to edit the /lib/systemd/system/mosquitto.service - it says it cannot find that service.

When I run sudo systemctl status mosquitto it says "Unit mosquitto.service could not be found."

Do you have any suggestions?

Look in /etc/systemd/system/multi-user.target.wants

@MulMic
Copy link

MulMic commented Apr 22, 2021

I have the general issue as well (unable to write pid-file), I had created the pidfile manually, but after software update and reboot everything is back to failure.

The suggested additions to mosquitto.service won't work, as mosquitto has no rights to write to /var/run. Changing ownership underneath /var/run is surely not the proper approach.

So the solution is, to either chose a writable directory (i.e. /tmp ) for the pidfile or set up the pidfile manually. (a cron job for root will serve as well)

All of these are kludges and the issue should be fixed by the developers. Especially since the documentation isn't very clear on this and error messages are rather cryptic (mosquitto.service: Start request repeated too quickly.)

btw. I am on Arch Linux and mosquitto 2.0.10-1

@WombatHollow
Copy link

Like everyone, I have also had this issue since I updated to 2.0.11 of Mosquitto earlier this week. I have completely uninstalled mosquitto (but not sure is something remains from v1.6) and reinstalled V2.0.11.

In summary Moz will not start on powerup (I have latest Raspbian on a Pi3B+) which cause also sorts of issues with Nod_red and clients. So I need to fix it.

What I have found is that
sudo chown mosquitto:mosquitto /var/run/mosquitto
before killing and restarting the Moz daemon worked reliably.
ps aux |grep mosquitto to get the PID
sudo kill -9 pid
sudo systemctl daemon-reload
sudo systemctl restart mosquitto.service
worked well but the problem returned on a CPU reboot.

I then put changed the /etc/mosquitto/mosquitto.conf file as follows
...
ExecStartPre=/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=/bin/chown mosquitto:mosquitto /var/run/mosquitto
...

Well that still didn't work on restart, but to restart I just needed
ps aux |grep mosquitto to get the PID
sudo kill -9 pid
sudo systemctl daemon-reload
sudo systemctl restart mosquitto.service
and no 'chown'.

But If I tried to kill this restarted process, it restarted it self ! It would not before as the attempt to start on powerup left a deformed process running that needed to be killed.

Also systemctl status mosquitto.service from the reboot start reports about something to quick.

so maybe we can fix it to delay moz daemon not starting for a few seconds after reboot!

@WombatHollow
Copy link

Just a but more, to add, it appears that the process on restart is being stopped as port 1883 is in use! and a sytemctl Restart fails (Exits with error code.
But after I have restarted the Moz daemon succesfully, systemctl does restart without first killing Moz.
It is like there is another Moz process running (v1.6?) on defaults stopping the systemctl process starting.

@WombatHollow
Copy link

SOLVED.
There was a root CRON job that ran launcher.sh which contained sudo mosquitto -d

How did I find it. Well once I found CRON has a @reboot option I llooked further and found CRON is by user including root. So I winged it and tried sudo crontab -l hoping the use of sudo finds root cronjobs instead of pi user jobs and there as a another job.

After that it was a tidy up of all the other things I had tweaked and re-enable the systemd job on reboot.

so now have 2.0.11 working as I like. I suspect that CRON job was a left over from earlier versions of Mosquitto (I was on 1.5 or 1.6) but the update to 2.0.11 didn't delete it when it created the mosquitto.service job.

@s17534
Copy link

s17534 commented Sep 30, 2021

As a current workaround we can use solution proposed here: https://serverfault.com/questions/482319/how-can-systemd-run-a-command-as-root-before-launching-a-service-as-a-different

I changed startup script (sudo systemctl edit --full mosquitto) to:

ExecStartPre=+/bin/mkdir -m 740 -p /var/log/mosquitto
ExecStartPre=+/bin/chown mosquitto: /var/log/mosquitto
ExecStartPre=+/bin/mkdir -m 740 -p /var/run/mosquitto
ExecStartPre=+/bin/chown mosquitto: /var/run/mosquitto

Now daemon starts without errors.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
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