Skip to main content
Added updated script and output
Source Link
DB guy
  • 69
  • 2
  • 8

I have been writing a script to find and kill slow mongodb queries, how can I store output of db.currentOp() in a variable?

current I am using this but failing

#! /bin/bash

out=""
mongo << EOF

out=db.currentOp();
EOF
echo"----------------------------"
echo"$out"

Updated Script

#! /bin/bash

out=$(mongo --eval 'db.currentOp({op:"query"})')
echo"-----------------------"
echo"$out"

Output

./kill_slow_mongo_queries.sh: line 4: echo-----------------------: command not found
./kill_slow_mongo_queries.sh: line 5: echoMongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("ce96f3ee-f5e1-49b5-aeef-b5766328dda3") }
MongoDB server version: 3.6.8
{ "inprog" : [ ], "ok" : 1 }: No such file or directory

I have been writing a script to find and kill slow mongodb queries, how can I store output of db.currentOp() in a variable?

current I am using this but failing

#! /bin/bash

out=""
mongo << EOF

out=db.currentOp();
EOF
echo"----------------------------"
echo"$out"

I have been writing a script to find and kill slow mongodb queries, how can I store output of db.currentOp() in a variable?

current I am using this but failing

#! /bin/bash

out=""
mongo << EOF

out=db.currentOp();
EOF
echo"----------------------------"
echo"$out"

Updated Script

#! /bin/bash

out=$(mongo --eval 'db.currentOp({op:"query"})')
echo"-----------------------"
echo"$out"

Output

./kill_slow_mongo_queries.sh: line 4: echo-----------------------: command not found
./kill_slow_mongo_queries.sh: line 5: echoMongoDB shell version v3.6.8
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("ce96f3ee-f5e1-49b5-aeef-b5766328dda3") }
MongoDB server version: 3.6.8
{ "inprog" : [ ], "ok" : 1 }: No such file or directory
Source Link
DB guy
  • 69
  • 2
  • 8

Save output of a Mongo Command to a Variable bash script

I have been writing a script to find and kill slow mongodb queries, how can I store output of db.currentOp() in a variable?

current I am using this but failing

#! /bin/bash

out=""
mongo << EOF

out=db.currentOp();
EOF
echo"----------------------------"
echo"$out"