cron exercise

Do this exercise on an instance of the class virtual machine. Login as student, start the GUI (startx), open a terminal window, become root in it ( sudo su - ).

In the root terminal window create three new users for this exercise.

useradd cronicler1
useradd cronicler2
useradd cronicler3

Set the password for each of them to  "password".

echo  -n  "password"  |  passwd  --stdin  cronicler1
echo  -n  "password"  |  passwd  --stdin  cronicler2
echo  -n  "password"  |  passwd  --stdin  cronicler3

Open a second terminal window (find "New Window" from within the first one). Within it become cronicler1:

su cronicler1

You now have two arenas in which to work, one as root and one as cronicler1.

We will install a scheduled job for each cronicler using different techniques. We will want to use webmin (remote web-based administration interface). We assume it is installed. If not see installation instructions included here, and go ahead and install it.


In the root terminal window create a test log file into which our cron jobs (i.e., scheduled jobs) can write things.

touch /tmp/testcron.log
chmod 766 /tmp/testcron.log
      (for universal writability)

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

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

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

ls -la /var/spool/cron/

It should be empty. In cronicler1's terminal window, view  cronicler1's current crontab file:

crontab -l

There is 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. (As a convenience you could launch an analog clock on the desktop:  "xclock  -update  1  &"  )

Now switch to root's terminal window 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

You will now set up a cron job for cronicler3 through webmin. You need to access webmin through a browser. Launch a graphical browser from the desktop "favorites" icon and go to URL "https://127.0.0.1:10000". If you see warnings, ignore and click through them till Webmin's login challenge appears. Use the machine's normal account "root" and its normal password. Webmin's main screen looks like this:

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 on, whether the same one or not. Click "system" on the main menu. On the ensuing screen click the "Scheduled Cron Jobs" icon. Notice that the cron jobs established for the 2 users above are shown. Click "Create a new scheduled cron job." Make the following entries on the resulting screen:

 

 

Be sure to specify cronicler3, enter the command shown above, click on the the "Times and dates selected below" button, and the five buttons for the time fields (minutes, hours,...) which is equivalent to a manually entered schedule of  *  *  *  *  * . 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 (if you use gedit launch it at the root command line, not the graphical icon, so that it will run as root and be able to write the file). 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 syntax difference between the entries you just made in /etc/crontab, versus those you made earlier in the individual user crontabs?

 

To turn in:

In your root terminal window run a command the reveals your hardware address and the recent entries in your cron log file:

clear
ifconfig  enp0s3  |  grep  ether
tail  /tmp/testcron.log

Send me a screenshot showing this terminal window. Name the screenshot file "cron.jpg" or .png. Mine looks like this:

Cleaning up:

If you let this machine run, it will continue growing the log file without bound. If you wish to clean it up, see below. If it is a virtual machine that is dispensible to you, remove it altogether.

Cleanup steps:
 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)