SOHO : Small Office Home Office
Freeware - Opensource software tips, tricks, tweaks & fixes for managing, securing, improving the performance of SOHO Desktop, Laptop, Networks

Tuesday, April 17, 2012

Add scripts to startup or shutdown

User-defined tasks can be done by executing a script at startup and shutdown for Linux. To execute script at startup of Ubuntu, the rc.local can be modified to include additional commands. To execute script when rebooting Ubuntu, the script has to be copied to the relevant directory. Once the permissions of the script have been changed to executable, it will run when rebooting the Linux operating system. Scripts are run in the alphabetical order in the directories and the scripts should be named following the nomenclature rules when executing a script at startup and shutdown.

To execute a script at startup of ubuntu
Edit /etc/rc.local and add your commands
The script must always end with exit 0

To execute a script upon rebooting Ubuntu
Put your script in /etc/rc0.d
Make it executable (sudo chmod +x myscript)
The scripts in this directory are executed in alphabetical order
The name of your script must begin with K99 to run at the right time. 

To execute a script at shutdown
Put your script in /etc/rc6.d
Make it executable (sudo chmod +x myscript)
The scripts in this directory are executed in alphabetical order
The name of your script must begin with K99 to run at the right time. 

How to put script in rc*.d?

  1. Create a script and make it executable
  2. Copy it to /etc/init.d folder
  3. Run the command ( dont forget the dot in the end)
sudo update-rc.d script_name start 3 0 6 . 

The above command will run the script during shutdown or reboot. update-rc.d adds this script to runlevels rc0.d and rc6.d 

rc0.d is shutdown
rc6.d is reboot

source :
Continue Reading...

Thursday, April 12, 2012

Unable to open env locale

In my ssh-log
/var/log/auth.log : I have the following error message

Unable to open env file: /etc/default/locale: No such file or directory

Edited on 19/December/2012

1. Create and edit file locale at  /etc/default
sudo nano /etc/default/locale
2.. Add this to the file (in this case we are adding US english)
LANG = en_US.utf8 
3. save and quit the editor

4. regenerate the locales
sudo locale-gen en_US.utf8
5. logout and login for the environment variables to be in effect

Solution for ubuntu
sudo update locale

if the error still persists then 
sudo apt-get install localepurge
sudo dpkg-reconfigure localepurge

check the out with command 

Solution for Deian
How to get rid of  locale/perl error messages?

perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
LANGUAGE = (unset),
LC_ALL = (unset),
LANG = "en_US.UTF-8"
are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
locale: Cannot set LC_CTYPE to default locale: No such file or directory
locale: Cannot set LC_MESSAGES to default locale: No such file or directory
locale: Cannot set LC_ALL to default locale: No such file or directory 
This is similar to the error explained above.

apt-get update
apt-get install locales
dpkg-reconfigure locales 

A window will popup, select en_US.UTF-8 as shown below:
Configuring locales choose en_us.UTF-8
Click on <OK>

Generating locales (this might take a while)...
en_US.UTF-8... done
Generation complete.
Reboot your system (changes to environment variables require system reboot)
Continue Reading...

Saturday, April 7, 2012

create terminal bash shortcuts

You can assign shortcuts for your most frequently used terminal commands by appending alias as many times as you did like to your bash configuration file (~/.bashrc). The syntax for alias is as follows.

alias yourCommand="YourTerminalCommand"

alias la="ls -la"
alias gs1="ssh"

Aliases shorten the amount of keystrokes necessary to throw commands.

I put my aliases in a file called ~/.aliases and then insert "source ~/.aliases" in my bash ~/.profile. That way it's easy to grab a copy of .aliases in case I'm doing command-line stuff on someone else's computer and don't want to mess with their .profile. 

I would advise that you only alias commands, that don't change your system or delete files. So don't alias "sudo" or "apt-get". When you're actually installing software, you want to do it deliberately, and you don't want the chance that a typo will actually do something that changes your system with elevated permissions.
Continue Reading...

Common Linux commands on system info

Print working directory, i.e., display the name of my current directory on the screen.

Print the name of the local host (the machine on which you are working). Use netconf (as root) to change the name of the machine.

Print my login name.

id username 
Print user id (uid) and his/her group id (gid), effective id (if different than the real id) and the supplementary groups.

Print or change the operating system date and time. E.g., I could change the date and time to 2000-12-31 23:57 using this command: 
date 123123572000 
To set the hardware (BIOS) clock from the system (Linux) clock, use the command (as root) setclock

Determine the amount of time that it takes for a process to complete + other info. Don't confuse it with the date command. E.g. I can find out how long it takes to display a directory content using: 
time ls

Determine the users logged on the machine.

rwho -a 
(=remote who) Determine all users logged on your network. The rwho service must be enabled for this command to run. If it isn't, run setup as root to enable "rwho".

finger user_name 
System info about a user. Try: finger root

Show listing of users last logged-in on your system.

history | more 
Show the last (1000 or so) commands executed from the command line on the current account. The "| more" causes the display to stop after each screenful.

Show the amount of time since the last reboot.

(=print status) List the processes currently run by the current user.

ps axu | more 
List all the processes currently running, even those without the controlling terminal, together with the name of the user that owns each process.

Keep listing the currently running processes, sorted by cpu usage (top users first). In KDE, you can get GUI-based Ktop from "K"menu under "System"-"Task Manager" (or by executing "ktop" in an X-terminal).

uname -a 
(= Unix name with option "all") Info on your (local) server. I can also use guname (in X-window terminal) to display the info more nicely.

Memory info (in kilobytes).

df -h 
(=disk free) Print disk info about all the filesystems (in human-readable form)

du / -bh | more 
(=disk usage) Print detailed disk usage for each subdirectory starting at the "/" (root) directory (in human legible form).

cat /proc/cpuinfo 
Cpu info--it show the content of the file cpuinfo. Note that the files in the /proc directory are not real files--they are hooks to look at information available to the kernel.

cat /proc/interrupts 
List the interrupts in use.

cat /proc/version 
Linux version and other info

cat /proc/filesystems 
Show the types of filesystems currently in use.

cat /etc/printcap 
Show the setup of printers.

(As root. Use /sbin/lsmod to execute this command when you are a non-root user.) Show the kernel modules currently loaded.

Show the current user environment.

echo $PATH 
Show the content of the environment variable "PATH". This command can be used to show other environment variables as well. Use "set" to see the full environment.

dmesg | less 
Print kernel messages (the content of the so-called kernel ring buffer). Press "q" to quit "less". Use less /var/log/dmesg  to see what "dmesg" dumped into this file right after the last system bootup. 

Continue Reading...

Thursday, April 5, 2012

Adding color to bash shell prompt

Mistakes do happen if not taken care off.

Other day I was handling two servers at the sametime. It so happend that the commands to be executed on  server-1 ended on server-2, this was a disaster. To avoid this I have set colored prompts to easily identify the servers. While I was doing this, I also modified the profiles to change colors for local user and root user. Now its easy to identify as to which server I am on and as a local or a root user. 

We are using ubuntu servers and to set the colored prompts I modified the .bashrc file. The .bashrc file is located in the users home directory. We have set green colored prompt for server-1 and blue colored prompt for server-2.

To set green color prompt for local user on server-1 append the following line to users .bashrc file.
export PS1="\e[0;32m[\u@\h \W]\$ \e[m"
To identify the root account we have hightlight the background with the same green color and red colored text. Append the following line to the root .bashrc 
export PS1="\e[0;31m\e[42m\u@\h \w> \e[m"

The image below will help you understand better.
click on image to enlarge

The code and its function

Change background color of the prompt
Change the background color by specifying \e[{code}m in the PS1 prompt as shown below.
$ export PS1="\e[47m\u@\h \w> \e[m"
[Note: This is for Light Gray background]
Combination of background and foreground
export PS1="\e[0;34m\e[47m\u@\h \w> \e[m"
[Note: This is for Light Blue foreground and Light Gray background]
Add the following to the .bash_profile or .bashrc to make the above background and foreground color permanent.
Play around by using the following background color and choose the one that suites your taste:
  • \e[40m
  • \e[41m
  • \e[42m
  • \e[43m
  • \e[44m
  • \e[45m
  • \e[46m
  • \e[47m

Foreground Color Code Table:

Black 0;30
Blue 0;34
Green 0;32
Cyan 0;36
Red 0;31
Purple 0;35
Brown 0;33
[Note: Replace 0 with 1 for dark color]
Continue Reading...

rtorrent queue manager python script

The complete story...

*** RTorrent as a torrent download manager ***

RTorrent is a very powerful console based torrent client.
You can find pretty advanced documentation on
I wanted to use rtorrent as my only torrent download manager, for that I needed to able to:

1) configure the total number of concurrent active torrent downloads;
2) add a queue of torrents to be automatically downloaded as soon as possible, provided the total number of active downloads before;
3) move the downloaded files to another directory and delete any related torrent;
4) limit the total amount of upload and download bandwidth not to choke my adsl connection.

while rtorrent provides built-in capabilities to accomplish 3) and 4), it doesn't provide a direct method to accomplish the first two points.
Fortunately rtorrent is able to watch a directory for torrents, meaning that automatically downloads any torrent added to that directory and automatically cancels any download related to a torrent that has been deleted from that directory.
I used this capability together with a little external script to solve 1) and 2).

First of all I created the following directory structure under /path (substitute /path with whatever you want):


loading contains the queued torrents;
session contains all the torrents rtorrent is downloading, this directory is managed by rtorrent and rtorrent only;
temp contains the uncompleted downloads, again this directory is managed my rtorrent;
completed contains all the completed downloads.

Then I configured rtorrent as follows:

--- .rtorrent.rc ---

directory = /path/temp
session = /path/session

schedule = watch_directory,10,10,load_start=/path/watch/*.torrent
schedule = untied_directory,5,5,"remove_untied="
schedule = low_diskspace,60,60,"close_low_diskspace=500M"

on_finished = rm_torrent,"execute=rm,$d.get_tied_to_file="
on_finished = move_complete,"execute=mv,-u,$d.get_base_path=,/path/completed/ ;d.set_directory=/path/

upload_rate = 15
download_rate = 50

port_range = 6881-6889

--- .rtorrent.rc ---

Save this as .rtorrent.rc in your home directory.
directory specifies the temporary downloads directory;
session specifies where to put the torrents related to the active downloads;
upload_rate is for limiting the upload bandwidth;
download_rate is for limiting the download bandwidth;
I also specified the port_range for compatibility with the other torrent clients.
The other options are more difficult to understand:
the first two schedule commands are to configure a watch directory as described above, the third is to stop any download when the disk is full.
The two on_finished commands are to move all the finished downloads to the completed directory and to delete their torrents from the watch directory too.

These configurations alone accomplish 3) and 4), but we are not finished yet with 1) and 2):
we need a script to move torrents from the loading directory to the watch directory, so that the total number of concurrent downloads is lower or equal to the specified value. As a consequence there must be no more than that number of torrents in the watch directory.
When a torrent download is completed, it is removed from the session and the watch directories; the script can recognise this event and move another torrent to the watch directory and so on.
I wrote this script in Python:

--- ---

#!/usr/bin/env python

# Now you can dynamically change the maximum number of simultaneous
# downloads writing the new number to the file "max_downloads_file" and
# then sending a signal SIGHUP to the rtorrentqueuemanager process.

import glob
import stat
import os
import shutil
import time
import signal

watch = "/share/storage/rtorrent/watch"
session = "/share/storage/rtorrent/session"
queue = "/share/storage/rtorrent/loading"
max_downloads_file = "/share/storage/rtorrent/max_downloads"
max_downloads = 2

def handler_sighup(signum, frame):
    f = open(max_downloads_file, "r")
    max_downloads = int(f.readline())

#### START
signal.signal(signal.SIGHUP, handler_sighup)
while True:
    bz2files = glob.glob(queue + "/*.torrent.bz2")
    for i in bz2files :
        os.system('bzip2 -d ' + i)
    gzipfiles = glob.glob(queue + "/*.torrent.gz")
    for i in gzipfiles :
        os.system('gunzip ' + i)
    sfiles = glob.glob(session + "/*.torrent")
    oldesttime = 0
    oldestfile = ""
    if len(sfiles) < max_downloads :
        qfiles = glob.glob(queue + "/*.torrent")
        for i in qfiles :
            ftime = os.stat(i)[stat.ST_MTIME]
            if oldesttime == 0 or ftime < oldesttime :
                oldesttime = ftime
                oldestfile = i
        if oldestfile != "" :
            shutil.move(oldestfile, watch)

--- ---

save it as
Now you can start rtorrent with the screen utility:

screen rtorrent

detach the screen pressing Ctrl-A-D.
Then start the script:

python &

From now on you can copy any torrent you want to download to the loading directory and rtorrent plus this script will take care of the rest!

Thanks to Jari Sundell for this great peace of software and to Shaun Dennie for giving me the idea of the queue manager script.

Stefano Stabellini

Credit & Source :
Continue Reading...

Tuesday, April 3, 2012

perl: warning: Setting locale failed

I get this error while updating and installing software : perl: warning: Setting locale failed

perl: warning: Falling back to the standard locale ("C").
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
    LANGUAGE = (unset),
    LC_ALL = (unset),
    LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
As root enter the following command in terminal and reboot your server.
echo "en_US.UTF-8 UTF-8" > /var/lib/locales/supported.d/local dpkg-reconfigure locales 
Continue Reading...

Monday, April 2, 2012

Logwatch output as file with date

update: This post has been edited with complete instructions from installaion  and configuration to store logwatch report as a file to a location of your choice.


Logwatch is a tool that will monitor your server's logs and email the administrator a digest on a daily basis.


Update your package list with:
$ sudo apt-get update
Install Logwatch with:
$ sudo apt-get install logwatch
Create a directory the Logwatch package in the repositories currently does not create, but is required for proper operation:
$ sudo mkdir /var/cache/logwatch
Configuration shouldn't be edited in the install directory (/usr/share/logwatch). Copy logwatch.conf to /etc/logwatch before editing:
$ sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/
Edit logwatch.conf to put in the e-mail where you want the report sent:
MailTo =
If you want logwatch to create a file in html format then edit logwatch.conf and make changes as below
Output  = file
Format = Html 
Detail level of the logwatch report can be Low, Med or High. Default level is Low. To change that, edit the newly copied logwatch.conf:
Detail = High
For Ubuntu systems with apache server:
sudo cp /usr/share/logwatch/default.conf/logfiles/http.conf /etc/logwatch/conf/logfiles/
Then add *combined.log files to the list

We want logwatch output as a file and to be stored on a local / remote server. In our case we will store it in the html folder to be accessed by webserver (You must secure you folder with .htaccess)
To accomplish this, first we need to write a script
$ mkdir ~/scripts
$ nano ~/scripts/logwatch_daily

Insert the below lines in the script file and save it

test -x /usr/share/logwatch/scripts/ || exit 0
/usr/sbin/logwatch --output file --filename /var/cache/logwatch/$(date +%Y%m%d)-logwatch.html
chmod 644 /var/cache/logwatch/*.html
mv /var/cache/logwatch/*.html /var/www/private/logwatch/

With the above script the logwatch report will be saved in "your-domain/private/logwatch/ " Date will also be appended to the file name. You can modifiy the script to ssh transfer the file to a remote server too. Need help with this, ask your questions in the comments section.

Run the script:

Logwatch adds its script 00logwatch to /etc/cron.daily. We need to modify this for our purpose. Edit the script 00logwatch
$ sudo nano 00logwatch
Delete all the lines from the script and add the below line

exit 0


Every time logwatch generates a report, it is stored in the path specified by the script. You can also get the reports emailed directly to your mailbox. But with this method we can access the logs from the server and look for any report. Also this is a best solution for centralized log collection and monitoring.

Below is the old post & has info on logwatch --save options
How to:
I am using a central log server which has been stripped from all mail sending capabilities. Even local ones. I would like to know how to set up the logwatch so that I would get similar output in a html report as I have when logwatch is sending out daily report via mail.

Is it possible to add a date to the --save option, so that logwatch output would be automatically stored with the information when it was created?

 you can use the option --filename to append a date:
# logwatch --output file --filename /tmp/logwatch-$(date +%Y%m%d)

or with your version:
# logwatch --save /tmp/logwatch-$(date +%Y%m%d)

Continue Reading...