2

I am trying to create a batch file to run start some micro services and database

   1 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1000') DO @ECHO TaskKill.exe /PID %%P
   2 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :1001') DO @ECHO TaskKill.exe /PID %%P
   3 FOR /F "tokens=4 delims= " %%P IN ('netstat -a -n -o ^| findstr :5432') DO @ECHO TaskKill.exe /PID %%P
   4 start cd "C:\Program Files\PostgreSQL\10\bin\" & pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start 
    @REM to start service
   5 start javaw -jar -Dserver.port=1000 text-annotation-tool-1.0-SNAPSHOT.jar

Line 1 to 3 and Line 5 execute correctly when executed by commenting line 4.

Line 4 is to start a Postgres server in a new prompt (beacuse of the Dir change). I think the problem is with the way I have used quotes. The 'start' at the beginning and ending of line 4 serve different purpose.

Also, if I execute line 4 in different prompt, How do I close the prompt after execution (nohup equivalent)

1 Answer 1

3

There are two errors: You can't "pass" cd to the start command. And start has the quirk to interpret the first quoted parameter as the new window's title. So start "C:\Program Files\PostgreSQL\10\bin\" ... wouldn't work, you need to supply a dummy window title. The & also seems wrong

So you need:

start "Postgres Server" "C:\Program Files\PostgreSQL\10\bin\pg_ctl.exe" -D "c:\Program Files\PostgreSQL\10\data" start

As the full path to pg_ctl.exe is supplied, there is no need for a cd. But if you want to define the default directory for the new process, you have to use the /D parameter:

start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start

Unrelated, but: putting the Postgres data directory into c:\Program Files\ is a very bad idea. That directory has special permissions for a purpose. You should use %ProgramData% or %AppData% instead

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, you are right about the 'data' directory. This one was a default setup of postgres. Is there a nohup equivalent for that new process. I mean, the command is executed in the "Postgres Server" process. can we suppress this prompt
@Betafish: not with plain batch scripting. I did something like that with a .vbs script.
Can I use a similar command to first stop and then start the postgres server start "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" stop "Postgres Server" /D "C:\Program Files\PostgreSQL\10\bin" pg_ctl.exe -D "c:\Program Files\PostgreSQL\10\data" start
I wouldn't run pg_ctl ... stop using the start command as you want to wait for the outcome of that before you re-start Postgres. So pg_ctl -D ... stop then start "postgres" pg_ctl -D ... start

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.