Red Hat Certified System Administrator RHCSA · Free Practice Question Medium
Question 23
On ServerB, as the root user, create a cron job that deletes empty directories from the /tmp directory at 10 pm on weekends (Saturday and Sunday). Verify the cron job after setting it up.
-
A
Pass
-
B
Fail
Reveal correct answer
Correct answer: A
Explanation
Answer:
To create a cron job that deletes empty directories in /tmp every Saturday and Sunday at 10 pm, follow these steps:
Open the Cron Table for the Root User:
- # crontab -e
Explanation: This command opens the root user’s crontab file for editing. Only root privileges allow setting cron jobs that affect the entire system, including the
/tmpdirectory.
Add the Cron Job to Delete Empty Directories:
Press
Ito enter insert mode, then add the following line:- 00 22 * * 6,7 find /tmp -type d -empty -delete
Explanation of Each Part:
00: Specifies the minute (00) when the job will run (exactly on the hour).
22: Specifies the hour (10 pm, in 24-hour format).
*: Specifies every day of the month.
*: Specifies every month.
6,7: Specifies only Saturdays (6) and Sundays (7).
find /tmp -type d -empty -delete: Executes the
findcommand, which:-type d: Searches for directories.
-empty: Finds only empty directories.
-delete: Deletes the found directories.
Save and Exit the Crontab:
Press
Escto switch to command mode, then type:wqand pressEnterto save the changes and exit the crontab editor.
Verify the Scheduled Cron Job:
- # crontab -l
Explanation: This command lists all active cron jobs for the root user, allowing you to confirm that the job has been scheduled as expected.
Test the
findCommand (Optional):Before relying on the cron job, you can manually run the
findcommand to ensure it performs as expected:- # find /tmp -type d -empty -delete
Explanation: This test confirms that the command effectively deletes empty directories in
/tmp. Running it manually can reveal any syntax or permission issues before relying on the automated cron job.
Check Cron Logs (Optional):
To verify that the cron job runs as expected, you can check the cron logs after the next scheduled run:
- # grep CRON /var/log/cron
Explanation: This displays cron activity, helping you confirm whether the job ran at the scheduled time. Look for entries indicating the job’s execution at 10 pm on Saturday or Sunday.
Additional Notes:
Understanding Cron Timing Syntax:
00 22 * * 6,7: Specifies the job to run at 10:00 pm (22:00) on Saturdays and Sundays only.
find /tmp -type d -empty -delete: This command removes empty directories from
/tmpwithout affecting non-empty directories.
Common Errors and Troubleshooting:
Permissions: Ensure the cron job is created as root, as only root can modify and delete files in
/tmpuniversally.Testing the Command: Testing the
findcommand directly before adding it to the cron job helps avoid syntax errors.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
