Mittwoch, 26. Februar 2014

Synchronizing a bold Bash prompt in Debian Terminals

Put the following at the end of the .bashrc file to get a bold prompt:
if [ "$TERM" = xterm ] ; then
  PS1="\[\e]0;$PS1\a$(tput bold)\]\$? \! $PS1\\$\[$(tput sgr0)\] "
  PS1="\[$(tput bold)\]\$? \! $PS1\\$\[$(tput sgr0)\] "
The prompt prints the exit code of the last command, the history id if the current command, the user name, the host name and the working directory.

It is also possible to send the prompt to other systems accessed with SSH. For this it is necessary to export the PS1 environment variable:

export PS1
The SSH daemon of the target system must accept the variable. Put the following line in /etc/ssh/sshd_config:
AcceptEnv PS1
And add the following line to your local SSH configuration in .ssh/config:
SendEnv PS1
After that you have on your local and target system the same prompt without the need to synchronize any bashrc files.

Montag, 10. Februar 2014

Using BSD process accounting to list the memory usage

Sometimes the memory usage of a program is of interest. The following program can be used to list the memory usage reported by the BSD process accounting.
#include <stdio.h>
#include <string.h>
#include <sys/acct.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#define STR(I) #I

int main(int argc, char **argv)
  char debian_default[] = "/var/log/account/pacct";
  char usage[] = "Usage: acctmem accounting-file\n";
  char *file;

  if (argc == 2) {
    if (argv[1][0] == '-' && argv[1][1] == 'h') {
      fprintf (stderr, "%s", usage);
      return 0;
    } else {
      file = argv[1];
  else {
    file = debian_default;
  printf ("Using %s\n", file);

  int fd = open (file, O_RDONLY);
  if (fd < 0) {
    perror ("Can not open file");
    return 1;

  struct acct_v3 entry;
  for (;;) {
    ssize_t bytes = read (fd, &entry, sizeof entry);
    if (bytes < 0) {
      perror("Can not read file");
      return 1;
    if (bytes == sizeof entry)
      printf ("%6u: %-" STR(ACCT_COMM) "s(%3u): %uk\n",
  return 0;
// Local Variables:
// compile-command: "gcc -o acctmem acctmem.c"
// End: