% CST8177 Assignment 06 - Process Management, EMail, crontab, at % Todd Kelley Richard Donnelly Ian! D. Allen - idallen@idallen.ca - www.idallen.com % Winter 2013 - January to April 2013 - Updated Tue Apr 9 17:40:30 EDT 2013 Due Date and Deliverables ========================= - **Due Date**: `23h59 (11:59pm) Saturday March 16, 2013 (end of Week 9)` - Late assignments or wrong file names may not be marked. Be punctual. - **Available online**: - Version 1: 09h15 Mon Mar 4, 2013 (Preliminary) - Version 2: 11h50 Wed Mar 6, 2013 - Version 3: 14h10 Wed Mar 6, 2013 (clarify output of process ID) - Version 4: 02:45 Sat Mar 9, 2013 (suggest charset fixes for degrees) - Version 5: 14:30 Wed Mar 13, 2013 (emphasize re-use of `temperature.sh`) - Version 6: 02:30 Thu Mar 14, 2013 (emphasize name `mail_temperature` even more) - **Prerequisites**: - [CST8207 GNU/Linux Operating Systems I] - an ability to **READ ALL THE WORDS** to work effectively - **Deliverables**: 1. One text file uploaded to Blackboard according to the steps in the [Checking Program] section below. 2. Directory structure and files created and left for marking on the [Course Linux Server] (**CLS**).\ **Do not delete any assignment work from the CLS until after the term is over!** 3. **See Also:** [CentOS 5.8 Installation] - This is not checked in this assignment, but it will be in a future assignment. Do the install this week. > Since we also do manual marking of student assignments, your final mark may > not be the same as the mark submitted using the current version of the > [Checking Program]. Write the scripts according to the specifications, > not according to the incomplete set of the mistakes detected by the > [Checking Program]. Purpose of this Assignment ========================== 1. Create shell scripts that deal with process management, email, `crontab`, and `at`. 2. Practise with a text editor. Remember to **READ ALL THE WORDS** to work effectively and not waste time. Introduction and Overview ========================= This is an overview of how you are expected to complete this assignment. Read all the words before you start working. Complete the [Tasks] listed below on the [Course Linux Server] (**CLS**). Test your script using the given examples. Run a [Checking Program] to verify your work after you have run your own tests. Submit the output of the checking script to Blackboard before the due date. > Since we also do manual marking of student assignments, your final mark may > not be the same as the mark submitted using the current version of the > [Checking Program]. Write the scripts according to the specifications, > not according to the incomplete set of the mistakes detected by the > [Checking Program]. Some of the tasks below ask you to write a small executable shell script, based on the lecture notes and slides. All scripts should begin with the International Script Header you used for your previous assignments. When you have completed each script, ensure that it is executable, so that it can be run as `./scriptname.sh`. Scripts must validate their arguments, even if they take no arguments. (A script that takes no arguments must not run if you give it arguments, since clearly you don’t know how to use it.) Error messages must be written in the style of [Good Error Messages], as you did in the previous assignment. Always follow the Good Error Message with a Usage message that says how the script should be run. When you are finished the tasks, leave the files, directories, `cron` jobs, and `at` jobs in place as part of your deliverables. **Do not delete any assignment work until after the term is over!** Assignments may be re-marked at any time; you must have your term work available right until term end. The previous term’s course notes are available on the Internet here: [CST8207 GNU/Linux Operating Systems I]. All the notes files are also on the CLS. You can learn about how to read and search these files using the command line on the CLS under the heading *Copies of the CST8207 course notes* near the bottom of the page [Course Linux Server]. Remember to **READ ALL THE WORDS** to work effectively and not waste time. The Source Directory -------------------- All references to the “Source Directory” below are to the directory `~idallen/cst8177/13w/assignment06/` and that name starts with a *tilde* character followed by a userid with no intervening slash. Tasks ===== - Do the following tasks in order, from top to bottom. - These tasks must be done in your account on the [Course Linux Server]. - **READ ALL THE WORDS!** and do not skip steps. Set Up ------ 1. Make the directory `~/Assignments/assignment06`, in which you will create the files and scripts resulting from the following tasks. 2. Create the `check` symbolic link needed to run the [Checking Program]. Terminating Processes --------------------- 8. Write a script to find the Process ID of a command run by a user 1. Refer to your work from [Assignment #02 Task 4.6] 2. Name the script `process_id.sh` 3. The script takes two arguments, the first is the *username* of the process owner, and the second is the name of the *process*: - `./process_id.sh` *username*   *process* - The active part of the script will be one line long, plus code to check the number of arguments (two arguments), plus the script header. 4. The script prints one or more lines containing the process ID of every command with the given *process* name owned by the given *username*. It should not show any lines for processes owned by other users. (The process ID does not have to be the only thing on the line. The line output may contain other information, or you can optionally process the line to only show the process ID field.) 5. The script prints a [Good Error Message][Good Error Messages] and Usage message if it doesn’t receive exactly two arguments. 9. Run the script `signaltrap.sh` which is located in the [Source Directory]. You’ll notice this script doesn’t print anything – it just sits there doing nothing, and you don’t get your `bash` prompt back because the script isn’t finished yet. The `bash` prompt won’t appear until the script process is finished or stopped. 1. Type `^C` to try to interrupt this script process (send it an `INT` signal). - Notice that the process did not do the normal thing (die) upon receiving the `INT` signal 2. Type `^Z` to send a `STOP` signal to the script process. - You will get your `bash` prompt back and see a message about the script process being `Stopped` by the signal. 3. Use `bg` to resume the script process running in the background. 4. Use your `process_id.sh` script to find and verify the process id of this script process. - Be sure your script finds *your* process, not any other process! 5. Send the script process a `TERM` signal (terminate) and notice that this too didn’t make it do the expected (clean up and die). 6. Finally, send the process a `KILL` signal. - `KILL` signals can’t be ignored, so it should be gone – verify this. 7. Find a `PID` of any process owned by `root` or by another user, and send it a `KILL` signal (or any signal). - Notice that regular users cannot kill other users’ processes. Sending information using a `mail` Command ------------------------------------------ > There are many text-mode email clients (“Mail User Agents”) for Linux. This > assignment uses the one named `mail`, which is fairly standard and fairly > small, which makes it ideal for use in sending email inside scripts. > **Optional:** If you are interested in trying a power-user text-mode mail > client for everyday interactive use, try [`mutt`] (`man mutt`). 10. Create an Ottawa Weather script. - Refer to work shown in CST8207 [Examples of Pipes]. (Scroll down to the *Ottawa weather temperature* example.) 1. As shown in the above example, use the text browser `elinks` with the given options (reproduced below) to retrieve the Ottawa Weather URL - `elinks -dump -no-numbering -no-references` *URL* - Note: the shell meta-character `?` in the URL will try to expand. [Do not let that happen.] - `elinks` defaults to fetching pages using the UTF8 character set, which may not display well on your screen. You can change the character set using the `-dump-charset` option followed by a new character set. (Try `ascii`, `latin1` or the default `utf8`.) 2. As shown in the above example, pipe the `elinks` output into `grep` with some options to extract the line with the word `Temperature:` and the line that follows it that is the actual temperature. The pipeline will print two lines. - If the degree special character between the temperature number and the letter “C” doesn’t display correctly (e.g. “10°C”), you may need to [reconfigure PuTTY for the UTF8 character set]. (Remember to save your PuTTY settings.) 3. Put this pipeline into a script named `temperature.sh` that prints out the current temperature for Ottawa. The active part of the script will be one line long, plus code to check the number of arguments (no arguments), plus the script header. - Do not create or use an `alias` in your script; use the full `elinks` command line with the correct URL. - You should see only two lines on your screen when you run the script correctly. 11. Read the man page for the `mail` command and try sending yourself email: 1. `echo "Testing mail command." | mail -s "Test"` *abcd0001*`@algonquinlive.com` - replace *abcd0001* with your **own** Algonquin email address. - You can pipe any text standard output into the `mail` program. - Do not send binary data this way; it won’t work. **Text Only** 2. EMail the output of your `temperature.sh` weather script to your Algonquin email address with the subject: `Current Ottawa Temperature` - If the degree special character between the temperature number and the letter “C” doesn’t display correctly in your email (e.g. “10°C”), you can try adding either `-dump-charset latin1` or `-dump-charset ascii` to the `elinks` command line in your `temperature.sh` script. 3. **Optional:** Use an [SMS Gateway List] to find the email address of your SMS enabled phone, and email a test message directly to your phone. (May be subject to length restrictions and incur an arbitrary delay.) - If the degree special character between the temperature number and the letter “C” doesn’t display correctly on your phone (e.g. “10°C”), you can try adding either `-dump-charset latin1` or `-dump-charset ascii` to the `elinks` command line in your `temperature.sh` script. 12. Create a script called `mail_temperature.sh` that will email the current Ottawa temperature to your Algonquin email address, or your phone. - The script will use your existing `temperature.sh` script to generate the temperature for the script pipeline. - Do not use any output or input temporary files. - Do not duplicate the `elinks` line in this new mail script; your new script must email the *output* created by running your previous script. - The active part of the script will be one line long, plus code to check the number of arguments (no arguments), plus the script header. - You can create this script correctly even if you have not got a working `temperature.sh` script. (Of course, you won’t be able to fully test it without a working `temperature.sh` script.) 1. Create a hard link to the `mail_temperature.sh` script in your own `bin/` directory and name the link `mail_temperature` - Verify that you can now type simply `mail_temperature` at a shell prompt to have the temperature sent to you. (If this doesn’t work, perhaps you haven’t set your `PATH` in your `.bashrc` as required in [Section 4.6 of Assignment #02]?) - Since sysadmin are generally lazy, and `mail_temperature` is a very long name to type, find a way to create another name for this script that is shorter and faster to type. (You choose the name; nobody is looking.) (Hint: Perhaps another hard link, or perhaps a shell `alias`?) Scheduling tasks with `crontab` and `at` ---------------------------------------- > - The `crontab` command is used to create tasks that are run repeatedly > by the system `cron` daemon program. These are commonly called `cron` > *jobs*. You can create personal `cron` jobs. There is also a system > file containing system `cron` jobs. > - The `at` command is used to create a task that runs only *once* at some > future date and time. These are commonly called `at` *jobs*. There is > no system file of `at` jobs, since these jobs only run once. 13. Read the course notes and the man page for the `crontab` command and note the syntax used to install a new `crontab` from a named file. Experiment with some `crontab` lines that run in the near future to make sure you know how it works. (If you don’t redirect the output of your tests, you will need to use the `mail` program to see the emailed output generated by your `cron` jobs.) 1. Create a `mail_weather.crontab` text file containing a `crontab` line designed to run your `mail_temperature` program 10:14am every day. **Use this exact name and this exact time in this file.** You will need to experiment with times in the near future while you are testing the `crontab` command, but make sure this file contains exactly the 10:14 time for marking. - You can create this `crontab` file correctly even if you have not got a working `mail_temperature` script. (Of course you won’t receive the correct output without a working `mail_temperature` script.) 2. Use the `crontab` command to submit your `mail_weather.crontab` file to the `crontab` system. 3. Learn the command that displays your scheduled `cron` jobs and display them to make sure your 10:14 job is there. 4. Did you use the `mail_temperature` script name and not the `mail_temperature.sh` name, as specified? 14. Read the course notes and the man page for the `at` command and note the syntax used to schedule an `at` job in the future. Experiment with some `at` command lines that run in the near future to make sure you know how it works. (If you don’t redirect the output of your tests, you will need to use the `mail` program to see the emailed output generated by your `at` jobs.) 1. Use the `at` command to schedule a run of your `mail_temperature` program at 10:00am, December 21, 2013. **Use this exact name and this exact time.** 2. Learn the command that displays a list of your scheduled `at` job numbers and display the list to make sure your December 21 job is there. - You may forget what command is scheduled in a job. What is the command syntax to show you the *content* of the job, not just the job number? (NOTE: The command that lists all your job numbers is not the same as the command that shows you what the content of the job actually is!) 3. Display the actual command that a specific `at` job will run. That is, display the actual command line that you submitted when you created the `at` job. - Assuming that the command being run by `at` is only one line long, how can you show *only* the command line itself and not all of the associated shell environment that is created as part of the job? (Hint: One command with one pipe.) 4. Did you use the `mail_temperature` script name and not the `mail_temperature.sh` name, as specified? When you are done ----------------- When you are finished, run the [Checking Program] to create an overall mark. > Since we also do manual marking of student assignments, your final mark may > not be the same as the mark submitted using the current version of the > [Checking Program]. Write the scripts according to the specifications, > not according to the incomplete set of the mistakes detected by the > [Checking Program]. Checking, Marking, and Submitting your Work =========================================== > Since we also do manual marking of student assignments, your final mark may > not be the same as the mark submitted using the current version of the > [Checking Program]. Write the scripts according to the specifications, > not according to the incomplete set of the mistakes detected by the > [Checking Program]. Check your work a final time using the `assignment06check` program symlink and save the output as described below. Submit your mark following the directions below. **Summary:** Do some tasks, then run the checking program to verify your work as you go. You can run the checking program as often as you want. When you have the best mark, upload the marks file to Blackboard. 1. There is a [Checking Program] named `assignment06check` in the [Source Directory] on the CLS. Create a symbolic link to this program named `check` under your new `assignment06` directory so that you can easily run the program to check your work and assign your work a mark. Note: You can create a symbolic link to this executable program but you do not have permission to read or copy the program file. To verify the symbolic link, try executing it. 2. Execute the above “check” program using its symbolic link. (Review the [CST8207 Search Path] notes if you forget how to run a program by pathname from the command line.) This program will check your work, assign you a mark, and display the output on your screen. (You may want to paginate the long output so you can read all of it.) You may run the “check” program as many times as you wish, to correct mistakes and get the best mark. 3. When you are done with checking this assignment, and you like what you see on your screen, redirect the output of the [Checking Program] into the text file `assignment06.txt` under your `assignment06` directory. Use the *exact* name `assignment06.txt` in your `assignment06` directory. You only get *one* chance to get the name correct. Case (upper/lower case letters) matters. Be absolutely accurate, as if your marks depended on it. Do not edit the file. 4. Transfer the above `assignment06.txt` file from the CLS to your local computer and verify its contents. Do not edit this file! No empty files, please! Edited or damaged files will not be marked. You may want to refer to this term’s updated [File Transfer] notes. 5. Submit the `assignment06.txt` file under the correct Assignment area on Blackboard (with the exact name) before the due date. Upload the file via the **assignment06** “Upload Assignment” facility in Blackboard: click on the underlined **assignment06** link in Blackboard. Use “**Attach File**” and “**Submit**” to upload your plain text file. No word-processor documents. Do not send email. Use only “Attach File”. Do not enter any text into the **Submission** or **Comments** boxes on Blackboard; I do not read them. Use only the “**Attach File**” section followed by the **Submit** button. (If you want to send me comments about your assignment, use email.) 6. Your instructor may also mark the `assignment06` directory in your CLS account after the due date. Leave everything there on the CLS. **Do not delete any assignment work from the CLS until after the term is over!** Use the *exact* file name given above. Upload only one single file of plain text, not HTML, not MSWord. No fonts, no word-processing. Plain text only. Did I mention that the format is plain text (suitable for VIM/Nano/Pico/Gedit or Notepad)? **NO EMAIL, WORD PROCESSOR, PDF, RTF, or HTML DOCUMENTS ACCEPTED.** No marks are awarded for submitting under the wrong assignment number or for using the wrong file name. Use the exact name given above. WARNING: Some inattentive students don’t read all these words. Don’t make that mistake! Be exact. **READ ALL THE WORDS. OH PLEASE, PLEASE, PLEASE READ ALL THE WORDS!** -- | Todd Kelley / Richard Donnelly and | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/ [Plain Text] - plain text version of this page in [Pandoc Markdown] format [CST8207 GNU/Linux Operating Systems I]: ../../../cst8207/12f [Checking Program]: #checking-marking-and-submitting-your-work [Course Linux Server]: 000_course_linux_server.html [CentOS 5.8 Installation]: 000_centos_install.html [Tasks]: #tasks [Good Error Messages]: assignment05.html#good-error-messages [Assignment #02 Task 4.6]: assignment02.html#creating-a-script [Source Directory]: #the-source-directory [`mutt`]: http://www.mutt.org/ [Examples of Pipes]: ../../../cst8207/13w/notes/200_redirection.html#examples-of-pipes [Do not let that happen.]: ../../../cst8207/13w/notes/440_quotes.html [reconfigure PuTTY for the UTF8 character set]: data/putty_utf8.png [SMS Gateway List]: http://en.wikipedia.org/wiki/List_of_SMS_gateways [Section 4.6 of Assignment #02]: assignment02.html#creating-your-first-shell-script [CST8207 Search Path]: ../../../cst8207/12f/notes/400_search_path.html [File Transfer]: 220_file_transfer.html [Plain Text]: assignment06.txt [Pandoc Markdown]: http://johnmacfarlane.net/pandoc/