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:

  1. Open the Cron Table for the Root User:

    • 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 /tmp directory.

  2. Add the Cron Job to Delete Empty Directories:

    • Press I to enter insert mode, then add the following line:

    • 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 find command, which:

        • -type d: Searches for directories.

        • -empty: Finds only empty directories.

        • -delete: Deletes the found directories.

  3. Save and Exit the Crontab:

    • Press Esc to switch to command mode, then type :wq and press Enter to save the changes and exit the crontab editor.

  4. Verify the Scheduled Cron Job:

    • Explanation: This command lists all active cron jobs for the root user, allowing you to confirm that the job has been scheduled as expected.

  5. Test the find Command (Optional):

    • Before relying on the cron job, you can manually run the find command to ensure it performs as expected:

    • 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.

  6. Check Cron Logs (Optional):

    • To verify that the cron job runs as expected, you can check the cron logs after the next scheduled run:

    • 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 /tmp without 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 /tmp universally.

    • Testing the Command: Testing the find command 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.

You must be logged in to post a comment.

Preparing For

Your Certification?

255+ certifications
Detailed explanations
Free PDF samples

Has All The Questions You Need