Monday, January 12, 2015

Keep running process : screen

Screen is a utility tool to keep running your process despite of dropped connection. 
There are following commands which are useful . These commands begin with 'CTRL+a' to distinguish them from normal shell commands.
Ctrl a c - Creates a new screen session so that you can use more than one screen session at once.
Ctrl a n - Switches to the next screen session (if you use more than one).
Ctrl a p - Switches to the previous screen session (if you use more than one).
Ctrl a d - Detaches a screen session (without killing the processes in it - they continue).
To close a screen session where all tasks are finished you can type exit to close.

To check the current screens on server with status. use screen -ls
[root@MYSYS001 ~]# screen -ls
There is a screen on:
        16495.pts-6.MYSYS001    (Detached)
1 Socket in /var/run/screen/S-root.

To connect a detached screen use command screen -r
[root@MYSYS001 ~]# screen -r 16495.pts-6.MYSYS001

When you detach your session using 'Ctrl+a' + d, below message will appear
[detached]

If a screen is attached following message will appear
[root@MYSYS001 ~]# screen -ls
There is a screen on:
        16495.pts-6.MYSYS001    (Attached)
1 Socket in /var/run/screen/S-root.

Trying to attach a already attached screen, you will found following message
[root@MYSYS001 ~]# screen -r 16495.pts-6.MYSYS001
There is a screen on:
        16495.pts-6.MYSYS001    (Attached)
There is no screen to be resumed matching 16495.pts-6.MYSYS001.

Thursday, January 8, 2015

Job Scheduling : crontab

A crontab file contains instructions to the cron daemon of the general form: "runthis command at this time on this date". Each user has their own crontab, and commands in any given crontab will be executed as the user who owns the crontab.
Blank lines and leading spaces and tabs are ignored.  Lines whose first non-spacecharacter is a pound-sign (#) are comments, and are ignored. Note that comments are not allowed on the same line as cron commands, since they will be taken to bepart of the command.  Similarly, comments are not allowed on the same line as environment variable settings.

How to view existing crontab: crontab -l
[root@localhost~]# crontab -l
1 * * * * /usr/sbin/logrotate -s /home/ajay/logdumps/script/logrotate.state /home/ajay/logdumps/script/logrotate.conf
0 5 * * * >/tmp/App.log

How to edit or create crontab : crontab -e
It will open a editable screen and save as :wq (vim editor command). It will automatically install new/modified one.
[root@localhost~]# crontab -e
crontab:installing new crontab

These special time specification "nicknames" are supported, which replace the 5 initial time and date fields, and are prefixed by the '@ character:
       @reboot    :    Run once after reboot.
       @yearly    :    Run once a year, ie.  "0 0 1 1 *".
       @annually  :   Run once a year, ie.  "0 0 1 1  *".
       @monthly    :    Run once a month, ie. "0 0 1 * *".
       @weekly    :    Run once a week, ie.  "0 0 * * 0".
       @daily     :    Run once a day, ie.   "0 0 * * *".
       @hourly    :    Run once an hour, ie. "0 * * * *".

Crontab format :
minutes(0-59)  hours(0-23) date(1-31)  month(1-12)  weekdays(0-6) command
* denotethat all possible values.

Each filed can be multiple with comma separated. e.g.
I want to clear logfile twice a day so my crontab will be 

[root@localhost~]# crontab -l
0 8,20 * * * >/tmp/App.log

Explanation:
0    : 0th minute (top of hour)
8,20 : 8'o clock in morning and 8'o clock in evening
*    : everyday
*    : every month
*    : each day of week

lets if we want to do this exercise during weekdays only
[root@localhost~]# crontab -l
0 8,20 * * 1-5 >/tmp/App.log

so i have modified my weekdays value to 1-5 (Monday-Friday).