timeout command in python

I would like to add a timeout to any shell command such that if it does not complete within a specified number of seconds the command will exit. This would be useful for a any long-running command where I’d like it to die on its own rather than manually killing the long-running process. There are […]

sqlplus, utility scripts

I would like to use sqlplus in development projects, and I would like it to be easy to use. First off, I want command-history and tab-completion. The easiest approach is to use rlwrap which uses the GNU readline library as a wrapper to command-line programs such as sqlplus. If you install rlwrap, you can then […]

screen and screenrc

I would like to use GNU screen as a window manager. By default screen seems like little more than a persistent shell (that you can resume even after logging out). By itself, this is incredibly useful if you wish to access the exact same terminal session from different locations. For example, on the command-line you […]

sqlplus pagesize and linesize

I would like sqlplus output to be more readable. Fortunately, you can adjust the pagesize and linesize variables on the fly, e.g., foo_owner@FOO> set pagesize 50000 foo_owner@FOO> set linesize 120 You can set pagesize to 0, which is very useful for scripted output, as it will not print any column headers and only print the […]

sqlplus command prompt (sqlprompt)

I would like to change the sqlplus command-prompt to something more useful than SQL> You can modify the sqlprompt variable as follows, SQL> SQL> set sqlprompt “_USER’@’_CONNECT_IDENTIFIER> ” FOO_OWNER@FOO> You can also use variables such as _PRIVILEGE and _DATE, although all I really want is the user and schema. Unfortunately, with the above technique, if […]

reverse ssh tunnel

I would like ssh access to a protected host that is not directly accessible on the Internet but does have outbound access. This is a common scenario in corporate networks that often require a vpn for remote access; but in situations where vpn access is not available (e.g., I forgot my keyfob, or I don’t […]

ssh agent across multiple hosts

I would like secure single-sign-in across multiple hosts. An easy way to do this is with ssh-agent, however, ssh-agent is a bit limited. For example, the normal use of ssh-agent looks like this, $ ssh-agent SSH_AUTH_SOCK=/tmp/ssh-stSwW11394/agent.11394; export SSH_AUTH_SOCK; SSH_AGENT_PID=11395; export SSH_AGENT_PID; echo Agent pid 11395; You would need to set those environment variables to use […]

python, unique files by content

I would like to retrieve a list of unique files by content rather than by filename. That is, if spam.txt and eggs.txt both contained the same contents I want only one of them to return. A very simple approach is to compute a SHA-1 checksum on each file, and build a dictionary with the checksum […]

mysqldump, tips and tricks

I want to backup a mysql database. The easiest approach is using mysqldump with its default options, i.e., # mysqldump -u user -h host -ppass db > backup.sql This will dump the full DDL and DML needed to re-create the database. Table locking is enabled by default, although for InnoDB it is recommended to use […]

tail -F

I would like to follow an error log as errors are written. The GNU tail command support a –follow option that outputs data as a file grows. There’s also a –retry option that will keep trying to access a file even if it becomes inaccessible (e.g., during log rotation). The -F option is equivalent to […]

nohup &

I would like to run a script in the background and to keep running even after I log out. To run a command or script in the background use an ampersand, e.g., # ./long-running-script.sh & This is equivalent to suspending the process with Ctrl-Z, and then issuing bg, # ./long-running-script.sh ^Z [1]+ Stopped ./long-running-script.sh # […]

smiley shell prompt, or frown

I’ve been using the following command prompt for years server ~/dir :) whoami victor server ~/dir :) badcommand -bash: badcommand: command not found server ~/dir :( echo sorry sorry server ~/dir :) It’s a smiley emoticon, but only when the previous command returned without error. If the exit code (stored in $?) returns any kind […]

recursive grep

I am trying to search through all files in a directory tree, perhaps to find a specific function declaration. Ideally I want the exact files and line numbers of every search match. A modern GNU grep contains a “-r” recursive options, so for example to find the function declaration (with line number) for “authenticate” you […]