Friday, October 31, 2014

Send An Email Alert When Your Disk Space Gets Low

The following script alerts you when your root partition is almost full:
  1. Update the Server : - apt-get update
  2. Install mailx client :- apt-get install bsd-mailx
  3. Create a File disk.sh
  4. Add the Content
#!/bin/bash
CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=9
if [ "$CURRENT" -gt "$THRESHOLD" ] ; then
    mail -s 'Disk Space Alert' ictops@gmail.com << EOF
Your root partition remaining free space is critically low. Used: $CURRENT%
EOF
fi
                                      The script sends an email when the disk usage rises above the percentage specified by the THRESHOLD variable (9% here).
Script automatically run when it added into the Crontab file
root@ubuntu:~# crontab -e
10 15 * * * /root/disk.sh (Execute the command at 3.10 pm everyday)

Wednesday, October 29, 2014

Set SSH Login Email Alerts In Linux Server

                        To enable server to automatically send a notification email to predefined email address every time someone logs in as root to the host. To configure the automatic email alert notification to a default email address on each incident of root logon on the server.
  1. Update the Server : - apt-get update
  2. Install Optional (apt-get install mailutils)
  3. Install mailx client :- apt-get install bsd-mailx
                  During the installation Its ask for selecting following features “Postfix Configuration and Package Configuration” (Leave its as Default)
  1. Login as root user and go to root’s home directory by typing cd /root command.
# cd /root
  1. Open .bashrc file with vi or nano editor. Please remember .bashrc is a hidden file, you won’t see it by doing ls -l command. You’ve to use -a flag to see hidden files in Linux.
        root@ubuntu:~# ls -a
.  ..  .bash_history  .bashrc  .cache  .profile  .viminfo
root@ubuntu:~# vi .bashrc
                                              Add the following whole line at the bottom of the file. Make sure to replace “ServerName” with a hostname of your Server and change “your@yourdomain.com” with a your email address.
Syntax:- echo 'ALERT - Root Shell Access (ServerName) on:' `date` `who` | mail -s
"Alert: Root Access from `who | cut -d'(' -f2 | cut -d')' -f1`" your@yourdomain.com
Eg:- echo 'ALERT - SSH Access Detected To (ubuntu) on:' `date` `who` | mail -s "Alert: Root Access from `who | cut -d"(" -f2 | cut -d")" -f1`" ictops@gmail.com
  1. Execute the following for instant result.
root@ubuntu:~# source .bashrc

Thursday, October 23, 2014

Schedule Tasks In Linux With CRON

Schedule Tasks In Linux With CRON
                                      There is an utility in Linux known as CRON with which you can start your jobs automatically at a desired time and schedule them to get executed periodically.
                                     Cron utility consists of two parts: The cron daemon and the cron configuration files. Cron daemon is just like any service that is started automatically whenever your system boots. Cron configuration files hold the information of what to do and when to do
  1. Edit a Crontab Entry :- crontab -e
  2. Select Favorite editor.If You select any other option, use the following command to open in VI editor
            export VISUAL=vi
     3. Create a new crontab file from another user
            crontab -l > name  (Created in Present Working Directory)
     4. Listing the Cron Jobs :- crontab –l
     5. To enlist the cron jobs for a particular user :- crontab –u [USER] –l
Example :- crontab –u root –l
     6. For deleting Crontab entry : - crontab -r
     7. For activating crontab entry : - crontab crontab name
Crontab syntax :-
                                           A crontab file has six fields for specifying minute, hour, day of month, month, day of week and the command to be run at that interval. See below:
  • 1: Minute (0-59)
  • 2: Hours (0-23)
  • 3: Day (0-31)
  • 4: Month (0-12 [12 == December])
  • 5: Day of the week(0-7 [7 or 0 == sunday])
  • /path/to/command - Script or command name to schedule
Easy to remember format:
*     *     *     *     *  command to be executed
-     -     -     -     -
|     |     |     |     |
|     |     |     |     +----- day of week (0 - 6) (Sunday=0)
|     |     |     +------- month (1 - 12)
|     |     +--------- day of month (1 - 31)
|     +----------- hour (0 - 23)
+------------- min (0 - 59)

Sample :-
  1. Created a bash script for making directory
root@IPAM:/home/ictops#vi job.sh
mkdir -p /home/ictops/test1
mkdir -p /home/ictops/test1
      2. Provide Executable permission :- chmod +x /home/ictops/job.sh
      3. Add this batch file “job.sh” into crontab entry for sheduling task
        root@IPAM:/home/ictops#crontab -l > mycron   
            no crontab for root
        root@IPAM:/home/ictops#crontab -e
no crontab for root - using an empty one
root@IPAM:/home/ictops#crontab -l > mycron   
root@IPAM:/home/ictops#crontab  mycron

php.exe is not recognized as an internal or external command, operable program or batch file

                After installing wamp, i try to check the php version in user running command prompt
C:\Users\jagannathanv> php -v
“php.exe’ is not recognized as an internal or external command, operable program or batch file”
                                 But the same its working when i change the path in command prompt to “C:\wamp\bin\php\php5.4.3\”
In CMD:-
cd C:\wamp\bin\php\php5.4.3\
C:\wamp\bin\php\php5.4.3\>php -v
PHP 5.4.7 (cli) (built: Sep 12 2012 23:48:31)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
Cause :- PHP is not included in your PATH.that is in Environment Variables
Solution :-
  1. Take System Properties (Right click your My Computer, then Properties)
  2. Advanced System Settings --> Environment Variables ---> system variables
  3. Edit the PATH variable, add your PHP installation directory (C:\wamp\bin\php\php5.4.3)
  4. Restart the PC

Monday, October 20, 2014

Reset A Lost Administrative Password In Ubuntu

                        By default the first user's account is an administrative account, If the user doesn't remember their password you need to reset it.For this
1.  Boot into recovery mode:-
                 Boot up the machine, and after the BIOS screen, hold down the left Shift key. You will then be prompted by a menu that looks something like this
2 . Get the Window like below
3. You should now see a root prompt, something like this:
root@ubuntu:~#
At this stage you should have a read-only file system. You have to remount it with write permissions:
root@ubuntu:~# mount -rw -o remount /
Now we can set the user's password with the passwd command.If you find the user names goto /home.
In this example I will use jagan user's username
root@ubuntu:~# passwd jagan
                                     Enter new UNIX password:
                                     Retype new UNIX password:
                                     passwd: password updated successfully
                                     root@ubuntu:~#
bloggerwidgets