Skip to main content

Linux Interview Questions

Part #5: Advanced Cron Jobs, Networking, and Shell Scripting (100–121 Questions)

In this part of the Linux Interview Handbook, we dive into advanced cron jobs, task scheduling, networking with multiple IPs, static routes, DNS troubleshooting, and shell scripting for automation.

Welcome to Part 5 of our Linux Interview Questions Series, where you will work with intermediate-level topics that go beyond simple commands and face questions designed to check your knowledge and your ability to apply Linux concepts in day-to-day situations.

πŸ•’ Advanced Cron Jobs & Scheduling

How do you list user cron jobs, schedule tasks every 5 minutes, run jobs only on weekdays, and debug when something goes wrong?

100. How do you view all scheduled cron jobs for a user?

You can use the crontab command with the -l flag and specify the user with -u:

crontab -l -u username

If you want to see the current logged-in user’s cron jobs:

crontab -l

You can also check system-wide cron jobs in these locations:

/etc/crontab
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.weekly/
/etc/cron.monthly/

101. What is the difference between /etc/crontab and user crontabs?

The main difference between /etc/crontab and user crontabs lies in scope, permissions, and format.

/etc/crontab is a system-wide crontab file that can be edited only by the root user and allows scheduling tasks for any user on the system because each line includes an extra field to specify the username that will run the command.

For example, an entry in /etc/crontab looks like this:

# m h dom mon dow user command
0 2 * * * root /usr/local/bin/backup.sh

On the other hand, user crontabs are specific to individual users and can be managed with the crontab command. Users cannot specify a different username; the commands run as the user who owns the crontab.

For example, to edit your own crontab, you would run:

crontab -e

And an entry in a user crontab looks like this:

0 2 * * * /home/user/backup.sh

This runs backup.sh at 2 AM as that user. You can also list your crontab entries with crontab -l or remove them entirely with crontab -r.

In short, /etc/crontab is system-wide and can run commands as any user, while user crontabs are per-user and run as that user.

102. How do you prevent email notifications from cron jobs?

When you run cron jobs, by default, any output (standard output or errors) is sent to your email. To prevent these email notifications, you need to ensure the cron job produces no output or explicitly redirects it.

The most common way is to redirect both standard output (stdout) and standard error (stderr) to /dev/null.

0 2 * * * /path/to/your/script.sh > /dev/null 2>&1

Here, > /dev/null sends the normal output to nowhere, and 2>&1 ensures that any errors are also discarded.

Another approach is to set the MAILTO variable at the top of your crontab to an empty string, which disables all email notifications for that user:

MAILTO=""
0 2 * * * /path/to/your/script.sh

103. How do you schedule a cron job to run every 5 minutes?

To schedule a cron job to run every 5 minutes, you need to edit the crontab file, which is the configuration file that specifies scheduled tasks for your user or system.

crontab -e

To run a job every 5 minutes, you can add a line like this:

*/5 * * * * /path/to/your/command

Here’s what each part means:

  • */5 β†’ every 5 minutes
  • * β†’ every hour
  • * β†’ every day of the month
  • * β†’ every month
  • * β†’ every day of the week

So, if you want to run a script called backup.sh located in /home/user/scripts/, you would write: