psql -f only runs the first query in file - postgresql

I have a file make_partition.sql like:
CREATE INDEX my_nice_index ON mytable (ts);
CREATE TABLE mytable_2017_10 AS SELECT * FROM mytable WHERE date_trunc('month', ts) = '2017-10-01';
CREATE TABLE mytable_2017_11 AS SELECT * FROM mytable WHERE date_trunc('month', ts) = '2017-11-01';
I run it in the background using screen and then the command:
psql postgresql://usr:pw#host.com:5432/db_name -f make_partition.sql
And detach from the screen. It completes, but then it appears that only the first query was run. I've tested this a few times with the same result.
How can I get it to run them all? Do I have to put them in a procedure like this? How to run multiple SQL queries?

When you 'detach' from the screen, what is likely happening is an HUP signal is sent to the process and the script stops executing, the query that is currently executing will finish because it is part of a different process.
You can properly send it to the background and make it safe to detach in a couple of ways, one of the ways is using the command nohup (see man nohup for more) which ignores the HUP signal.
An alternative is to start the script as you have above, then pause it by pressing CTRL+Z, you can then send it to the background using bg and finally you can ensure it is ignoring the HUP signal using disown -h (see man disown for more).

This question is relatively old, and Adam Dadvar's answer provides the basics, but I'll provide one other piece of the puzzle that could trip someone up. If the SQL within your file will create output after each statement, you need to redirect at least the stdout into files, rather than letting it go to the terminal. Doing the latter will mean it's displaying the results and waiting for you to spacebar through the results before running the next command. If your code may produce error that you want to ignore, you should also redirect that output. Concretely, using the disown option, it would look something like:
psql postgresql://usr:pw#host.com:5432/db_name -f make_partition.sql 1> messages.log 2> errors.log
Then use CTRL+Z to suspend the current process, disown -h %1 to disown the process, and bg to "background" the process.

Related

Mplayer - Change track, play/pause with command line

Mplayer provides shortcuts to change the track and play/pause the music but they only work if the mplayer window has the focus.
Is it possible, for example, to change the track currently played in mplayer with a command line? In that case, I could assign this command to a shortcut and use it even if mplayer does not have the focus.
All I found on google was how to use mplayer to play files or which shortcuts to change track when the window has the focus.
I am actually in the middle of trying to do this exact thing! I am going to share what I have discovered so far in case it helps anyone.
I have not been able to find a way to play/pause tracks. However, I have been able to change tracks, at least in terms of going to the next track.
I have found 2 ways so far: Using /proc/$pid/fd/0 and Named Pipes (FIFOs). Explaining FIFOs is a little harder (I just recently learned of them) so I will do that last.
Modifying STDIN via /proc/$pid/fd/0
We can communicate with a process in a different terminal session by redirecting input to the process' file definitions directory at /proc/$pid/fd/ where $pid is the process' PID.
Firstly, find the process ID using pidof. Make sure mplayer is actually running!
$ pidof mplayer
This will output the PID of mplayer. Using this $PID, we can input what we want to STDIN for mplayer by redirecting it to /proc/$PID/fd/0. (Note, 0 refers to STDIN, 1 to STDOUT, etc.). An example:
$ echo "p" > /proc/$PID/fd/0
This has the effect of changing the tracks for mplayer.
However, even though I used p in my example, I have found that you can echo anything (including nothing) and it will still accomplish the same goal of changing the track no matter what you input.
Named Pipes
Also called FIFOs, named pipes, just like regular pipes (|), redirect output to other processes. The main difference is that these are part of the filesystem and can be used to communicate between different terminal sessions.
To start off, create a new named pipe anywhere in your filesystem. I chose /tmp/.
$ mkfifo /tmp/mp3control.pipe
Then start mplayer redirecting its STDIN to the named pipe.
$ mplayer -playlist brutal_tunez.txt < /tmp/mp3control.pipe
It will look like the process hangs and that's because it is expecting the other side to input something.
Now open up a new terminal and input stuff into the pipe.
$ echo "p" > /tmp/mp3control.pipe
You will hear music start to play from mplayer. Again, just like before, any further inputs to the pipe will just change tracks.
I believe this has something to do with the way mplayer handles STDIN and is not the same thing as entering p to play or pause music.
I will continue looking into this but I would love to hear from someone if I made a mistake somewhere. All in all, this might end up being something where the amount of time spent is not worth it, to be honest. (But we still must!)

psql show me the : and ask user input, when running one sql file

I created one new database, then I am using psql to run this sql file(https://github.com/Arelle/Arelle/blob/master/arelle/plugin/xbrlDB/sql/public/xbrlPublicPostgresDB.ddl)
here is my command:
/usr/bin/psql postgresql://db_user:db_password#localhost:5432/my_new_db -f /tmp/xbrlPublicPostgresDB.ddl
psql show me the : , which is asking me to input something.
I checked xbrlPublicPostgresDB.ddl, which does not ask any user input. why does psql ask any user input?
That colon : is not asking for user input. That's from the pager less. By default, when psql outputs something greater than a certain number of lines at a time, it will "paginate" it with less. You can see the same thing by running
cat /tmp/xbrlPublicPostgresDB.ddl | less
You can use arrow keys, page up, page down, j, k, and other keys to move around and see all the output. To exit, press q.
Paginating individual queries doesn't make much sense for what you're running, so you can disable pagination altogether when you run psql by setting the environment variable PAGER to a blank string:
PAGER= psql dbname -f xbrlPublicPostgresDB.ddl
I found the following parameter can stop that pagination, then I do not need to enter any key to continue.
-P pager=off

Cannot press any key to continue with command pause

I wrote a script to remote execute test.cmd file by PsExec.exe.
PsExec.exe \\IP -u administrator -p password "C:\\Users\Administrator\Desktop\test.cmd"
Although I can run the script successfully, I cannot press any key to continue the process when it runs into command pause.
Press any key to continue . . . aaaaaa
The only way to keep going is press Enter.
Besides, if there are many lines of Pause or set /p which waits users to input, once user press Enter, it will affect couple lines of Pause or set /p. It really confuses me.
Best is that you use different logic in accordance with your limitations.
Try exchanging the Pause with a different suitable solution.
Other suggestion would be that you share your code, inputs and expectations (output is already given - the script doesn’t continue). Then someone can review the code and suggest for further steps.
Alternatively, it is also possible that you can fix it by using Console.ReadKey()

How do I enter P4 triggers from standard input on Windows

I need to set the table to perforce triggers from the command line on Windows 8.
I am entering the following command and pressing "enter":
C:\Program Files\Perforce>p4 -H localhost -p 1666 -u Administrator -P ** triggers -i
Text cursor moves to a new line as if it awaits the trigger lines. Good so far. I input triggers one by one and when I am done, I have no idea how to tell p4.exe to "flush" my input and save it. What is the correct way of setting triggers in my situation?
Omit the -i and things are much easier: Perforce will bring up your P4EDITOR, which is Notepad by default I think, and you can edit your trigger table in Notepad and then save it.
The -i option allows you to "Read the trigger table from standard input without invoking the editor". The keyword here is table. You have to enter the entire trigger table, in its proper format, not just your triggers. The trigger table starts with a line that contains the following...
Triggers:
...followed by "a list of triggers; one per line. Each trigger line must be indented with spaces or tabs in the form. Each line has four elements..."
So, entering the trigger table from the command line might look something like this (I'm just entering a single trigger in this example):
C:\>p4 triggers -i
Triggers:
example change-submit //depot/... "cmd %changelist%"
C:\>
The sequence is...
"Triggers:" Enter Tab [trigger line] Enter Tab [trigger line] Enter...
...until you are done entering all of your triggers. When you are done, hit Ctrl+C to "flush" your input and save it.

Stop a script in Matlab

My question is, how do I stop a script by a pressing a GUI button? I already tried to write a code that simulates "CTRL+C" press, but it doesn't work.
I'm not sure there's a way to stop another script from being called. One alternative would be to set a global variable that's periodically checked by the script you wish to stop. If you set the value of a "stop processing" variable to true in your callback, the other script could stop if it found that it was supposed to stop.
Edit
If you'd like to have a GUI option to stop an ongoing process, I would recommend you take a look at something like STOPLOOP on the MATLAB File Exchange.
I won't write the code for you but here's a high-level way to accomplish this:
Display a waitbar with a button on it. Create a callback function for the button which sets a flag to true.
Begin computation inside of a for-loop. In the loop:
1. update the waitbar.
2. call the drawnow function so that the callback is executed properly. Remember MATLAB is single-threaded, so this is necessary or the callback will not execute until the script finishes.
3. perform any other computation
4. check for the flag set to true. if it is true, return to stop execution.
The flag could be a global variable, or a handle-based object (so that it is passed by reference).
EDIT:
This answer is not applicable for the current question.
This answer is applicable only for scripts having the first line = #!/usr/bin/matlab
use pkill without option will send a TERM signal:
pkill yourscriptname
If you really want the same signal as CTRL+C then:
pkill -3 yourscriptname
If your script still does not stop, you can use the most aggressive signal KILL:
pkill -9 yourscriptname
Of course, if you known the PID (Process IDentifier), you can simply use kill:
kill yourPID
kill -3 yourPID
kill -9 yourPID
You can have more info about signals using one of these commands:
man 7 signal
kill -l
info signal
I don't do a lot of GUIs, but for debugging purposes I would try to set the button callback to #keyboard. That is, something like:
set(handleToGuiButton,'Callback',#keyboard)
To actually stop execution you would need to somehow communicate this button press into the loop that was executing, for example via global variables, or something fancier (e.g. https://stackoverflow.com/a/8537460/931379)
But I would honestly look at the stoploop link (from another answer) before going down any of these routes.