12

I am trying to run this command from bash script:

 mongo 192.168.10.20:27000 --eval "use admin && db.shutdownServer() && quit()"

but i get this error :

[rs.initiate() && use admin && db.shutdownServer() && quit()] doesn't exist

how can i do it without using a js file?

MoienGK
  • 4,544
  • 11
  • 58
  • 92

3 Answers3

23

There are differences between interactive & scripted mongo shell sessions. In particular, commands like use admin are not valid JavaScript and will only work in an interactive shell session.

The working equivalent of your shutdown command line would be:

mongo 192.168.10.20:27000/admin --eval "db.shutdownServer()"

You can include the database to use in the connection string, and there is no need to quit from a scripted mongo shell session.

If you do need to change databases from a scripted session, there is a db.getSiblingDB() JavaScript function. An alternative way to write the shutdown command above would be:

 mongo 192.168.10.20:27000 --eval "db=db.getSiblingDB('admin');db.shutdownServer()"
Stennie
  • 63,885
  • 14
  • 149
  • 175
  • 2
    @MoienGK Yes, you can put longer JavaScript in the `--eval`. Separate multiple statements with semicolons as per my second example. – Stennie Jun 24 '15 at 14:08
  • unexpected error: "shutdownServer failed: shutdown must run from localhost when running db without auth" – MoienGK Jun 24 '15 at 14:13
  • thanks, is putting && instead of semi colon, makes second command dependent on first one's success? – MoienGK Jun 24 '15 at 14:15
  • 1
    The error message explains the issue: your version of MongoDB only accepts shutdown command from localhost unless you have authentication enabled. You should enable authentication on your MongoDB instance or work out a different approach for shutting down the server (i.e. run your bash command via ssh). The `&&` syntax is specific to bash commands, not JavaScript. If you want to write a more advanced script with error checking I would encourage you to save this in a JS file which can also be passed in a bash command line (`mongo foo.js`). What's your reason for not wanting to use a JS file? – Stennie Jun 24 '15 at 14:23
  • actually i started with .js but it bothered me a bit , i thought too much headache for small code. but it seems that i was wrong – MoienGK Jun 24 '15 at 14:27
  • FYI, example of running the same command line via ssh (if you don't want to enable auth for some reason, though doing so is definitely recommended): `ssh user@192.168.10.20 'mongo localhost:27000/admin --eval "db.shutdownServer()"'`. Replace "user" with a valid username on the remote server. – Stennie Jun 24 '15 at 14:31
  • there is one other problem. there is no mongo (client or shell) is installed on remote server.so i can not use commands that i need from remote server. so i guess sending the js file to remote server wont solve auth problem – MoienGK Jun 24 '15 at 14:34
  • If you don't want to install a `mongo` shell on the database server, I would recommend enabling authentication (even if this is on your local network). There's a [MongoDB security checklist](http://docs.mongodb.org/manual/administration/security-checklist/) which has a list of best practices. – Stennie Jun 24 '15 at 14:40
12

You can use heredoc syntax.

#! /bin/sh
mongo <<EOF
use admin
db.shutdownServer()
quit()
exit

Turns out heredoc syntax throws a warning when EOF is missing at the end for bash script. This is the bash script version.

#! /bin/bash
mongo <<EOF
use admin
db.shutdownServer()
quit()
EOF

Here is the output, I guess this is what you expected.

MongoDB shell version: 2.4.14
connecting to: test
switched to db admin
Wed Jun 24 17:07:23.808 DBClientCursor::init call() failed
server should be down...
Wed Jun 24 17:07:23.810 trying reconnect to 127.0.0.1:27017
Wed Jun 24 17:07:23.810 reconnect 127.0.0.1:27017 ok
Wed Jun 24 17:07:23.812 Socket recv() errno:104 Connection reset by peer 127.0.0.1:27017
Wed Jun 24 17:07:23.812 SocketException: remote: 127.0.0.1:27017 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:27017] 
Wed Jun 24 17:07:23.812 DBClientCursor::init call() failed
Ugur
  • 1,679
  • 14
  • 20
  • this seems correct. but i have other irrelevent issue now: shutdown must run from localhost when running db without auth.thanks for reply – MoienGK Jun 24 '15 at 14:19
4

From the mongo docs:

--eval option

Use the --eval option to mongo to pass the shell a JavaScript fragment, as in the following: mongo test --eval "printjson(db.getCollectionNames())"

You can also put your JS Fragments into a .js file then do:

mongo < myScript.js

You may also find more useful stuff in this SO question

Community
  • 1
  • 1
Hamed
  • 869
  • 10
  • 26