cron exercise

Login as root. Create three new users for this exercise.

useradd cronicler1
useradd cronicler2
useradd cronicler3

Using the "passwd <user>" command, set "password" as the password for each user.

We will install a scheduled job for each one using different techniques. We want to use webmin (remote web-based administration interface) so must install it. Get webmin per your instructor. It is likely to take the form of a file with a name like webmin-1.250-1.noarch.rpm (the version numbers may differ).

cd
ftp <address of ftp server per instructor>
[supply user name "anonymous" and no password when prompted]
ftp> cd pub
ftp> get webmin-1.250-1.noarch.rpm
ftp> quit

Install it using the rpm (RedHat package manager) system:

rpm -Uvh webmin-1.250-1.noarch.rpm

Create a test log file to which our cron jobs (i.e., scheduled jobs) can write things.

touch /tmp/testcron.log
chmod 766 /tmp/testcron.log

We will install scheduled jobs for our users by the following methods:

cronicler1 - user will install his own job
cronicler2 - root will install job for him
cronicler3 - webmin

Before doing anything, examine the content of the /var/spool/cron/ directory:

ls -la /var/spool/cron/

It should be empty. Now at a virtual terminal (don't su from a GUI xterm window) login as cronicler1. View your (i.e., cronicler1's) current crontab file:

crontab -l

You have no such file, and the command should so indicate. Launch a cronfile editor:

crontab -e

Enter the following line:

* * * * *   /bin/echo "$USER's self-installed cron job" >> /tmp/testcron.log

Save and exit as you would from the vi editor (colon, w, q, enter). Switch to root once again and re-examine the content of /var/spool/cron/. There should be a file there named cronicler1. Wait two minutes. Examine the test log's contents:

cat /tmp/testcron.log

There should be a new line in the file reading "cronicler1's self-installed cron job". Another such line should appear in that file every minute on the minute.

Now switch to root once again in order to install a cron job for cronicler2 on his behalf:

crontab -u cronicler2 -e

In the resulting editor session enter this line (identical to the previous except it declares a "root-" instead of "self-"installed cron job)::

* * * * *   /bin/echo "$USER's root-installed cron job" >> /tmp/testcron.log

Re-examine the content of /var/spool/cron/. There should be another file there, cronicler2. Wait a bit and again examine the test log's contents. You should see a mix of entries similar to:

cronicler1's self-installed cron job
cronicler1's self-installed cron job
cronicler2's root-installed cron job
cronicler1's self-installed cron job
cronicler1's self-installed cron job
cronicler2's root-installed cron job

As root you will now set up a cron job for cronicler3 through webmin. You need to access webmin through a browser. Start GUI mode ("startx" command) if not already started; there launch a graphical browser and go to URL "http://127.0.0.1:10000". Webmin's login challenge appears. Use the machine's normal account "root" and its normal password. You are talking to the local copy of webmin on your very machine. Webmin is usually used on a remote machine but like all IP servers is indifferent to which machine the client runs, whether the same one or not. Click "system" on the iconic horizontal menu bar. On the ensuing screen click the "Scheduled Cron Jobs" icon. Scroll to the bottom and look for a reflection of the cron jobs established for the 2 users above. Click "Create a new scheduled cron job." Make the following entries on the resulting screen:

 
Additionally, using the option buttons just below, select "Times and dates selected below" and select " and then "All" five times, once for each time field (equivalent to a manually entered " * * * * * "). Then press the "Create" button at page bottom. After the screen refreshes, from within webmin, note the reflection of the new cron job along with the other two you noted before. Note as well, from outside webmin in your character terminal as root:

 

ls -la /var/spool/cron/
cat /tmp/testcron.log

You should see a mix of entries similar to:

cronicler1's self-installed cron job
cronicler2's root-installed cron job
cronicler2's root-installed cron job
cronicler1's self-installed cron job
cronicler2's root-installed cron job
cronicler3's webmin-installed cron jo
cronicler1's self-installed cron job
cronicler1's self-installed cron job
cronicler3's webmin-installed cron jo
cronicler2's root-installed cron job

Now create another crontab file. This one will not be "personal" to any particular user but global to the computer. And you won't edit or access it with the crontab command but, as root, with vi or another ordinary editor. The file is /etc/crontab. It pre-exists. Open it, take note of its contents, and append the following to it:

* * * * * cronicler1 /bin/echo "$USER, from /etc/crontab" >> /tmp/testcron.log
* * * * * cronicler2 /bin/echo "$USER, from /etc/crontab" >> /tmp/testcron.log
* * * * * cronicler3 /bin/echo "$USER, from /etc/crontab" >> /tmp/testcron.log

After a couple of minutes observe the content of the log file again:

cat /tmp/testcron.log

You should see a mix of entries simliar to:

cronicler3, from /etc/crontab
cronicler2, from /etc/crontab
cronicler1, from /etc/crontab
cronicler2's root-installed cron job
cronicler1's self-installed cron job
cronicler3's webmin-installed cron job
cronicler3, from /etc/crontab
cronicler2, from /etc/crontab
cronicler1, from /etc/crontab
cronicler2's root-installed cron job
cronicler1's self-installed cron job
cronicler3's webmin-installed cron job

What is the point of difference between the syntax of the entries you just made in /etc/crontab, versus those you made earlier in the individual user crontabs?

 

Clean up:

remove the 3 user crontabs from /var/spool/cron/
back your edited changes out of the system crontab /etc/crontab
remove the test logfile /tmp/testcron.log
remove the 3 user accounts (e.g., userdel -r cronicler1)