Wednesday, August 22, 2018

Process monitoring tools, at and crontab

Some of them are top,iostat,vmstat

We can view the native performance of various processes in OS using these tools.

To view a dynamic real-time view of the top running processes in OS, use the following command:top

Go through the man page of the above tools to get more details.


Many a times we need to schedule a task for a future time, say in the evening at 8 P.M. on a specific day. We can use the at command in such a situation.Sometimes we need to repeat the same task at a specific time, periodically, every day, or every month. In such situations, we can use the crontab command.

at time date

 Example:
 The Control + D command will save the at job. The task will be executed at
11.15 A.M. This command will log messages to the log.txt file at 11.15 A.M.:
$at 11.15 AM
at > echo "Hello World" > $HOME/log.txt
at > Control + D

The following command will send an e-mail on March 31, 2015 at 10 A.M.:
$ at 10am mar 31 2015
at> echo "taxes due" | mail jon
at> ^D

The following command will make the task run on May 20 at 11 A.M.:
$ at 11 am may 20

All the jobs which are scheduled by the at command can be listed using
the following command:
$ atq

To remove a specific job listed by the atq command, we can use the
following command:
$ atrm job-id

If we need to run a specific task repetitively, then the solution is to use crontab
 The syntax of the command is as follows:
$ crontab –e


 This will open a new editor.Finally, to save the jobs, use the following:wq

The following are a few examples of the crontab command:

Use the following command to run a script every 5 minutes, every day:
5 * * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

Use the following command to run 5 minutes after midnight every day:
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1

Use the following command to run at 2.15 P.M. on the first of every
month—the output is mailed to Paul:
15 14 1 * * * $HOME/bin/monthly

Use the following command to run at 10 P.M. on weekdays, send the
e-mail to ganesh@abc.com:
0 22 * * 1-5 sendmail ganesh@abc.com < ~/work/email.txt

The sendmail utility is used for sending e-mails. We can use the mail utility
also as follows:
sendmail user@example.com < /tmp/email.txt

The following commands are self-explanatory from text of echo:
23 0-23/2 * * * echo "run 23 minutes after midn, 2 am, 4 am,
everyday
5 4 * * sun echo "run at 5 after 4 every Sunday"

The following are a few more crontab command examples:

 Min  Hour  Day/Month  Month  Day/Week  Time of Execution
 45  0  5  1,6,12  *  00:45 hrs on the 5th
of January, June, and December.
 0  18  *  10  1-5  6.00 P.M. every weekday
(Monday-Friday) only in
October.
 0  0  1,10,15  *  *  Midnight on the 1st ,10th,
and 15th of the month
 5,10  0  10  *  1  At 12.05 and 12.10 every
Monday and on the 10th
of every month

We can add macros in the crontab file. Use the following to restart my_program after each reboot

@reboot /bin/my_program
@reboot echo `hostname` was rebooted at `date` | mail -s "Reboot notification"  our.admin@some-corp.com

The following is the summary of a few more macros:

 Entry  Description  Equavalent to
 @reboot  Run once at startup  None
 @weekly  Run once a week  0 0 * * 0
 @daily   Run once a day  0 0 * * *
 @midnight   (same as @daily)  0 0 * * *
 @hourly    Run once an hour  0 * * * *

No comments:

Post a Comment