Lemur zaprasza
Linux: A Network Solution for Your Office ContentsIndex Chapter 18: Scheduled Tasks, Scripts, and Programming: Scheduling Program Execution Previous ChapterNext Chapter Sections in this Chapter: Scheduling Program Execution Examples Shell Scripts Summary Other Often Used Languages Manual Pages Previous SectionNext Section Chapter 18 Scheduled Tasks, Scripts, and Programming Under Linux, many system administration tasks can be greatly simplified through automation. The tools you use include a UNIX daemon that runs scheduled tasks in the background (crond) and simple programming utilities. No, I'm not planning to tell you that you need to become a programmer in order to manage a Linux server effectively. However, it's always a good idea to know at least a little bit about this subject. Just as DOS and Windows "power users" are able to automate many simple tasks using batch files--although it's a little bit of a stretch calling MS-DOS batch files a form of programming--you'll be able to do tasks more efficiently under Linux if you know how to create simple shell scripts to automate your work. First, let's see how you can automate the execution of scheduled tasks. Scheduling Program Execution The cron Daemon Running Commands with at Many system administration tasks are recurring. Whether it's the rotation of log files, the creation of weekly backups, or other daily, weekly, or monthly tasks, you can make your life much easier if you have these programs execute automatically. Linux (and indeed, most UNIXes) offer two tools for the scheduled execution of tasks. The cron daemon lets you schedule recurring tasks; the at command lets you schedule the execution of a specific task at a given time. The cron Daemon The cron daemon runs in the background. It regularly checks the contents of the /var/spool/cron directory ( /var/spool/cron/crontabs on some systems), rereading any files that have been changed. These files have names that coincide with user identifiers, and they contain schedules of programs that are to be executed under those users' names. For instance, if I want to run a program named myprog, located in my home directory, once every hour, I could have a crontab line like this one: 0 * * * * /home/vttoth/myprog The cron daemon uses a simple, yet powerful format for schedules. Each line in a crontab file looks like this: min hour day mon wday command The min, hour, and day arguments represent the minutes, hours, and day of month, respectively. Each of these arguments can be a single numeric value, a list of values following a special syntax, or a single asterisk (meaning any value ). The aforementioned special syntax is easier to demonstrate via a few examples than to explain. For instance, you may have the following values in place of the min field: 0 At the top of the hour 30 At the bottom of the hour 0,30 At the top and bottom of the hour 7,25,42 At 7, 25, and 42 minutes after the hour */15 Every 15 minutes */30,10-25/5 Every 30 minutes, plus every 5 minutes between the 10th and 25th minutes of the hour The hour and day fields follow a similar logic. The mon field represents the month. Three-letter abbreviations are used--for example, jan, feb, and so on. The wday field represents the day of the week. Again, three-letter abbreviations are used, and ranges can be specified. For example, mon-fri means the five workdays of the week. If you specify both a day of the month and a day of the week, the scheduled task will be executed on both dates. Suppose, for example, you have a crontab entry like this: 0 0 13 * fri /home/vttoth/badday The program badday will be executed on both the 13th of the month and every Friday, even if a particular Friday doesn't fall on the 13th. Your crontab entry can be edited or viewed using the crontab command. To bring up your crontab entry for editing, type crontab -e . This will open a copy of your crontab file using the dreaded vi editor. Note that the file may contain lines that begin with the pound sign ( #). These lines are considered comment lines and are ignored by the cron daemon. When you're finished editing the file, the crontab command will install your new crontab file automatically. You can also use crontab to simply list the contents of your crontab file: crontab .l . Lastly, as the root user, you can edit other users' crontab files using the .u parameter. For instance, you may want to edit the crontab file that belongs to the news or uucp system accounts in order to alter the respective programs' behaviors. Running Commands with at Sometimes you don't want programs to execute repeatedly--rather only once at a scheduled time. Although the cron daemon could be used for this purpose, it's not ideal. In fact, strictly speaking, there's no way to schedule a program to execute just once with cron; even if you specify an exact minute, hour, day, and month, the program will be executed again next year at the specified time, unless you change your crontab file in the intervening period. There's a much simpler solution to this problem: the at command. This command allows you to schedule the one-time execution of a single command or a series of commands. The at command executes in conjunction with the atd daemon, which is another system daemon that's usually started when the system boots. The command or commands you want to execute must be placed in a file. The contents of this file will be executed at the scheduled time by the /bin/sh shell, just as though you typed the commands yourself at the keyboard. To schedule execution of a command with at, at a given time, simply type: at HH:MM -f command-file Any output produced by the commands thus executed will be mailed to the user who scheduled the execution. For instance, if you want to see the size of user mailboxes in the /var/spool/mail directory at a later time of the day, you could create a file named viewmaildir with the following single line as its contents: ls -al /var/spool/mail You can then schedule execution of this command at 7 p.m. using at, as follows: at 19:00 -f viewmaildir At the scheduled time, the command will run and the directory listing will be emailed to you. Linux: A Network Solution for Your Office ContentsIndex Chapter 18: Scheduled Tasks, Scripts, and Programming: Scheduling Program Execution Previous ChapterNext Chapter Sections in this Chapter: Scheduling Program Execution Examples Shell Scripts Summary Other Often Used Languages Manual Pages Previous SectionNext Section © Copyright Macmillan USA. All rights reserved. |