Mongodb upstart script

Tom tommedema at gmail.com
Sat Mar 17 14:56:09 UTC 2012


Thanks Evan and sorry for misspelling your name earlier.

For those who face similar problems I will share my experiences. I
played around with this today, scanned most of the cookbook, and came
to the conclusion that Ubuntu does not yet provide version 1.4 with
the normal package distributions.

As such, I had to use start-stop-daemon. This is my configuration file:

        # Mongodb Ubuntu upstart file at /etc/init/mongodb.conf

        description "Runs mongod server daemon as service."

        start on runlevel [2345]
        stop on runlevel [06]

        respawn
        respawn limit 10 100

        pre-start script
            mkdir -p /home/mainuser/data/db
            mkdir -p /home/mainuser/data/logs
        end script

        exec start-stop-daemon --start -c mainuser --exec
/home/mainuser/apps/mongodb/bin/mongod -- --journal --nohttpinterface
--dbpath /home/mainuser/data/db --logpath
/home/mainuser/data/logs/mongodb.log --logappend --port 30000

It seems to work. I do not know why I do not need to add expect fork
or expect daemon, but with those it does not work. Without it does
work.

I reckon version 1.4 would make this a lot more elegant, like so:

    # Mongodb Ubuntu upstart file at /etc/init/mongodb.conf

    description "Runs mongod server daemon as service."

    setuid mainuser

    start on runlevel [2345]
    stop on runlevel [06]

    expect fork
    #expect daemon

    #respawn
    #respawn limit 10 100

    pre-start script
        mkdir -p /home/mainuser/data/db
        mkdir -p /home/mainuser/data/logs
    end script

    exec /home/mainuser/apps/mongodb/bin/mongod --fork --journal
--nohttpinterface --dbpath /home/mainuser/data/db --logpath
/home/mainuser/data/logs/mongodb.log --logappend --port 30000

Thanks again!

2012/3/17 Evan Huus <eapache at gmail.com>:
> The cookbook has the answer [1] :)
>
> TLDR: If you're running upstart 1.4 or later, you can use the 'setuid' and
> 'setgid' stanzas. Otherwise you'll have to use su, sudo or
> start-stop-daemon. Start-stop-daemon is recommended, because su and sudo do
> a bunch of extra stuff to the PAM session that is probably unnecessary for a
> daemon process.
>
> Cheers,
> Evan
>
> [1] http://upstart.ubuntu.com/cookbook/#changing-user
>
>
> On Sat, Mar 17, 2012 at 8:28 AM, Tom <tommedema at gmail.com> wrote:
>>
>> Thanks Even.
>>
>> I will read through the cookbook and hopefully it will indeed answer
>> my questions.
>>
>> About the su command, I read that this was the only way to run a
>> process as an unprivileged user with upstart here:
>>
>> http://superuser.com/questions/213416/running-upstart-jobs-as-unprivileged-users
>>
>> "A future release of Upstart will have native support for that, but
>> for now, you can use something like:
>>
>> exec su -s /bin/sh -c 'exec "$0" "$@"' username -- /path/to/command
>> [parameters...]"
>>
>> Is there a better way of doing this nowadays?
>>
>> Tom
>>
>> 2012/3/17 Evan Huus <eapache at gmail.com>:
>> > Hi Tom,
>> >
>> > As per the upstart cookbook [1], you'll likely need a few extra stanzas.
>> >
>> > According to the 'expect' section [2] you'll probably need to add
>> > 'expect
>> > daemon' to allow upstart to track the proper process ID when mongodb
>> > forks
>> > in order to daemonize.
>> >
>> > Also, I'm not sure what you're trying to do with the "su -s /bin/sh -c
>> > exec". If you're trying to change users [3] you probably shouldn't be
>> > using
>> > su. If you need the program to be run in a shell environment for some
>> > reason, you can simply use 'script' [4] instead of exec. If you're
>> > simply
>> > using it to fork so that it daemonizes properly instead of only forking
>> > once, you can change 'expect daemon' to 'expect fork' and upstart will
>> > track
>> > the process ID correctly.
>> >
>> > There are a lot of good resources in the cookbook [1], so hopefully it
>> > will
>> > answer any other questions you might have.
>> >
>> > Cheers,
>> > Evan
>> >
>> > [1] http://upstart.ubuntu.com/cookbook/
>> > [2] http://upstart.ubuntu.com/cookbook/#expect
>> > [3] http://upstart.ubuntu.com/cookbook/#changing-user
>> > [4] http://upstart.ubuntu.com/cookbook/#script
>> >
>> > On Sat, Mar 17, 2012 at 7:32 AM, Tom <tommedema at gmail.com> wrote:
>> >>
>> >> Hi folks,
>> >>
>> >> I'm trying to run mongod as a deamon using upstart under the
>> >> unprivileged mongouser account.
>> >>
>> >> The following appears to work great:
>> >>
>> >> # Mongodb Ubuntu upstart file at /etc/init/mongodb.conf
>> >>
>> >> description "MongoDB deamon"
>> >>
>> >> pre-start script
>> >>    mkdir -p /home/mongouser/data/db
>> >>    mkdir -p /home/mongouser/data/logs
>> >> end script
>> >>
>> >> start on runlevel [2345]
>> >> stop on runlevel [06]
>> >>
>> >> exec su -s /bin/sh -c 'exec "$0" "$@"' mongouser --
>> >> /home/mongouser/Applications/mongodb/bin/mongod --fork --journal
>> >> --nohttpinterface --dbpath /home/mongouser/data/db --logpath
>> >> /home/mongouser/data/logs/mongodb.log --logappend --port 30000
>> >>
>> >> ------------
>> >>
>> >> When I reboot the system mongod indeed starts properly. However, here
>> >> are the issues:
>> >> 1. when I do sudo status mongodb I get "stop/waiting" even though it is
>> >> running
>> >> 2. when I do sudo start mongodb it starts the deamon again, even
>> >> though mongod is already running.
>> >>
>> >> Can anyone tell me what is going wrong here?
>> >>
>> >> --
>> >> upstart-devel mailing list
>> >> upstart-devel at lists.ubuntu.com
>> >> Modify settings or unsubscribe at:
>> >> https://lists.ubuntu.com/mailman/listinfo/upstart-devel
>> >
>> >
>>
>> --
>> upstart-devel mailing list
>> upstart-devel at lists.ubuntu.com
>> Modify settings or unsubscribe at:
>> https://lists.ubuntu.com/mailman/listinfo/upstart-devel
>
>



More information about the upstart-devel mailing list