recovering the last Sage command while it is still running

38 views
Skip to first unread message

John Cremona

unread,
Aug 20, 2020, 12:41:04 PM8/20/20
to SAGE support
Suppose I enter a command at the Sage prompt and it runs and runs,
producing vast amounts of output to the screen (too much to scroll
back), during which I start to wonder if I typed the command correctly
or perhaps added a 0 to a parameter by mistake. I don't want to
interrupt the computation. I thought that there was a Sage history
file somewhere where the last command I gave would be stored but if
there is (as I know there is) it seems to be unreadable. I think it
is in .sage/ipython-*/profile_default/history.sqlite.

The command (which is still running as I type) will probably have
finished before I get an answer, and after that it will be as simple
as pressing the up-arrow key!

John

john_perry_usm

unread,
Aug 20, 2020, 1:27:36 PM8/20/20
to sage-support
On Linux (bash) I use the less command, tell it that yes I want to read the binary file, then press >. That takes me to the end of the history, and I can read what's there. However, it doesn't seem to have the most recent commands.

David Lowry-Duda

unread,
Aug 20, 2020, 1:32:20 PM8/20/20
to sage-support
> I thought that there was a Sage history file somewhere where the last command I gave would be stored but if there is (as I know there is) it seems to be unreadable. I think it is in .sage/ipython-*/profile_default/history.sqlite.

I've never actually tried this, but you're right, it'll be there. And I just tested and it seems to update immediately, even in the middle of executing long-running sage commands. Reading it depends on your preferred sqlite parser.

For instance, in the `sqlite3` command line tool,  you can run `sqlite3 path/to/history.sqlite`. You can then enter a query like

    SELECT * FROM history;

Depending on your sage usage, this might be tremendously long and inconvenient, but this is the basic idea. Default ordering will output commands from more recent sage sessions later, so it might be possible that running this and scanning the last few entries might work. A slightly more sophisticated use might be to use `sqlite3 path/to/history.sqlite "SELECT * FROM history" | grep -C5 <somethinginyourcommand>`. This runs the single SELECT command, pipes it to grep, and then you can examine.

Of course if you've run many similar commands afterwards, this might not be very effective. It might be necessary to actually leverage the structure of the database. For example, if it was actually the last sage command you've run, you could do something like

    SELECT * FROM history ORDER BY  session DESC LIMIT 1;

The schema of the history table can be seen with `.schema history` (note the period before schema and the lack of a semicolon). This will indicate that the history table contains a session integer, a line integer, the source text, and the raw source text.

Good luck! - DLD

Alex J Best

unread,
Aug 20, 2020, 5:09:42 PM8/20/20
to sage-support
At least on my machine opening a new terminal window and starting a new sage session then pressing "up" gives me the previous command (still running) from the old session.

(I'm on 9.2.beta8 in case that's relevant)

Reply all
Reply to author
Forward
0 new messages