Featured

    Featured Posts

Scheduling jobs using ' at ' and ' cron '

If you try to execute any command in linux/unix terminal, it is for one time execution and it will get executed once it is invoked.

Suppose if you have a command/script which should be executed at particular time/period OR at regular intervals of time... is it possible ?

Yes. it is possible!

We have a commands  "at" and "crontab" which were used for scheduling one time job and regular interval of time (recurring jobs)  respectively.

For ex: If you want to remove files in /tmp at evening 4pm today. You can automate this using "at" job . i.e., One time scheduler.

Suppose if the same task should be performed on everyday at 4pm , you can make use of "crontab" which is used to schedule the recurring jobs.


Scheduling jobs:

    at              ⇒  One time job scheduler ; Executes commands at specified time.
    crontab     ⇒  Recurring jobs ; it is a deamon to execute scheduled commands.


--------------------------------------------------------------------------------------------------------------------------------------------------------

at :

Creating jobs using “at” :
Syntax:    #at  time                                                       [here time is mandatory]
                 at >commands                                           
                 at >commands
                 at >ctrl+d                                                      [ctrl+d is for save]


Note: If you dont specify any time, then it will give grabled time error.
[root@myserver ~]# at
Garbled time
[root@myserver ~]# 


Few examples:
#at 8:31                                [it will schedule the job at 8:31am to remove all files under /tmp]
at> rm –rf   /tmp/*
at> ctrl+d 


Sample at job scheduling:
#at 7am
#at 11:30
#at noon
#at tea time                        → schedules job at evening 4pm or 16:00
#at now+10mits                  →  after 10mins from  current time  
#at 8pm + 3days
#at now + 2hours
#at 7pm monday          
#at 8am tomorrow
#at 8pm december 25


To see pending jobs:
# atq      (or)    # at -l

To remove jobs:
# at  -d  jobno                (OR)          #atrm jobno

ex: # atrm 4       → 4th job will be removed/deleted


To see job details:
# at -c jobno

Few important points to remember:
  • If you set the job for the which is already passed then it will be scheduling at next day Ex: current time is 10:00am if we scheduled at 7:00am then it will run on tomorrow             7:00am
  • Editing (or)modifying at job is not possible after we set it. We need to delete job and recreate it.
  • If the system is down but job exist at the time then once the syatem is up, immediately the job will run
  • We can see our own job using  # atq
  • Other user jobs cannot be seen. only the root user can see all jobs of users.
  • After executing “at” job it will be deleted.
  • Non redirected output will be redirected to mail
eg: in place of commands if you give
               echo "helloooo"
then the output ont be displayed on terminal rather the outpout will be sent to user mail.
if you want to display the output on terminal, then use terminal name
eg:     echo "helloooo" > /dev/tty2                             [to verify tty name : use tty command]

To allow and Deny the user to create crontab:

/etc/at.deny    :  To deny  the user to create “at” job, we need to make an entry of username  into /etc/at.denny

Vi /etc/at.deny
kiran
:wq!

Now system wont allow kiran to create at jobs, it will show permission denied.

# /etc/at.allow    : Block/restrict all current and future users otherthan one user.

Note: by default /etc/at.allow will be empty.
Be careful, if you update /etc/at.allow with any name, remaining all users will be denied to use at jobs.
--------------------------------------------------------------------------------------------------------------------------------------------------------
crontab:

Recurring Jobs: 

crontab:  It is used to schedule the  jobs on daily basis.
                It stands for cron tables
                It consists fo 6 fields. Out of which 5 are time and date related and last filed (6th                     filed) is either command/script to be executed.
 

below is the commad to know crontab fileds:
# cat /etc/crontab 


Here
Minutes              -     0 to 59
Hours                 -    0 to 23
Day of Month     -    1-31
Month                 -     jan-oct or  1-12
Day of Week      -    0-sun ; 1-mon; 6-sat

Commands:

crontab -e
To edit the crontab if exist or create a new one
crontab -l
To display existing crontab.
crontab -r
To remove existing crontab.
crontab  -e -u  user
To set/edit the crontab for another user. (Only root can do this)


Note:
  • An " * " in a fiels represtent all possible values.
  • Multiple values may be seperated by " , " (comma)
  • Ranges can be seperated by " - "
  • Seconds is not possible. Default is atleast a minute. ( you can write script to schedule for a second)
  • If we specify */5 in a minutes fields , it means every 5 minutes
  • 0-10/2 in a minutes fields means, every 2 minutes in  1 to 10 minutes 
  • Root can set crontab for any users , normal users can set crontab for only himself
Examples:

1. Every month; Every monday & Friday ; at 10:40am ; execute a job.
     45               10               *                 *                  1,5                commands

2. January & March; Monday to Friday; 13:40 ; execute a job
     40                13               *                1,3               1-5               commands

3.Every new year starting time ; execute a job
     0                    0               1                 1                  *                   commands

4. Every Month starting time; execute a job
     0                    0               1                 *                  *                   commands
 5 5.
5. Every Day starting time; execute a job
     0                    0               *                  *                  *                  commands

 6. Weekly starting time ; execute a job
0                    0               *                       *                  1                   commands
 kjklfkdlsjlkdksjfklds
7.Every 2 hours ; execute a job
     0                    */2             *                   *                 *                   commands


Few more easy commands:

Fields
Usage
Entries
@reboot

Execute the command immediately after reboot.(once system is UP)
        @reboot          commands
@hourly

To run once an hour
0   *   *   *   *  commands
@daily
To run once a day
0   0   *   *   * commands

@weekly

To run once a week
0   0   *   *   0  commands
@monthly

To run once a month
0   0   1   *   *  commands
@yearly
To run once a year

0    0   1   1   *  commands

Usages of crontab:
To take regular backups, scheduled updates, copying/moving/deleting files, displaying notifications etc.,






--------------------------------------------------------------------------------------------------------------------------------------------------------

Thanks 😊

Linux Boot Process

Have you ever wondered what happens while LINUX boot? 

Then you are at the right place !!!


The Boot process is the primary thing to know by all Linux users / administrators. Ofcourse it is one of the important interview questions of all the time.

[it is important to know the boot sequence which helps us to analyze the boot logs, understand what exactly happening at boot time and troubleshooting various boot related issues]

The process behind the scene, when we turn on the system and until the login prompt appears. This entire process is nothing but " boot process / boot sequence".

There are 6 stages available in Linux boot process:

Boot Process stages



BIOS:
  • Basic Input/output system (BIOS) will run (POST - Power On Self Test) to check all hardware and its operating state.
  • BIOS gets loaded in memory and checks system, connected peripherals, boot device (cd-rom ,floppy, hard disk) path. 
  • BIOS will determines the boot loader program and loads into memory.
  • Once boot loader program is loaded, BIOS hand over boot control to it.


MBR:
  • MBR is located in first sector of bootable drive, which is of size 512 bytes.
  • This 512 bytes is comprise of 446 bytes for primary boot loader, 64 bytes for partition table and 2 bytes for MBR validation check.  [ 512 = 446 + 64 + 2 ]
  • MBR contains the information about GRUB/LILO
  • LILO is Linux Loader which was used for old system.
  • Now boot control will move to GRUB stage.

GRUB:
  • Grand Unified Boot Loader.
  • The main config file for GRUB is /etc/grub.conf
  • It will display a splash image with the list of available kernels in your system  and wait for few seconds.
  • You can use Arrow keys and select the required kernel to load otherwise it will load the default kernel in a specified time present in /etc/grub.conf file.

sample grub.conf file :

  • It will have the details related to kernel and initrd image.
  • initrd - Initial Ram Disk is used to temporarily mount the root file system
  • The initramfs is used by the kernel to load drivers and modules necessary to boot the system. 
  • Once kernel and initramfs is loaded into memory, then the boot control will goes to kernel.

KERNEL:
  • Kernel will initialize and configure the system's memory and the hardware of the system ( processor/storage devices).
  • It will also helps to load all the necessary drivers.
  • kernel mounts the root partition as read-only, and free up any unused memory.
  • Kernel will execute /sbin/init. Since it is the first process which is generated by kernel,  init will have PID as 1 . Thus init is the parent or ancestor process of all process
  • Now the control will goes to init.
INIT:
  •  /etc/inittab file contains the run levels as follows:
sample inittab file:
  • Usually the default runlevel will be either 3 or 5, depends on your system.
  • Commands to check runlevel:
                 # who –r 
                 # runlevel

Runlevel:
  • The Runlevel directories will look's like  /etc/rc.d/rc*.d
  • Here * can be replaced by 0 to 6 . For ex: runlevel 0 will make use of /etc/rc.d/rc0.d
Runlevel  Directories:
Run level 0     →     /etc/rc.d/rc0.d
Run level 1     →     /etc/rc.d/rc1.d
Run level 2     →     /etc/rc.d/rc2.d
Run level 3     →     /etc/rc.d/rc3.d
Run level 4     →     /etc/rc.d/rc4.d
Run level 5     →     /etc/rc.d/rc5.d
Run level 6          /etc/rc.d/rc6.d
  • Depending on the selected runlevel, the init process will executes startup scripts under the subdirectories of  /etc/rc.d directory.
Thus Boot Process is completed.
If  everything goes fine, you should be able to see the Login prompt on your system.





-----------------------------------------------------------------------------------------------------------------------------------------------------
Thanks 😊



Linux - Filesystem Hierarchy - Directory Structure

Linux Directory Structure Overview:
Before we proceed with learning commands, it’s better to get a detailed idea about Linux's file structure.
The Linux file system structure looks like upside down tree.
This hierarchy is very easy to understand as all directory names are all self-explanatory.

Below picture shows how the directories were organised.





To list the Directories under " / "
Open Terminal
Change to / directory by typing “ cd / 
Type “ 
ls “ to view the contents ( Directories)


The Directory Structure in Unix & Linux are a unified Directory Structure where in all the directories are unified under the " " Root file system. Irrespective of where the File System is physically mounted all the directories are arranged hierarchically under the Root file system.

The Linux Directory Structure follows the "Filesystem Hierarchy Structure (FHS)" maintained by the Free Standards Group although most of the distributions sometimes tend to deviate from the standards.


Below is the detailed description of different directories under the Linux Filesystem Hierarchy :

/
The root directory.
Top level Directory, parent of all other directories.

/boot
Includes Linux boot related files / startup files, including the Linux kernel. Can be small; 16MB is usually adequate for a typical modular kernel. If you use multiple kernels, such as for testing a kernel upgrade, increase the size of this partition accordingly.

/root
Home directory of the root user.
Contains Root user related files

/home
Home directories for almost every user.
Contains normal user files
eg: /home/kiran

/bin
Contains Binary files.
Essential command line utilities.
Normal user commands.

/sbin
Super user binary files.
System administration/ Super user / root user commands.

/etc
Edit to configure.
Contains almost all  basic configuration files.

/usr
Unix Shareable resource.
Contains the packages which were installed with OS.
Includes many system administration commands & utilities.
  
/opt
Optional directory.
Contains packages which were installed manually.
eg: WordPerfect or Star Office.
  
/dev
Contains all device files.
eg: /dev/sda

/lib
Contain library files
Program libraries for the kernel and various command line utilities

/mnt
The mount point for removable media, including floppy drives,CDROMs.
eg: /mnt/partition1

/proc
Currently running kernel related processes, including device assignments such as IRQ ports, I/O addresses, and DMA channels.
Contains process related files.
eg: cat /proc/loadavg
       cat /proc/uptime

/tmp
Contains temporary files.
By default, Red Hat Linux deletes all files in this directory periodically.

/var
Variable data, including log files, mails and printer spools etc.,
eg: cat /var/log/messages
       cat /var/log/boot.log

/media
Contains removable media.

/srv
Services offered – server data



To list Directories under " / "



Graphical View of Directories under " / "







------------------------------------------------------------------------------------------------------------

www.CodeNirvana.in

COPYRIGHT © 2017. Powered by Blogger.

Featured Posts

Featured Posts

Featured Posts

Popular Posts

Translate

Total Page Views

blog counter
Copyright © gskLinux | Linux Tutorials | Designed By | Gnanasekhar