Executing Programs On A Schedule
The Linux scheduling application is known as cron. Cron is your friend when you want a program to run at a specific time every day, or at multiple times during the day.
The primary schedule is kept in a file known as crontab on most systems. A relatively normal location for this file is /etc/crontab. It is a text file that looks a bit odd, but is easily managed to create a variety of ways to get stuff to run when you want it to on a regular basis.
A sample crontab file:
-
SHELL=/bin/sh
-
PATH=/usr/bin:/usr/sbin:/sbin:/bin:/usr/lib/news/bin
-
MAILTO=root
-
#
-
# check scripts in cron.hourly, cron.daily, cron.weekly, and cron.monthly
-
#
-
-*/15 * * * * root test -x /usr/lib/cron/run-crons && /usr/lib/cron/run-crons >/dev/null 2>&1
-
*/5 * * * * root run-parts /etc/cron.fivemin
-
59 * * * * root rm -f /var/spool/cron/lastrun/cron.hourly
-
10 2 * * * root run-parts /etc/cron.0210
-
14 4 * * * root rm -f /var/spool/cron/lastrun/cron.daily
-
29 4 * * 6 root rm -f /var/spool/cron/lastrun/cron.weekly
-
44 4 1 * * root rm -f /var/spool/cron/lastrun/cron.monthly
This crontab tells the sytem to run a half-dozen different schedules including:
- Every 5 minutes – run whatever shell scripts you find in the /etc/cron.fivemin directory
- Every hour on the 59th minute – run whatever is in the /etc/cron/lastrun/cron.hourly directory
- Every night at 2:10 AM – run the scripts in /etc/cron.210
- Every night at 4:14 – run the shell scripts in /etc/cron.daily
You can see the pattern if you look closely. Details can be found on numerous websites and in your system docs (try man or info).
Getting Something To Run Every Hour
Using the sample crontab noted above, you can get a program to run every five minutes by simply creating a shell script (you do know how to do that, don’t you?) and placing it in the /etc/cron.fivemin directory. You’ll need to remember to set the executable bit BTW (755 is usually a good permission setting for the file).
Typically the shell script will run a program written in another language such as perl. For example a file named “notifyifcrashed.sh” might contain:
-
#!/bin/sh
-
cd /var/www/cgi-bin
-
./monitor.pl
-
exit 0
We’ll assume that the sendhello.pl perl applet does something friendly like send a notification email to our server admin if the applet detects the Apache web server has gone offline.
Devices
How Much Disc Space Is In Use
To list the disc space on a Linux system:
df -h
Results in something like this:
Filesystem Size Used Avail Use% Mounted on /dev/sda7 2.0G 261M 1.7G 14% / /dev/sda1 1012M 46M 915M 5% /boot none 2.0G 0 2.0G 0% /dev/shm /dev/sda8 121G 96G 19G 84% /home /dev/sda6 2.0G 71M 1.8G 4% /tmp /dev/sda2 9.9G 4.8G 4.6G 52% /usr /dev/sda5 9.9G 1.9G 7.5G 21% /var /dev/sdb1 147G 93M 140G 1% /backup /tmp 2.0G 71M 1.8G 4% /var/tmp
Listing Mounted Drive Partitions
For a Linux system running CentOS:
sort /etc/mtab
Will result in something like this:
/dev/sda1 /boot ext3 rw 0 0 /dev/sda2 /usr ext3 rw,usrquota 0 0 /dev/sda5 /var ext3 rw,usrquota 0 0 /dev/sda6 /tmp ext3 rw,noexec,nosuid 0 0 /dev/sda7 / ext3 rw,usrquota 0 0 /dev/sda8 /home ext3 rw,usrquota 0 0 /dev/sdb1 /backup ext3 rw 0 0 none /dev/pts devpts rw,gid=5,mode=620 0 0 none /dev/shm tmpfs rw 0 0 none /proc proc rw 0 0 none /proc/sys/fs/binfmt_misc binfmt_misc rw 0 0 none /sys sysfs rw 0 0 /tmp /var/tmp none rw,noexec,nosuid,bind 0 0 usbfs /proc/bus/usb usbfs rw 0 0
The columns:
- Device name
- Where it is mounted on the filesystem (the directory it is attached to)
- The file system type
- the rest of the columns are defaul permissions, etc.
Disc Device Names
If you look in the mounted drives table above you’ll see something interesting about the standard drive names:
- most start with /dev/sda
- one starts with /dev/sdb
This indicates that we have two physical drives mounted in the system. A “drive a” and “drive b”.
The numbers after the “a” or “b” indicate the partition on that drive. Our first drive is broken up into 6 pieces that we have access to:
/dev/sda1 /boot /dev/sda2 /usr /dev/sda5 /var /dev/sda6 /tmp /dev/sda7 / /dev/sda8 /home
The second drive is one piece mounted at /backup
/dev/sdb1 /backup