Learn, Share, Build

189
October 07, 2017, at 11:40 PM

I need to run a script in python that takes weekly backups of my mongodb database. I have used task scheduler in windows 8.1 and had set the script to run on a particular weekday at a particular time. The point is that my python code needs to produce 2 instances of command line window so that in first instance it can enter "mongod" commmand that establishes the mongoserver connection and in second instance of cmd window ,"mongodump" command needs to be entered and these have to be done and accomplished in this very order only.I have used 'os' module in python and used " os.system('desired cmd command ') " but if using 'os.system' the second time to initiate the 2nd cmd instance ,it closes down the first cmd instance window and runs self(the second instance)so the desired process gets disrupted.I need to ensure that the first command 'mongod' and second command 'mongodump' are to be entered automatically in respective cmd windows from already written python code itself and not to be entered anything manually or from outside. Things I have tried are: : 1)

import os  
os.system('mongod')      //this stops
os.system('mongodump')  //this starts

2)

import os
os.system('mongod && mongodump')  

//the first continues and 2nd command is never reached

therefore I need to run 2 separate instances of cmd window as the command 'mongod' establishes the connection and nothing can be written after that in cmd window and therefore 'mongodump' command can only and needs to be executed in other cmd window. Please note that even if two instances of cmd windows can be launched but entering the mongod and mongodump commmand cannot be done manually by the user and it has to be predefined in the python code itself.

Answer 1

Looks like you're looking for an async subprocess. https://docs.python.org/3/library/asyncio-subprocess.html#asyncio.create_subprocess_exec

Answer 2

You may be able to do this with a simple Windows .bat or .cmd script, like this:

START /B "" mongod
TIMEOUT /T 10 /NOBREAK
mongodump

Just run this at the right time and it should launch both processes for you. I think the mongod process will automatically get terminated once the main script finishes (see https://superuser.com/questions/198525/how-can-i-execute-a-windows-command-line-in-background#comment1569081_591084).

Rent Charter Buses Company
READ ALSO
Learn, Share, Build

Learn, Share, Build

I have a dictionary with strings as keys, and also strings as valuesFirst, I want to find the most frequent key in my dictionary

220
Learn, Share, Build

Learn, Share, Build

I'm working on a Django (110 & Python3) project in which I need to implement a logic as: We have articles from various categories, the user needs to tag these articles, mean when user read articles then he needs to provide his feedback rather it's relevant...

213
Learn, Share, Build

Learn, Share, Build

This is my code:

240
Learn, Share, Build

Learn, Share, Build

This question already has an answer here:

231