I'm trying to run a batch file to execute many commands in 1 psql shell
I'm using Postgres version 11.4
This is my code:
#ECHO OFF
"C:\Program Files\PostgreSQL\11\bin\psql.exe" "dbname=databasename
host=hostname user=username password=#bcd1234 port=5432 sslmode=require"
DELETE from my_table1;
DELETE from my_table2;
DELETE from my_table3;
PAUSE
I expect the script delete all data from 3 tables, but it only run the first command line to login Postgres.
You can execute multiple commands by executing them from a file.
Create a file and write all your commands in it.
Use the -f option to pass the file as the source of commands.
Please refer: (-f option) https://www.postgresql.org/docs/9.1/app-psql.html
Related
I am a regular FME user and I know how to create a postgis database to be used in FME (using PGadmin). I now want to use FME to backup and then delete a generated table. I use the 'systemcaller' transformer for this, the systemcaller basically opens command prompt.
I was able to make a backup file using cd C:\Program Files\PostgreSQL\118\bin\ && set PGPASSWORD=password&& pg_dump.exe -U postgres -p 5433 -d bag -t tablename -F c -f C:/Users/username/Downloads/tablename.backup", but I am having a hard time getting the table to be deleted.
I tried the 'drop table' command, but this is not recognized. It will recognize drobdb, but I obviously do not want to drop the whole database.
What commandline should I use to drop te table 'testtable' in the database 'testdatabase'?
I'm using the 'qwinsta' cmd command to get the session ID of a remote computer and output it to a textfile, so I create a new batch file and write the command then I try running the batch file through python but it only returns the first line of the output. When I run the batch file by simply double-clicking it it works properly.
Using python 2.7:
def run_qwinsta(self, computerName):
qwinsta_check = open("q.bat", "w")
qwinsta_check.write('PsExec -u <username> -p <password> \\\\' + computerName + ' qwinsta' + ' > "q.txt" ')
qwinsta_check.close()
os.system("q.bat")
Expected results:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
>services 0 Disc
console <username> 1 Active
rdp-tcp 65536 Listen
Actual results:
SESSIONNAME USERNAME ID STATE TYPE DEVICE
I would recommend you to avoid writing the batchfile, If you can. You can execute your batch command from os.system(). Also you can try using subprocess (documentation here) and then redirecting the stdout and stderr to file.
EDIT:
PsExec is a good choice, but If you want another way, you can also use ssh.
You can run PsExec from os.system() and then write the response to text file on the remote machine. The default PsExec working directory is System32 there you can find your text file.
Tested code:
import os
os.system('Psexec \\\\SERVER cmd.exe /c "tasklist > process_list.txt"')
I used tasklist, because I don't have qwinsta on my remote machine.
If you want to store the PsExec response on your machine you can use subprocess and then redirect the stdout to text file.
Tested code:
import subprocess
process_list = open("process_list.txt", "w")
subprocess.call(['Psexec', '\\\\SERVER', 'tasklist'], stdout=process_list)
process_list.close()
Actually I used Python 3.x, because I don't have Python 2.x, but it should work on both.
If this still didn't solve your problem, please provide more details to your question!
I'm trying to use a batch FOR loop inside a psexec. I need to make a script that offers me the possibility to choose the subfolders (I store the items in %list_of_items%) in %local_folder% that are needed to be copied with ncftpput on remote server. I cannot put the entire psexec inside the loop because I dont want to insert every time the password.
The piece of code:
psexec \\remote_server -u DOMAIN\user cmd /c FOR %%i IN "%list_of_items%" DO ("ncftpput -f c:\folders\file_with_creds.cfg -R remote_folder/ %local_folder%/%%i")
Example
I have:
%local_folder%\folder111
%local_folder%\folder222
%local_folder%\folder333
I choose to copy the items folder111 and folder333 so I store them in %list_of_items%. For every item in the list I need to run ncftpput to transfer to remote server the folder but doesnt work....
The error:
"folder111" was unexpected at this time.
cmd exited on remote_server with error code 1.
Can you help me please to find what I'm doing wrong?
Thank you.
My shellscript for taking backup of databse works fine normally.
But when i try to run through crontab there is no backup.
this is mycrontab
* * * * * /home/mohan/sohan/backuptest.sh
content of backuptest.sh are
#!/bin/bash
name=`date +%Y%m%d`.sql
#echo $name
mysqldump -u abc --password=abc my_db > $name
backup.sh works fine when normally run .But fails to generate backup when run through crontab
A couple of possibilities... first that your programs/commands cannot be found when run from cron, and second that your database cannot be found when run from cron.
So, first the programs. You are using date and mysqldump, so at youir Terminal prompt you need to find where they are located, like this:
which date
which mysqldump
Then you can either put the full paths that you get as output above into your script, or add a PATH= statement at the second line that incorporates both paths.
Secondly, your database. Where is it located? If it is in /home/mohan/sohan/ for example, you will need to change your script like this:
#!/bin/bash
name=`/bin/date +%Y%m%d`.sql
cd /home/mohan/sohan
/usr/local/bin/mysqldump -u abc --password=abc my_db > $name
I'm trying to create a script just to run other scripts and do some extra stuff in case of successful or failure.
You have the full code on this link just to try to be clear what I'm trying to achieve.
Basically I want to:
-- DO SOME STUFF HERE
r: "$(fileName)"
-- MORE STUFF HERE
and call it from sqlcmd this way:
sqlcmd -i "RunScript.sql" -v fileName="someFileName.sql" -s server -d database
But I can't, I'm getting the following error:
Msg 102, Level 15, State 1, Server SERVER, Line 19 Incorrect syntax
near 'someFileName.sql'.
So, it seems that the little r: couldn't be used with a parameter on his side.
Just to clarify, someFileName.sql isn't in the SQL Server, but in my machine, so I couldn't use this way to read the file. In fact, I just tried it later.
Is there a workaround to archive this? Any ideas to solve it?
You could break your current RunScript.sql script into separate header and footer SQL scripts and concatenate them together with a SQL script in the middle that is denoted by an input parameter to a CMD script. For example:
RunSQL.CMD consists of:
#ECHO OFF
COPY /V /Y RunScriptHeader.sql + %1 + RunScriptFooter.sql RunScriptTemp.sql
SQLCMD -i "RunScriptTemp.sql" -s server -d database
The /V does a verify on the copy
The /Y suppresses prompting to confirm overwriting an existing file
You would run it as follows:
RunSQL.CMD someFileName.sql