PFA
Lab 10: System Administration Introduction In this lab, you will learn administrative tasks using automation tools available in Linux. Automation, if done properly, can alleviate many of the administrative tasks that would normally take up a lot of time for an administrator. For this lab, you will learn how to manage administrative tasks using cron and the at commands. You will also learn how to create, monitor, and terminate processes using the kill command. Finally you will learn how to prioritize processes using the nice command. Learning Outcomes After completing this lab, you will understand: · Manage cron and at jobs · Configure user access to cron and at services · Configure anacron · Redirect input and discard output · Use the tee command · View processes using the /proc directory · Work with the process monitoring commands · Identify the priority of a process · Manage the priorities of new processes · Change the priority of a running process Exercise 1: Automate System Administration Tasks by Scheduling Jobs Task 1: Manage cron jobs Linux offers two commands that can help you automate tasks. One such command is cron, which can be used to schedule recurring activities. The second command is the at command which is used to schedule a single command. The cron command loads a text file called a crontab. The crontab maintains a list of commands to execute and the time when they should be executed. A system file is maintained in the /var/spool/cron directory. Users can also use a personal crontab to schedule personal jobs to be run on a system. These jobs are stored in the same directory as the user’s name. For example, when Mary schedules her Document’s directory to be backed up, she will create a cron job that will save her crontab as /var/spool/cron/mary. All crontab files must be located in the /var/spool/cron directory. Cron is not a program but consists of the cron daemon (crond) and the crontab file. The at command is different. While cron will run hourly, daily, or even yearly, the at command schedules a task to be run once. You can define a set time for the job to run, for example, at 12:01 am tomorrow morning. However, once the job has completed, it will not run again until it is manually scheduled. For this task, log into your CentOS VM and open a terminal. The steps for this task begin on the next page. Also open a word processing document (i.e. MS Word, or OpenOffice Writer). Step 1 From your terminal, change to the root user using the su command. su - Figure 1: Logging in as root Step 2 Clear the screen with the clear command then use the rpm command to verify cron is installed. rpm -q cronie cronie-anacron Figure 2: Verifying cron is installed Step 3 Remember that crond schedules job using a crontab file and stores it in the /var/spool/cron. Check to see if there are any jobs scheduled. ls -l /var/spool/cron Figure 3: Checking on jobs Step 4 For any job to run, the crond service must be active, enabled, and running. For systems using SysV, the command to check the status of a service is service crond status. On systems using Systemd, the command is systemctl status crond. Some systems using Systemd will redirect service commands to systemctl. But this is not always the case. For CentOS, service commands are redirected to systemctl. Use systemctl to check the status of crond. Note: The command below shows the .service extension. This is optional unless there is a target named the same. It is better to use .service when there are custom targets or other even .device, .mount, or .path. systemctl status crond.service Figure 4: Checking the status of crond Step 5 Press q to exit out of systemctl and return to a prompt. Then use the clear command to clear the screen. The main configuration file is in the /etc/crontab. Verify there are no entries into this file using the cat command. cat /etc/crontab Figure 5: Examining the /etc/crontab file The output from this file provides an example of how to make an entry in the crontab file. There are 7 fields. Each field is listed in the table on the next page. One thing to note, When setting the day of the week for a job to run, you can use a number between 0 and 7. The Sunday can be represented by 0 or 7 depending on your personal preference. Some countries view Sunday as the last day of the week while other view it as the first. Therefore, Sunday can be represented with a 0 or a 7. You can also type the three character abbreviation for each day rather than use a number. Field Description Minute Any integer from 0 to 59 to identify the minute to run the job Hour Any integer from 0 to 23 to identify the hour to run the job Day Any integer from 1 to 31 to identify the day to run the job Month Any integer from 1 to 12, or the abbreviated month to identify the month to run the job Day of Week Any integer from 0 to 7, or the abbreviated day to identify the day of the week to run the job User Name The name of the user that the job will run as. This can also be a service name. This is optional Command The command to run. Step 6 Scripts can be created and saved into one of five directories. These directories are described in the following table. Directory Name Description /etc/cron.d Run jobs based on a schedule not specified as an hourly, daily, weekly or monthly job. /etc/cron.daily Scripts in this directory are run daily. Scripts much be shell scripts /etc/cron.hourly Scripts in this directory are run each hour. Scripts much be shell scripts /etc/cron.monthly Scripts in this directory are run every month. Scripts much be shell scripts /etc/cron.weekly Scripts in this directory are run every week. Scripts much be shell scripts View the files in the /etc/cron.hourly directory. ls /etc/cron.hourly Figure 6: Viewing the contents of the /etc/cron.hourly/ directory Step 7 The crontab command has several options. To find out who has a job scheduled, the -l option will list any jobs scheduled. The -u option can be used to specify a user. The -l option alone will list only the jobs for the current user. Verify that root does not have any jobs scheduled. crontab -l Take a screenshot and save it to your document. Figure 7: Listing scheduled jobs for the current user Step 8 Exit out of the root user and create a script that will backup all of the files in the admin’s Documents directory. Use a text editor or gedit to create the script. Save the script to the admin’s home directory. Save the file as backup.sh. Note: The TIME variable is using backtics. These are next to the 1 key. #!/bin/bash TIME=`date +%b-%d-%y` tar -cpjf /share/admin/documents-backup-$TIME.tar.bz2 /home/Documents Take a screenshot and save it to your document. Figure 8: Backup Script Step 9 The script could be moved into the /etc/cron.daily directory where it would run automatically every day. However, for this lab, an entry into the crontab file is needed. Enter the following command to open the crontab editor and create a job to run the backup.sh command daily at midnight. crontab -e Figure 9: The crontab editor Step 10 Make an entry to the crontab file. If you chose vim as the default editor, then press i to get into insert mode then enter the text below. If you chose nano as the default editor, enter the following text. 0 0 * * * bash /home/admin/backup.sh Figure 10: Making an entry into the user's crontab Step 11 The entry into the crontab file on the previous page will run at 12:00 am every day, every month, and every day of the week. When we created the script, we did not make it executable. Therefore we need to run the script with bash. The command bash /home/admin/backup.sh will execute the the script based on the schedule. Log back in as root and see if the user admin has a crontab scheduled. crontab -l -u admin Take a screenshot and save it to your document. Figure 11: Viewing a scheduled job Task 2: Manage at jobs The at command can also be used to schedule jobs. However, when the at command completes, the job is never run again. In the following steps, you will use the at command to schedule one time tasks to run and view the queue to verify they are scheduled. Step 1 To schedule a task that only needs to run once use the at command. The at command when executed will change to the at command prompt. Enter the following text to schedule a new job. Change the time to be 30 minutes from your current time. at 1:30PM today Take a screenshot and save it to your document. Figure 12: Starting an at job Step 2 In the at command prompt, enter the commands to execute. Enter one command at a time then press CTRL+D to exit the command prompt. Enter the following commands to run. ls -l / > /home/admin/Documents/mylist.txt ls -l /etc/ >> /home/admin/Documents/mylist.txt ls -l /share/ >> /home/admin/Documents/mylist.txt Figure 13: Command to run with the at command Step 3 Clear the screen and list the current jobs scheduled to run. at -l Figure 14: Current at job list Note: Your job ID may be different than the one shown in Figure 14. Step 4 Repeat step 2 to have two jobs scheduled. Change the time to be 30 minutes after the previous scheduled job. Then list the current jobs scheduled. Take a screenshot and save it to your document. Figure 15: Scheduling a second job Step 5 A job can be deleted from the queue using the -d option. Delete the first job with the following command. Change the job ID to match your the job ID for your list. at -d 2 Take a screenshot and save it to your document. Figure 16: Deleting a scheduled job Step 6 The atq command performs a similar function as the at -l command. Clear the screen and run the atq command to see what jobs are currently scheduled. atq Take a screenshot and save it to your document. Figure 17: Listing the current at queue Step 7 You can delete a scheduled job with the atrm command. This is similar to the at -d command from earlier. Remove the last scheduled job with the atrm command. atrm 3 Take a screenshot and save it to your document. Figure 18: Removing a job from the at queue Task 3: Configure User Access to cron and at