0

So usually I start a mongod process with 2 arguments like this

~$ mongod --dbpath /media/nhuyvan1106/0CB6-189A/linux/mongodb/data --logpath /media/nhuyvan1106/0CB6-189A/linux/mongodb.

But I don't want to specify the arguments every time, so I saved them to a text file called mongo.txt with this following content.

--dbpath /media/nhuyvan1106/0CB6-189A/linux/mongodb/data --logpath /media/nhuyvan1106/0CB6-189A/linux/mongodb

Then I cat that file and pipe it through mongod like this

~$ cat mongod.txt | mongod

But it does not work, mongod still uses the default value /data/db for the argument --dbpath and same for the other argument. Does any know how I can achieve this?

  • Have you tried mongod $(cat mongod.txt)? You could also look into aliases. – earthmeLon Jan 27 '17 at 22:53
  • @earthmeLon so based on your comment. I've added mongod to the beginning of the text file in addition to the arguments I've already had in there and it works now. Thank you. Post as an answer, I'll accept it – Teedeez Jan 27 '17 at 23:01
  • There's a much easier approach for passing a collection of arguments to mongod: use a configuration file and specify this via --config /path/to/conf or -f /path/to/conf. Is there a reason you need to pipe the arguments instead? – Stennie Jan 28 '17 at 5:35
  • @Stennie So in /etc/mongod.conf file, under the storage section, I changed the value of dbPath to my path, and under systemLog section, I changed path to the path as in my question, but when I type mongod on the terminal, it still shows /data/db as the location for the data directory and gives a write permission error because I don't run it with sudo that's why I need the arguments every time I run mongod which is annoying to be honest – Teedeez Jan 28 '17 at 5:48
  • Are you passing the configuration file as a parameter to mongod (eg: mongod --config /etc/mongod.conf)? There is no default configuration file location baked into mongod. – Stennie Jan 28 '17 at 7:10
1

You're on the right path, but you're trying to pipe the output of cat mongod.txt. Piping doesn't mean the output will get added as the arguments. Instead, when you pipe data, the receiving program has to know how to deal with the pipe'd data.

For example if we have banana.txt with the text Test:

  1. cat banana.txt | echo
    • Empty line as output
  2. echo "$(cat banana.txt)"
    • Test as output

Instead of piping, we're asking bash to evaluate cat banana.txt, and insert that there immediately, and before executing echo.

While there are thousands of actual examples, a few examples of where you would want to pipe would be with tools like grep and sed, and dd. Look into 'bash piping', 'bash evaluation/expansion', and stuff like STDIN.

  • The issue in your example is not with piping, but with echo. Piping doesn't work with cat file.txt | echo simply because echo requires command-line argument, and doesn't read stdin stream provided by pipe. If you have command that requires command-line arguments, you need xargs. Compare this to cat banana.txt | xargs -L1 echo ( which is a terrible waste of resources anyway, but for the sake of example, I'll repress the cringe ). – Sergiy Kolodyazhnyy Jan 27 '17 at 23:22
  • Additional note: if the file contains multiple lines in the file, or it's important to preserve the structure of the file ( avoid word splitting at tabs and newlines) , then quote command substitution as "$(cat file.txt)" – Sergiy Kolodyazhnyy Jan 28 '17 at 0:05
  • @Serg That was some extremely helpful info – Teedeez Jan 28 '17 at 5:51
2

Ubuntu , and all Linux distributions with GNU utilities installed, have xargs , which allows running a command with arguments piped from another command or from provided from stdin. In your case , what you would want to do is the following:

xargs  mongod < mongo_args.txt

Here you have mongo_args.txt which is the text file with your arguments, and it's being redirected as stdin to xargs. That will be joined together with mongod and ran as complete command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.