Ubuntu 14.10 rc.local / rc0.d debugging / test scripts

Debugging rc.d scripts can be a pain. These two scripts might help you to get a better idea on what is going on.

Note: replace $USER with your username.

Starting up

Save the following lines as /etc/init.d/custom-startup.sh

#!/bin/bash
LOGFILE="/var/log/custom-start.log"
TIMESTAMP=$(date +"%F %H:%M:%S")
rm $LOGFILE
exec 2>> $LOGFILE
echo $TIMESTAMP >> $LOGFILE
echo -e '\nnext command: ls /' >> $LOGFILE
ls / >> $LOGFILE
echo -e '\nnext command: ls /home' >> $LOGFILE
ls /home >> $LOGFILE
echo -e "\nnext command: ls /home/$USER/Desktop" >> $LOGFILE
ls /home/$USER/Desktop >> $LOGFILE

Set the file as executable:
sudo chmod +x /etc/init.d/custom-start.sh

Add this line to /etc/rc.local before “exit 0″
/etc/init.d/custom-startup.sh

Shutdown

Save the following lines as /etc/init.d/custom-shutdown.sh

#!/bin/bash
LOGFILE="/var/log/custom-exit.log"
TIMESTAMP=$(date +"%F %H:%M:%S")
rm $LOGFILE
exec 2>> $LOGFILE
echo $TIMESTAMP >> $LOGFILE
echo -e '\nnext command: ls /' >> $LOGFILE
ls / >> $LOGFILE
echo -e '\nnext command: ls /home' >> $LOGFILE
ls /home >> $LOGFILE
echo -e "\nnext command: ls /home/$USER/Desktop" >> $LOGFILE
ls /home/$USER/Desktop >> $LOGFILE

Set the file as executable.
sudo chmod +x /etc/init.d/custom-shutdown.sh

Add a symlink to call the script:
sudo ln -s /etc/init.d/custom-shutdown.sh /etc/rc0.d/K04custom-shutdown.sh

After a restart of the system you can check the logfiles:
cat /var/log/custom-exit.log
cat /var/log/custom-start.log

Ubuntu 14.10 shutdown script with rc0.d (rc6.d, rc.d)

An important change happened between Ubuntu 14.04 and 14.10. The automatic halt script was changed from /etc/rc0.d/S90halt to /etc/rc0.d/K11halt. Therefor custom scripts with the prefix K99 won’t be executed anymore. Changing your custom script to e.g. K04 ensures, that the script is executed in time.

Warning for encrypted home directories (not the same as full disk encryption): It seems that an encrypted home directory is umounted before the scripts in rc0 are executed (even if the script starts with K010). So a script won’t be able to access files of the home directory anymore.

Example:

Generate a custom shutdown file. The result will be an empty goodbye.txt on your Desktop after the next reboot.
echo -e '#!/bin/sh\ntouch /home/'$USER'/Desktop/goodbye.txt' | sudo tee /etc/init.d/custom-shutdown.sh

Set the file as executable.
sudo chmod +x /etc/init.d/custom-shutdown.sh

Insert a symlink from in the rc0.d directory with the process order K04.
sudo ln -s /etc/init.d/custom-shutdown.sh /etc/rc0.d/K04custom-shutdown.sh

Optional: This will execute the script at a reboot as well
sudo ln -s /etc/init.d/custom-shutdown.sh /etc/rc6.d/K04custom-shutdown.sh

Your custom script will be executed at the shutdown of the system.
Note: Currently Ubuntu is in the process of migrating from upstart to systemd.

References

Xubuntu 14.10 installation & customization

Note: This installation checklist is mainly chosen by personal preferences. It should give you an idea how an installation can be done in a fast and comfortable way.

  1. Choose the best server mirror for your installation files. The default mirror for your country might not be the fastest.
  2. Optional: correct standard directory locations.
    mkdir ~/Desktop (e.g. for the standard desktop location)
    nano ~/.config/user-dirs.dirs
    xdg-user-dirs-update
  3. Install (great) additional applications.
    sudo apt-get install audacity audacious baobab boinc-manager chromium-browser dia eog gedit gnome-disk-utility gnome-terminal gparted gsmartcontrol gufw imagemagick inkscape k3b k3b-i18n kde-l10n-de kio-ftps krename krusader libreoffice libreoffice-style-sifr mc nautilus pdftk synaptic ttf-mscorefonts-installer unetbootin virtualbox vlc vym xsane
  4. Connect and install your printer.
  5. Remove unnecessary applications.
    sudo apt-get remove abiword blueman gmusicbrowser gnome-mines gnome-sudoku gnumeric mousepad orage pidgin ristretto xfce4-mailwatch-plugin xul-ext-ubufox
  6. Remove unused packages.
    sudo apt-get clean && sudo apt-get autoremove
  7. Set preferred applications.

Additional software

  • 0 A.D. – a free, open-source game of ancient warfare
    sudo apt-get install 0ad
    Website of 0 A.D.
  • Google Chrome
    Although closed source, Google Chrome is a good fallback browser – e.g. for live streams based on flash. Visit the website of Chrome for installation details.

Many more ideas can be found on this page:
To Do List After installing Ubuntu MATE 14.10 Utopic OS

“Ubuntu Flavors 14.10 Officially Released” gives an overview of the latest Xubuntu changes.

OCR Tesseract – Text Recognition in Ubuntu 14.04

  1. Installation via command line
    sudo apt-get install tesseract-ocr
  2. Install a language file (e.g. -eng, -deu, -fra, -ita, -ndl, -por, -spa, …)
    sudo apt-get install tesseract-ocr-eng
  3. Run OCR on the scanned file
    tesseract scan.png scanned.txt -l eng

A great addition is the graphical frontend gImageReader for tesseract.

  1. Add the application repository
    sudo add-apt-repository ppa:sandromani/gimagereader
  2. Update the repository sources
    sudo apt-get update
  3. Install the application
    sudo apt-get install gimagereader

References

 

Accessing external LUKS HDD with custom user, Ubuntu 14.04

For some esoteric reason you might want to access your dm-crypt/LUKS encrypted harddisk with another user – e.g. perl, php, etc.

When plugging in external devices with automount standard permissions are used. Therefor a user like www-data will not be able to access the data of the drive. A solution is to permanently register the drive with crypttab and fstab and give the drive custom permissions.

  1. Check which user is running your target application. (e.g. user “www-data”)
    ps -aux
  2. Connect the drive manually and check the files /etc/crypttab and /etc/fstab.
    mount -l will help you to check the current fstab permissions.
  3. Disconnect the drive and recheck the permissions. Notice the missing line in /etc/crypttab.
  4. Add the missing line to /etc/crypttab – example given (replace LUKS ID and UUID):
    luks-504c9fa7-d080-4acf-a829-73227b48fb89 UUID=01234567-89ab-cdef-0123-456789abcdef none luks,discard
  5. Create the target mount directory.
    sudo mkdir /archive
  6. Add the line to /etc/fstab:
    /dev/mapper/luks-504c9fa7-d080-4acf-a829-73227b48fb89 /archive ext4 noauto,uid=mainsername,gid=www-data,umask=0027 0 0
    This example configuration will give the user “www-data” read access to the drive.
  7. Mount the drive:
    sudo mount /dev/mapper/luks-50* /archive
    sudo mount -a

To put it in a nutshell, fstab needs the /dev/mapper/luks-… before it can mount the drive.

References

Save energy by spinning down HDDs, Ubuntu 14.04

Especially when using a fast SSD as the main harddisk, it makes sense to spin down a secondary (larger) harddisk which is seldom used.

First of all you have to know which drive is mapped to the /dev directory. E.g. you can use the program gparted to show you the relevant /dev drive.

To spin down a hard disk into standby mode use this command in the terminal application (in this case the drive is “sdb”):

sudo hdparm -y /dev/sdb

To start your computer with the secondary drive in standby mode just add the described command to the file /etc/rc.local.

In that file you can also define an automatic spin down time –¬† e.g.

hdparm -S 120 /dev/sdb

The parameter 120 is nonlinear, so experiment with the number until it fits your needs.

 

Further Reference

hdparm – Ubuntu manuals

Install LibreOffice skin (sifr) in Ubuntu 14.04

The standard LibreOffice skin in Ubuntu 14.04 and 14.10 looks rather simple.

There is a fine alternative icon theme/skin which can be installed easily. Just execute this command in your terminal application.

  1. sudo apt-get install libreoffice-style-sifr
  2. Re-/Start LibreOffice.
  3. In LibreOffice visit the menu Tools > Options > LibreOffice > View and set “Icon size and style” to Automatic + Sifr

Graphviz + PHP + Ubuntu Linux 14.04 via exec

As the installation of the PHP Graphviz library on Ubuntu seems quite tricky. This shows a fast (& dirty) way of using Graphviz.

sudo apt-get install graphviz

Save and execute the following PHP code in a directory with full permissions (777).

<?php

$input = <<<DATA
digraph G {
main [label="main\n(start here)"];
main -> parse -> execute;
main -> init;
main -> cleanup;
execute -> make_string;
execute -> printf
init -> make_string;
main -> printf;
execute -> compare;
}
DATA;
file_put_contents('input.dot', $input);

exec('dot -Tpng input.dot -o graph.png');

echo ('<img src="graph.png" />');

?>

References:
Graphviz documentation