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:
- 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.
@rob_kodekloud
I also struggled with this question in the same way as @sreenivasc. Kindly see the feedback from ChatGPT when I questioned how the question was phrased (SEE DETAILS BELOW). Question also refer to capital letter “M” for Mary but in actual fact based on solution, it should be “mary”.
Could you please help address this?
You’re right to question whether the original phrasing of the question is technically accurate. Let’s break it down:
Original 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
.”
Analysis of the phrasing:
-
“System-wide cron table” refers specifically to
/etc/crontab
, or other system-wide cron directories like /etc/cron.d/
. It does not refer to user-specific cron tables, such as those managed by sudo crontab -e
.
-
“Run as a user called Mary”: To run a job as a specific user in the system-wide cron table (
/etc/crontab
), you need to explicitly specify the user in the format of /etc/crontab
.
Corrected Phrasing:
A more technically accurate phrasing would be:
-
Option 1 (If
/etc/crontab
is intended):
“Add a cron job to the system-wide cron table (/etc/crontab
). This cron job should run as the user 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
.”
-
Option 2 (If
sudo crontab -e
is intended):
“Add a cron job that should run as the user Mary. It should run on the 15th day of every month, at 4 AM, and execute the command: find /home/mary/ -type d -empty -delete
.”
Explanation:
- If the goal is to specifically refer to the system-wide cron (
/etc/crontab
), then it’s essential to clarify that this file explicitly requires specifying the user, making Option 1 more accurate.
- If the intention is to add a job for the user Mary, but not necessarily through the system-wide crontab, then Option 2 is more appropriate, leaving flexibility to use either the user’s crontab (
crontab -u mary -e
) or a root-managed crontab using sudo crontab -e
.
Conclusion:
The original question is slightly ambiguous and could be rephrased for better technical clarity. Based on the requirement to “run as Mary,” it’s most correct to mention /etc/crontab
if system-wide jobs are intended or leave the system-wide requirement out entirely for user-specific jobs.