In Mock exam 3, Add a cron job to the system-wide cron table. This cron job should run as a user called Mary, on the 15th day of every month, at 4 AM*. The command that the cron job should execute is: find /home/mary/ -type d -empty -delete

In LFCS mock exam 3, there was this question: Add a cron job to the system-wide cron table. This cron job should run as a user called Mary, on the 15th day of every month, at 4 AM*. The command that the cron job should execute is: find /home/mary/ -type d -empty -delete.

I answered as: as the question asked about the system-wide cron, i added:

/etc/crontab
0  4   15 * *   mary    find /home/mary/ -type d -empty -delete

but this was the solution given at the end of the exam: (this would add to the root user crontab but not to the system-wide cron)

sudo crontab -e
0 4 15 * * su - mary -c 'find /home/mary/ -type d -empty -delete'

I believe 2 things are incorrect in this:

  1. this would add to the root user crontab but not to the system-wide cron
    2 i am not sure if this correct 0 4 15 * * su - mary -c ‘find /home/mary/ -type d -empty -delete’ . also it doesn’t say to run it as mary user

Regards,
Sreenivas

Let’s look at the posted solution, and see how it’s structured (for details, do man 5 crontab):

[TIME FIELDS] command

Your time fields are right, but the command you are running is

mary    find /home/mary/ -type d -empty -delete

This is not a valid command; in the system crontab, there’s no field to indicate what user to run as, because the system crontab is run a root. So you need a command that will “run as Mary”. The way to do this is to use the su command. The solution’s command is

su - mary -c 'find /home/mary/ -type d -empty -delete'

which means: using the environment of user mary (su - mary) run the command (-c) …

which will run the command as user mary, using mary’s environment.

Hi,

Could you please give us more information about the first part of@sreenivasc question?
We’re asked to edit the system-wide crontab (sudo vim /etc/crontab) , however if we do, the question is considered wrong. The autocorrect pin the answer as right only if we edit the root crontab (sudo crontab -e).

Hi Rob, thanks for answering. There is a system crontab /etc/crontab that has time fields, username and command to run. Please take a look. there is also a separate sudo crontab -e for the root user and a separate crontab for each user. The question isn’t clear, if it should be run as a user mary then we could have directly added the crontab under that user’s crontab and we don’t have to use a username.