0

I am trying to create a .bat file that will run automatically during a startup on my server. I want the .bat file to open CMD, go to a given directory and then run a Python script from there.

Once the Python script has been activated, I want a separate CMD window to run a shutdown timer so that after a given time (t=86400, daily) the system will reboot. This is my way of making sure the file will continue to run after I disconnect to my server.

My current code is

@echo off
start cmd /k cd C:/Users/Administrator/Documents/
python scraperv2.py
start cmd /k shutdown -t 86400 -r -f

This code will go to the directory C:/Users/Administrator/Documents but it will not run the Python script. Please note that Python is set as a PATH variable.

What do I need to do to get this script to work?

5
  • what error do you get ? is scraperv2.py in the PATH ? Commented Feb 1, 2018 at 9:50
  • what are you trying to accomplish by using start ..? Commented Feb 1, 2018 at 9:51
  • There is no specific error. It simply just does not run the script. My output when running the file is 2 CMD windows. Screen 1 has accessed the given directory. Screen 2 has run the shutdown timer command. However, there is no sign of the python script being active Commented Feb 1, 2018 at 9:54
  • I used start because I thought it was a prerequisite for opening a program. I removed it but the issue remains Commented Feb 1, 2018 at 10:02
  • 1
    start cmd /k cd C:/Users/Administrator/Documents/ will start a new instance of cmd.exe but will not change the directory of the current one. Why you just dindn't used cd /d "C:/Users/Administrator/Documents/" Commented Feb 1, 2018 at 10:16

2 Answers 2

2

Ensure that you are able to run python from cmdline using any path to make sure it is actually in your environment, then simply do:

@echo off
python "C:\Users\Administrator\Documents\scraperv2.py" && shutdown /r /f /t 86400

Which will call python and the script from directory and if successful (%errorlevel%==0), it will do shutdown command. If %errorlevel% is anything other than 0 it will not run the shutdown command.

If you really want to do the cd then simply do this:

@echo off
cd /d "C:\Users\Administrator\Documents"
python scraperv2.py && shutdown /r /f /t 86400
Sign up to request clarification or add additional context in comments.

Comments

-1

This should work now:

@ECHO OFF
START CMD /K "CD C:/Users/Administrator/Documents && python scraperv2.py"
START CMD /K "SHUTDOWN /R /F /T 86400"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.