cis 191 - lesson 12 system monitoring 1. cis 191 - lesson 12 ps command 2

55
CIS 191 - Lesson 12 System Monitoring 1

Upload: zachery-barnwell

Post on 28-Mar-2015

230 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

SystemMonitoring

1

Page 2: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

pscommand

2

Page 3: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

Information Description

PID Process Identification Number, a unique number identifying the process

PPID Parent PID, the PID of the parent process (like … in the file hierarchy)

UID The user running the process

TTY The terminal that the process's stdin and stdout are connected to

S The status of the process: S=Sleeping, R=Running, T=Stopped, Z=Zombie

PRI Process priority

SZ Process size

CMD The name of the process (the command being run)

C The CPU utilization of the process

WCHAN Waiting channel (name of kernel function in which the process is sleeping)

F Flags (1=forked but didn't exit, 4=used superuser privileges)

TIME Cumulative CPU time

NI Nice value

Just a few of types of information kept on a process.

Use man ps to see a lot more.

3

Page 4: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

Show just my processes

Show all processes with a tty

4

Page 5: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

-a show processes with a terminal

x option shows full commands being run and states (most are asleep).

I'm using two Putty sessions, in one session I have the man page open for ps, the other I'm issuing ps commands 5

Page 6: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

Use the u option to look at processes owned by a specific user

6

Page 7: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

Use the u option to look at processes owned by a specific user

7

Page 8: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

Use –l for additional options

8

Page 9: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

9

Page 10: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

root 372 11 0 Sep10 ? 00:00:00 [ata/1]root 373 11 0 Sep10 ? 00:00:00 [ata_aux]root 377 11 0 Sep10 ? 00:00:00 [scsi_eh_0]root 378 11 0 Sep10 ? 00:00:00 [scsi_eh_1]root 379 11 0 Sep10 ? 00:01:25 [kjournald]root 412 11 0 Sep10 ? 00:00:00 [kauditd]root 446 1 0 Sep10 ? 00:00:00 /sbin/udevd -droot 869 11 0 Sep10 ? 00:00:01 [kedac]root 1420 11 0 Sep10 ? 00:00:00 [kmpathd/0]root 1421 11 0 Sep10 ? 00:00:00 [kmpathd/1]root 2082 1 0 Sep10 ? 00:00:05 /usr/sbin/restorecondroot 2098 1 0 Sep10 ? 00:00:11 auditdroot 2100 2098 0 Sep10 ? 00:00:05 /sbin/audispdroot 2120 1 0 Sep10 ? 00:00:23 syslogd -m 0root 2123 1 0 Sep10 ? 00:00:00 klogd -xroot 2160 1 0 Sep10 ? 00:00:20 mcstransdrpc 2183 1 0 Sep10 ? 00:00:00 portmaproot 2201 1 0 Sep10 ? 00:01:18 /usr/bin/python -E /usr/sbin/setroubrpcuser 2227 1 0 Sep10 ? 00:00:00 rpc.statdroot 2275 1 0 Sep10 ? 00:00:00 rpc.idmapdroot 2345 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-bridge -d /var/run/vmroot 2364 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-natd -d /var/run/vmnedbus 2383 1 0 Sep10 ? 00:00:15 dbus-daemon --systemroot 2434 1 0 Sep10 ? 00:00:51 pcscdroot 2472 1 0 Sep10 ? 00:00:00 /usr/bin/hidd --serverroot 2493 1 0 Sep10 ? 00:00:02 automount

10

Page 11: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Informationroot 2534 1 0 Sep10 ? 00:00:00 ./hpiodroot 2539 1 0 Sep10 ? 00:00:00 python ./hpssd.pyroot 2556 1 0 Sep10 ? 00:00:00 cupsdroot 2575 1 0 Sep10 ? 00:00:11 /usr/sbin/sshdroot 2600 1 0 Sep10 ? 00:00:01 sendmail: accepting connectionssmmsp 2609 1 0 Sep10 ? 00:00:00 sendmail: Queue runner@01:00:00 forroot 2626 1 0 Sep10 ? 00:00:00 crondxfs 2662 1 0 Sep10 ? 00:00:00 xfs -droppriv -daemonroot 2693 1 0 Sep10 ? 00:00:00 /usr/sbin/atdroot 2710 1 0 Sep10 ? 00:00:00 rhnsd --interval 240root 2743 1 0 Sep10 ? 00:01:33 /usr/bin/python -tt /usr/sbin/yum-uproot 2745 1 0 Sep10 ? 00:00:00 /usr/libexec/gam_serverroot 2749 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-netifup -d /var/run/vroot 2758 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-netifup -d /var/run/vroot 2768 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-netifup -d /var/run/vroot 2827 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-dhcpd -cf /etc/vmwareroot 2858 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-dhcpd -cf /etc/vmwareroot 2859 1 0 Sep10 ? 00:00:00 /usr/bin/vmnet-dhcpd -cf /etc/vmware68 2875 1 0 Sep10 ? 00:00:01 haldroot 2876 2875 0 Sep10 ? 00:00:00 hald-runner68 2883 2876 0 Sep10 ? 00:00:00 hald-addon-acpi: listening on acpid68 2886 2876 0 Sep10 ? 00:00:00 hald-addon-keyboard: listening on /d68 2890 2876 0 Sep10 ? 00:00:00 hald-addon-keyboard: listening on /droot 2898 2876 0 Sep10 ? 00:02:46 hald-addon-storage: polling /dev/hdaroot 2944 1 0 Sep10 ? 00:00:00 /usr/sbin/smartd -q neverroot 2949 1 0 Sep10 tty2 00:00:00 /sbin/mingetty tty2

11

Page 12: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Process Information

root 2950 1 0 Sep10 tty3 00:00:00 /sbin/mingetty tty3root 5365 2575 0 08:19 ? 00:00:00 sshd: rsimms [priv]rsimms 5368 5365 0 08:19 ? 00:00:00 sshd: rsimms@pts/0rsimms 5369 5368 0 08:19 pts/0 00:00:00 -bashroot 5969 2575 0 10:14 ? 00:00:00 sshd: valdemar [priv]valdemar 5971 5969 0 10:14 ? 00:00:00 sshd: valdemar@pts/5valdemar 5972 5971 0 10:14 pts/5 00:00:00 -bashrsimms 6173 5369 0 10:36 pts/0 00:00:00 man psrsimms 6176 6173 0 10:36 pts/0 00:00:00 sh -c (cd /usr/share/man && (echo ".rsimms 6177 6176 0 10:36 pts/0 00:00:00 sh -c (cd /usr/share/man && (echo ".rsimms 6182 6177 0 10:36 pts/0 00:00:00 /usr/bin/less -isroot 6200 2575 0 10:37 ? 00:00:00 sshd: rsimms [priv]rsimms 6203 6200 0 10:37 ? 00:00:00 sshd: rsimms@pts/6rsimms 6204 6203 0 10:37 pts/6 00:00:00 -bashroot 6408 2575 0 11:07 ? 00:00:00 sshd: dymesdia [priv]dymesdia 6418 6408 0 11:08 ? 00:00:00 sshd: dymesdia@pts/1dymesdia 6419 6418 0 11:08 pts/1 00:00:00 -bashrsimms 6524 6204 0 11:15 pts/6 00:00:00 ps -eflyonsrob 12891 1 0 Oct01 ? 00:00:00 SCREENlyonsrob 12892 12891 0 Oct01 pts/3 00:00:00 /bin/bashroot 29218 1 0 Oct15 tty1 00:00:00 /sbin/mingetty tty1[rsimms@opus ~]$

12

Page 13: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Signals

Page 14: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Signals are asynchronous messages sent to processes

They can result in one of three courses of action:1. be ignored, 2. default action (die) 3. execute some predefined function.

Signals are sent:• Using the kill command: $ kill -# PID

• Where # is the signal number and PID is the process id.• if no number is specified, SIGTERM is sent.

• Using special keystrokes • limited to just a few signals

Use kill –l to see all signals

Signals

Page 15: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

/home/cis90/roddyduk $ kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR213) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+439) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+843) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+1247) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-1451) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-1055) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-659) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-263) SIGRTMAX-1 64) SIGRTMAX/home/cis90/roddyduk $

SignalsUse kill –l to see all of them

Page 16: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

SignalsSpecial keystrokes

/home/cis90/roddyduk $ stty -aspeed 38400 baud; rows 26; columns 78; line = 0;intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>;eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^F; rprnt = ^R;werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;

[rsimms@opus ~]$ stty -aspeed 38400 baud; rows 39; columns 84; line = 0;intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>;swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W;lnext = ^V; flush = ^O; min = 1; time = 0;

use stty –a to see special keystrokes

Page 17: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringuptime

Uptime shows how long the system has been running

17

Page 18: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Use top to find CPU hogs and memory leaks

18

Page 19: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Use top to find CPU hogs and memory leaks 19

for the past 1, 5 and 15 minutes

PR = priority

NI = nice values

VIRT=Virtual memory used

RES=Physical memory used

SHR=Shared memory used

Page 20: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Use top to find CPU hogs and memory leaks 20

for the past 1, 5 and 15 minutes

PR = priority

NI = nice values

VIRT=Virtual memory used

RES=Physical memory used

SHR=Shared memory used

Page 21: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Running Jim's cpu.sh script (uses binary calculator) 21

binary calculator

Page 22: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Running Jim's disk.sh script (lots of find commands) 22

Page 23: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringtop

Running Jim's sleep script 23

Page 24: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoringvmstat

Procs r: The number of processes waiting for run time. b: The number of processes in uninterruptible sleep.

Memory swpd: the amount of virtual memory used. free: the amount of idle memory. buff: the amount of memory used as buffers. cache: the amount of memory used as cache. inact: the amount of inactive memory. (-a option) active: the amount of active memory. (-a option)

Swap si: Amount of memory swapped in from disk (/s). so: Amount of memory swapped to disk (/s).

IO bi: Blocks received from a block device (blocks/s). bo: Blocks sent to a block device (blocks/s).

System in: The number of interrupts per second, including the clock. cs: The number of context switches per second.

CPU These are percentages of total CPU time. us: Time spent running non-kernel code. (user time, including nice time) sy: Time spent running kernel code. (system time) id: Time spent idle. Prior to Linux 2.5.41, this includes IO-wait time. wa: Time spent waiting for IO. Prior to Linux 2.5.41, included in idle. st: Time stolen from a virtual machine. Prior to Linux 2.6.11, unknown.

24

Page 25: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

scheduling

25

Page 26: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System MonitoringScheduling Jobs

• at• cron• anacron

26

Page 27: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

at

27

Page 28: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

at command

So that the multiprocessing CPU on a UNIX system does not get overloaded, some processes need to be run during low peak hours such as early in the morning or later in the day.

The at command is for this purpose.

The at command reads its stdin for a list of commands to run, and begins running them at the time of day specified as the first argument:

$ at 10:30pm < batch_file

$ at 11:59pmat> cat files.out bigshell > lab08at> cp lab08 /home/rsimms/cis90/$LOGNAME       at> Ctrl-D$ Note: the Ctrl-d must be entered as the first character on the last line.

Use the at command to schedule a command or script to run in the future

28

Page 29: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Load Balancing

Several ways to specify a future time to run

This job makes a backup of myscript and sends an email when finished

Use the atq command to show queued jobs

29

Page 30: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Load Balancing

The atrm command is used to remove jobs from the queue

The jobs command lists processes running or suspended in the background.

The atq command lists jobs queued to run in the futures that were scheduled by at command

30

Page 31: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

cron

31

Page 32: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System MonitoringScheduling Batch (cron) Jobs

• cron - Scheduling Program that executes tasks at a given time

• crontab - a program that submits tasks to the cron daemon

‒ crontab filename - registers your cron file ‒ crontab -l - displays your current cron file ‒ crontab -r - removes your current cron file ‒ crontab -u user - sets a cron job for another

user • Crontab service can be denied or

allowed on a per/user basis • crond cron daemon examines crontab

files and at command files. 32

Page 33: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

cron

crond's parent is init

crond daemon lives in /usr/sbin

service command can be used to stop and start (Red Hat family)

checkcfg command can be used to configure startup by run level

33

Page 34: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System MonitoringScheduling Batch (cron) Jobs

Two cron options:• "System" cron jobs

• Uses /etc/crontab (has additional user field)• No need to edit cron files just drop new jobs in:

• /etc/cron.daily/• /etc/cron.weekly/• /etc/cron.monthly/

• Fortified with anacron

• "User" cron jobs• Uses cron files kept in /var/spool/cron• Use crontab commands to create, view, and remove cron files• One cron file per user (multiple entries for multiple jobs)

34

Page 35: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System cron jobs

[root@benji ~]# ls /etc/cron*/etc/cron.deny /etc/crontab

/etc/cron.d:sa-update

/etc/cron.daily:0anacron cups makewhatis.cron prelink tmpwatch0logwatch logrotate mlocate.cron rpm

/etc/cron.hourly:

/etc/cron.monthly:0anacron

/etc/cron.weekly:0anacron makewhatis.cron[root@benji ~]#

[root@benji ~]# cat /etc/crontabSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=rootHOME=/

# run-parts01 * * * * root run-parts /etc/cron.hourly02 4 * * * root run-parts /etc/cron.daily22 4 * * 0 root run-parts /etc/cron.weekly42 4 1 * * root run-parts /etc/cron.monthly[root@benji ~]#

Note: 0anacron is the first job to run in each cron directory. anacron complements cron by handling jobs that were scheduled to run when the system was down.

spam assassin cron file

Has additional user field

35

Page 36: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Create a cron job with the crontab –e command which uses vi

Creating user cron jobs

Option 1

36

Page 37: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Create a cron job with vi then register it

Creating user cron jobs

Option 2

37

Page 38: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

30 08 1 3,6,9,12 * /root/bin/script1

Min Hr Day Month Weekday Job

At 8:30 AM, on the first day of every quarter

User cron jobs

Note: No user field like in /etc/crontab

Field Value Range

Min 00-60

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

38

Page 39: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

30 10 * * 6 /root/bin/script1

At 10:30 AM every Saturday

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

39

Page 40: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

30 10 * * 1-5 /root/bin/script1

At 10:30 AM week days Monday through Friday

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

40

Page 41: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

0,10,20,30,40,50 * 5 5 * /root/bin/script1

script1 runs every 10 minutes on May 5th

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

41

Page 42: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

*/5 * * * * /root/bin/script1

script1 runs every 5 minutes

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

42

Page 43: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

*/10 * * * * /root/bin/script1

script1 runs every 10 minutes

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

43

Page 44: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

30 07 1 4 * /root/bin/script131,32,33 07 1 4 * /root/bin/script2

On April 1st, script1 is run at 7:30AM followed by script2 running at 7:31AM, 7:32AM and 7:33AM

User cron jobs

Field Value Range

Min 00-59

Hr 00-23

Day 01-31

Month 01-12

Weekday 0-6 (0=Sunday)

Time value option Description

n Single time event

n,m,o Multiple time events

n-m Range of time events

*/n Repetition

Min Hr Day Month Weekday Job

44

Page 45: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

10 0 * * * /root/bin/script >> /root/script.log 2>&1

00 18 */2 * * /root/bin/script >> /root/script.log 2>&1

0 8 * * 1-5 /root/bin/script >> /root/script.log 2>&1

0 12 4 7 * /root/bin/script >> /root/script.log 2>&1

Everyday at 10 minutes past midnight

Every other day at 6:00 PM

Weekdays at 8:00 AM

Noon on the 4th of July

User cron jobs

Min Hr Day Month Weekday Job

45

Page 46: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

*/2 * * * * echo `date` "To file" >> /root/crontest*/2 * * * * echo `date` "To email"

stdout and stderr get emailed if not redirected

User cron jobs

Where does job output go ?????????????

46

Page 47: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

[root@benji ~]# ls -l /var/spool/crontotal 2-rw------- 1 root root 127 Nov 29 08:29 root

[root@benji ~]# cat /var/spool/cron/root* * * * * echo "Every minute: " `date` >> /root/crontest-1*/2 * * * * echo "Every 2 minutes: " `date` >> /root/crontest-2[root@benji ~]#

[root@benji ~]# crontab -l* * * * * echo "Every minute: " `date` >> /root/crontest-1*/2 * * * * echo "Every 2 minutes: " `date` >> /root/crontest-2[root@benji ~]#

crontab –l lists the user's cron jobs

User cron jobs are stored in /var/spool/cron/ by user name

User cron jobs

47

Page 48: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

crontab –r will delete a user' s cron configuration file

User cron jobs

48

Page 49: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Linux lets anyone run cron jobs.

You can use /etc/cron.allow and /etc/cron.deny to manage as necessary. Add users, one per line.

If there is a conflict, the most restrictive will apply

User cron jobs

49

Page 50: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Create a cron file as root to perform a backup of the /home file system. Do a full level 0 backup, followed by 3 level 1 backups, followed by 3 level 2 backups.

Backup example

[root@benji ~]# mkdir -p /backup/level0 /backup/level1 /backup/level2[root@benji ~]# vi cron[root@benji ~]# cat cron30 7 * * * /sbin/dump 0uf /backup/level0/backup-L0-`date +\%Y-\%d-\%m`.dmp /home31-33 7 * * * /sbin/dump 1uf /backup/level1/backup-L1-`date +\%Y-\%d-\%m`.dmp /home34-36 7 * * * /sbin/dump 2uf /backup/level2/backup-L2-`date +\%Y-\%d-\%m`.dmp /home

[root@benji ~]# crontab cron[root@benji ~]# crontab -l30 7 * * * /sbin/dump 0uf /backup/level0/backup-L0-`date +\%Y-\%d-\%m`.dmp /home31-33 7 * * * /sbin/dump 1uf /backup/level1/backup-L1-`date +\%Y-\%d-\%m`.dmp /home34-36 7 * * * /sbin/dump 2uf /backup/level2/backup-L2-`date +\%Y-\%d-\%m`.dmp /home

[root@benji ~]#

Note, for demonstration purposes we are doing the backups every minute rather than everyday.

escapes needed

User cron jobs

50

Page 51: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Backup example continued

[root@benji ~]# mailMail version 8.1 6/6/93. Type ? for help."/var/spool/mail/root": 7 messages 7 new>N 1 [email protected] Mon Dec 1 07:30 41/1749 "Cron <root@benji> /sbin/dump 0uf /backu" N 2 [email protected] Mon Dec 1 07:31 42/1807 "Cron <root@benji> /sbin/dump 1uf /backu" N 3 [email protected] Mon Dec 1 07:32 42/1807 "Cron <root@benji> /sbin/dump 1uf /backu" N 4 [email protected] Mon Dec 1 07:33 42/1807 "Cron <root@benji> /sbin/dump 1uf /backu" N 5 [email protected] Mon Dec 1 07:34 42/1807 "Cron <root@benji> /sbin/dump 2uf /backu" N 6 [email protected] Mon Dec 1 07:35 42/1807 "Cron <root@benji> /sbin/dump 2uf /backu" N 7 [email protected] Mon Dec 1 07:36 42/1807 "Cron <root@benji> /sbin/dump 2uf /backu"&

Backups results are emailed

User cron jobs

51

Page 52: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

& 1Message 1:From [email protected] Mon Dec 1 07:30:02 2008Date: Mon, 1 Dec 2008 07:30:02 -0800From: [email protected] (Cron Daemon)To: [email protected]: Cron <root@benji> /sbin/dump 0uf /backup/level0/backup-L0-`date +%Y-%d-%m`.dmp /homeContent-Type: text/plain; charset=UTF-8Auto-Submitted: auto-generatedX-Cron-Env: <SHELL=/bin/sh>X-Cron-Env: <HOME=/root>X-Cron-Env: <PATH=/usr/bin:/bin>X-Cron-Env: <LOGNAME=root>X-Cron-Env: <USER=root>

DUMP: Date of this level 0 dump: Mon Dec 1 07:30:01 2008 DUMP: Dumping /dev/sda7 (/home) to /backup/level0/backup-L0-2008-01-12.dmp DUMP: Label: /home DUMP: Writing 10 Kilobyte records DUMP: mapping (Pass I) [regular files] DUMP: mapping (Pass II) [directories] DUMP: estimated 90 blocks. DUMP: Volume 1 started with block 1 at: Mon Dec 1 07:30:02 2008 DUMP: dumping (Pass III) [directories] DUMP: dumping (Pass IV) [regular files] DUMP: Closing /backup/level0/backup-L0-2008-01-12.dmp DUMP: Volume 1 completed at: Mon Dec 1 07:30:02 2008 DUMP: Volume 1 150 blocks (0.15MB) DUMP: 150 blocks (0.15MB) on 1 volume(s) DUMP: finished in less than a second DUMP: Date of this level 0 dump: Mon Dec 1 07:30:01 2008 DUMP: Date this dump completed: Mon Dec 1 07:30:02 2008 DUMP: Average transfer rate: 0 kB/s DUMP: DUMP IS DONE

&

CIS 191 - Lesson 12

This is an email of the level 0 backup

Backup example continued

52

Page 53: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

Note: After the backup jobs have been run, the dump files appear in the appropriate directories

The last dumps for each level recorded here

User cron jobs Backup example continued

53

Page 54: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

date command

BTW, Here is how you set the system date and time

54

Page 55: CIS 191 - Lesson 12 System Monitoring 1. CIS 191 - Lesson 12 ps command 2

CIS 191 - Lesson 12

System Monitoring

Process Management

• ps

‒ Provides snapshot of all system processes

• uptime

‒ Can be used as indication of systematic degredation

• top

‒ Interaction version of ps with additional summary info

• vmstat

‒ Provides snapshot of overall memory55