Register
It is currently Wed Oct 01, 2014 4:19 pm

KDE Konqueror crash recovery


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sun Dec 03, 2006 8:25 pm   

Joined: Wed Sep 06, 2006 12:19 pm
Posts: 54
Location: Covington, WA
Here's a couple of scripts which can be used to help you resume where you left off, if for some reason Konq crashes, or the computer (such as loss of power, which is more frequent than I like where I currently live at).....

I use Konqueror more than any other browser, and tend to have many tabs opened up at the same time when researching for a project I happen to be working on.......It's quite frustrating when all of a sudden a crash occurs..........I do use Firefox at times (actually Swiftfox), but more for troublesome sites that complain because I'm not using a "popular" browser, or for online transactions such as shopping or bill paying....

A little background info on how KDE and Konq work - It's true Konqueror has an automatic session recovery of sorts already, but the problem is it recovers a previously SAVED session which is restored when first logging in to KDE.........NOT the session you were working on at the time of the crash...... :-(

So I came up with these little scripts to make my life a bit easier, and anyone else's, by using the 'kfmclient' command line syntax to restore the URLs found in the crash log....This crash log is a temporary running log file that is written to at the time a tab is opened or closed (on-the-fly), when a new browser window is opened up........When the browser window is finally closed, the log file is deleted automatically to avoid cluttering up the user's temp directory......Therefore, if a crash occurs, the log file never gets deleted........

The first script (log2url) mines the active URLs found in the crash log, ignoring any tabs that were closed by the user during the course of the session, and builds a list of URLs.....This list has the same name as the crash log, but replaces the ".log" extension with a ".urls" extension, and drops it in the user's $KDEHOME directory ($HOME/.kde)......I also use this to not only recover from crashes, but as a way to save sessions for a particular project I'm working on.......I'm a command line freak, so I'm more comfortable saving sessions this way than saving them as Konqueror "profiles".....

The second script (konqrestore) opens up a new browser window and populates it with opened tabs using the URLs from the list generated by the first script........This script can be used directly on crash logs, as well as the saved URL lists..........For the crash logs, it merely calls on the first script to convert the log into a URL list, then uses the newly saved list to restore a session, which also means 'konqrestore' depends on 'log2url' when working with log files........

Both scripts will accept "file://" paths, which means you can right-click on the desired file in the Konq filemanager and choose "Copy", then pasting either in the "Run Command" dialog box, or a Konsole window.........The argument is optional, meaning they will find the most recent files in the default places if no file was specified as an argument - log2url will look in the user's KDE temp directory, which is '/tmp/kde-$USER', for the latest crash log; konqrestore will look in '$HOME/.kde' for the latest URL list.......

Caveat emptor: It's best if there are no other Konqueror windows open in the current desktop (Desktop 1, for example), including the Konqueror filemanager...........What can happen is a new browser windows opens up with the first URL in the list, but then the rest of the URLs are opened up as tabs in the other Konqueror window.....This only applies to the windows open in the same desktop, not opened ones in the other desktops (2, 3, 4, etc).........This behavior doesn't always happen, but it does more often than not....... ;-)

Anyway, I hope you KDE fans find them as useful as I do.....

log2url
Code:
#! /bin/bash
################################################################################
##
## log2url: Converts KDE Konqueror crash logs to a list of URLs
##
## Usage: log2url  [logfile]
##        'logfile' is an optional argument
##
## Written by CTWaley - Sep 2006.
##
## Freely given to the public domain by the author
##
################################################################################

readonly PROG="${0##*/}"
KDEHOME="${HOME}/.kde"  ## Just in case it's not already defined.
LOGDIR=/tmp/kde-${USER}

alias echo='echo -e'

if [ "${1}" ];then
     ## If a log file was specified, assign it. Also re-assign the log directory:
    LOGFILE="${1##*/}"  LOGDIR="${1%/*}"
    test -z "${LOGDIR}"  &&  LOGDIR="${PWD}"  ||  LOGDIR="${LOGDIR#file://}"
else  ## Else find the latest log file
    LOGS=($(cd ${LOGDIR}; ls konqueror-crash-??????.log))
    LOGCOUNT=${#LOGS[@]}

     ## Try to find log files which are newer than the one being tested:
    for ((IND=0; IND<LOGCOUNT; IND++));do
        FIND=$(cd ${LOGDIR}; find -type f -prune  -name 'konqueror-crash-??????.log' -newer "${LOGS[${IND}]}")
         ## There can only be one file which is the latest, which means we stop:
        test -z "${FIND}"  &&  LOGFILE="${LOGS[${IND}]}"  &&  break
    done
    test -z "${LOGFILE}"  &&
        { echo "${PROG}: Cannot find any crash logs to recover.\n"  &&  exit 1;}
    unset IND LOGS LOGCOUNT
fi

## Sanity check:
test -r "${LOGDIR}/${LOGFILE}"  ||
    { echo "${PROG}: Cannot read '${LOGFILE}'\n"  &&  exit 1;}
URLFILE="${LOGFILE%.*}.urls"

## Read log file and build URL file list:
CLOSE=$(sed -r 's/^close([(].+[)]):.+/\1/' <(grep '^close[(]' "${LOGDIR}/${LOGFILE}"))
OPENED=$(sed -r 's/[%]2B/+/g' <(grep '^opened' "${LOGDIR}/${LOGFILE}" | grep -v "$CLOSE") | tac)
HEXNUM=$(sed -r 's/^.+([(].+[)]):.*/\1/' <<<"${OPENED}")
## One long parsing command to data mine the URLs and build the list file:
while read;do
    nl <<<"${HEXNUM}" | sort -u -k2 | grep "${REPLY}"
done <<<"${HEXNUM}" | sort -u | cut -f2 | while read num;do
    grep "${num}" <<<"${OPENED}" | line
done | tac | cut -d: -f2- > "${KDEHOME}/${URLFILE##*/}"

## Reset the new URL list timestamp to the original crash log's timestamp:
touch -r ${LOGDIR}/${LOGFILE} ${KDEHOME}/${URLFILE##*/}

echo "-->\tKonqueror session URLS are saved in \"${KDEHOME}/${URLFILE##*/}\""

exit 0



konqrestore
Code:
#! /bin/bash
################################################################################
##
## konqrestore: Opens a new KDE Konqueror browser window and populates it from
##              a list of URLs, one line per URL, or a Konqueror crash log.
##              Requires it's companion script, log2url, for directly restoring
##              crash logs.
##
## Usage: konqrestore [url_list|logfile]
##        'url_list' or 'logfile' is an optional argument
##
## Written by CTWaley - Sep 2006.
##
## Freely given to the public domain by the author
##
################################################################################

readonly PROG="${0##*/}"
KDEHOME="${HOME}/.kde"  ## Just in case it's not already defined.
PROFILE='webbrowsing'   ## The Konqueror profile for restoring sessions

type -p log2url >/dev/null  ||
    { echo "${PROG}: Cannot find required script 'log2url'"  &&  exit 1;}

if [ "${1}" ];then
     ## If a file was specified, assign it. Also re-assign the file's directory:
    FILE="${1##*/}";  FILEDIR="${1%/*}"
    test -z "${FILEDIR}"  &&  FILEDIR="${PWD}"  ||  FILEDIR="${FILEDIR#file://}"

     ## Are we dealing with a crash log or URL list?:
    egrep -qs '^opened[(][0-9a-f]+[)]:.+:.+$' "${FILEDIR}/${FILE}"  &&
        {  ## If dealing with a crash log, convert to an URL list:
          log2url "${FILEDIR}/${FILE}"
           ## And point FILE and FILEDIR to the new file and location:
          FILE="${FILE%.*}.urls"; FILEDIR="${KDEHOME}";}

else  ## Else find the latest URL list file:
    FILEDIR="${KDEHOME}"
    FILES=($(cd ${FILEDIR}; ls konqueror-*.urls))
    FILECOUNT=${#LOGS[@]}

     ## Try to find files which are newer than the one being tested:
    for ((IND=0; IND<FILECOUNT; IND++));do
        FIND=$(cd ${FILEDIR}; find -type f -prune  -name 'konqueror-*.urls' -newer "${FILES[${IND}]}")
         ## There can only be one file which is the latest, which means we stop:
        test -z "${FIND}"  &&  FILE="${FILES[${IND}]}"  &&  break
    done
     ## Sanity check:
    test -z "${FILE}"  &&
        { echo -e "${PROG}: Cannot find any crash logs to recover.\n"  &&  exit 1;}
    unset IND FILES FILECOUNT
fi

## Sanity check:
test -r "${FILEDIR}/${FILE}"  ||
    { echo -e "${PROG}: Cannot read '${FILE}'\n"  &&  exit 1;}

## Input the list of URLs into an array:
URLS=($(while read;do  egrep '^[a-z]+:.+$' <<<"${REPLY}";  done \
            <<<"$(cat "${FILEDIR}/${FILE}")"))

## Any URLs found?:
test "${#URLS[@]}" == "0"  &&
    { echo "${PROG}: ${FILEDIR}/${FILE}: No URL found"  &&  exit 1;}
TABCOUNT=${#URLS[@]}

## Open a new browser window with the first URL in the list:
kfmclient openProfile "${PROFILE}" "${URLS[0]}"

## Now open up new tabs in the new browser window with the rest of the URLs:
for ((IND=1; IND<TABCOUNT; IND++));do
    sleep 1
    kfmclient newTab ${URLS[${IND}]}
done



Enjoy :-)
---thegeekster


Top
 Profile  
 PostPosted: Tue Sep 28, 2010 1:40 pm   

Joined: Tue Sep 28, 2010 1:18 pm
Posts: 2
Nice scripts, thanks!

The second one will be useful for a lot of cases, but I think I need to modify the first one--the script seems to assume that the last crash log written to is the one of interest, but:

* afaict, a separate crash log is created for each running instance of konqueror
* sometimes (or more often) I will want to process a crash file other than the last one written to--for instance, if I have a system wide crash (I get them) that kills all instances of konqueror, or if, after a crash of a single instance, I don't immediately take steps to recover that instance

(hmm, now I'm also wondering if the content of /kde/tmp-<username> is wiped out on a reboot?)

BTW, I still use konqueror 3.5 (under kde 3.5)


Top
 Profile  
 PostPosted: Thu Sep 30, 2010 11:47 am   

Joined: Tue Sep 28, 2010 1:18 pm
Posts: 2
I've somewhat answered my own questions--by running the following command, I can get the name of the crash log associated with a particular instance of konqueror:

dcop konqueror-<PID> KonquerorIface crashLogFile

where <PID> is the process ID of the desired instance of konqueror.

For example:

dcop konqueror-27656 KonquerorIface crashLogFile

At some point in time, I may modify log2url to make use of that--maybe prompt for the process ID of the desired instance of konqueror, then process the associated crash log.

Oops, wait, that won't work after that instance of konqueror has crashed, so I'll need an alternative--maybe something like:

  • run a cron job periodically (once an hour?) to maintain a file containing the "process" name for each open instance of konqueror and, on the same line, the name of the crash log file for that instance, possibly in a format something like this:

  • konqueror-27656 /tmp/kde-rhk/konqueror-crash-ivpmVa.log

  • then, after a crash, interrogate this file, manually or via some code

BTW: That dcop command is for konqueror 3.x--there is presumably a very similar dbus command that will do the same for konqueror 4.x and up.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP