how do i use launchctl for mongod on macosx without homebrew to run the service as mongodb at startup
0
votes
0
answers
37
views
I'm wondering why I can run this command from terminal:
sudo su mongodb -c "mongod --config /etc/mongod.conf"
but the LaunchAgent cannot run this command on login as root, so that my mongodb can start always as a service. I think it is related to LaunchAgent not knowing the root password, because when I run the command from terminal - naturally I need to input the password for my user.
I can run the mongod binary in the way mongo recommends. Meaning, I am running it as a user I created named "mongodb". The user I am logged in as does not have read/write access to any of the "mongodb" data directories, etc, which is also the recommended security configuration. But my user is a sudoer, and can "su" (switch users) to the mongodb user and run a command with sudo.
However, launchctl is unable to run the service when my user logs in. I created and successfully bootstrapped and started my service plist under
/Library/LaunchAgents/com.my.test.plist
Below are snips from example plist configs I've tried and their resultant errors:
ProgramArguments
su
mongodb
-c
"mongod --config /etc/mongod.conf"
Service log error is: *su: Sorry*
ProgramArguments
sudo
su
mongodb
-c
"mongod --config /etc/mongod.conf"
Service log error is: *sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper*
I do not want to use the -S option, because that would be awful to store a root password somewhere on my system. Also the command needs to be run using "sudo" because per mongoDBs pre-requisites, the use of the mongodb data directory, the /etc/mongod.conf file, the log directory, and the mongod binary itself are all only supposed to be owned by and usable by the "mongodb" user (which is why they create the "mongodb" user in the first place instead of just advocating that you use your regular login user?)
UserName
mongodb
ProgramArguments
mongod
--config
/etc/mongod.conf
There are no errors written to the service file with this configuration, but the service is not running. When I check why with
launchctl print gui/$(id -u)/com.my.test.plist
I find that *last exit code = 78: EX_CONFIG*
sysexits
I'm wondering why I can run this command from terminal:
sudo su mongodb -c "mongod --config /etc/mongod.conf"
but the LaunchAgent cannot run this command on login as root, so that my mongodb can start always as a service.
**please refrain from any homebrew responses.** The point of this installation is to not use homebrew (it's not needed) and to learn how to use launchctl directly as mongo intends.
Also I thought to try and put my service in **/System/**Library/LaunchAgents/com.my.test.plist (because maybe the LaunchAgent would not be prompted to enter the root password then?) but this is also not possible, since this directory and kind of service is reserved for software services created by Apple. I’ve also tried the /Library.
/LaunchDaemon to some extent with variations of the plist configs above with similar results. I also tried using
crontab -e
And having @reboot my.sh scripts running without success
Asked by user1709076
(101 rep)
Oct 21, 2024, 04:57 PM