1. What is crontab used for in Linux?
A) crontab schedules tasks (cron jobs) to run automatically at specific times or intervals.
2. What is the syntax of a crontab entry?
A)
* * * * * command
| | | | |
| | | | └─ Day of week (0-6)
| | | └─── Month (1-12)
| | └───── Day of month (1-31)
| └─────── Hour (0-23)
└───────── Minute (0-59)
3. How do you list current user’s crontab jobs?
A)
crontab -l
4. How do you edit your crontab file?
A)
crontab -e
5. What is the difference between /etc/crontab and crontab -e?
A)
- /etc/crontab → system-wide, includes user field
- crontab -e → per-user cron jobs
6. What does 0 2 * * 1 /backup.sh mean?
A) Runs /backup.sh at 2:00 AM every Monday.
7. How do you run a cron job every 5 minutes?
A)
*/5 * * * * command
8. How do you redirect cron job output to a log file?
A)
* * * * * /script.sh >> /var/log/script.log 2>&1
9. How do you schedule a cron job for another user?
A)
crontab -u username -e
10. Where are cron logs stored in Linux?
A) Usually in /var/log/cron or /var/log/syslog (depends on distro)
11. A cron job is scheduled, but it’s not running. How do you troubleshoot?
A) Check if crond service is running:
systemctl status crond
Verify the job with crontab -l
Check logs:
grep CRON /var/log/cron # RHEL/CentOS
grep CRON /var/log/syslog # Ubuntu/Debian
Ensure correct PATH is set (cron has a limited environment).
12. You need to schedule a script to run at server reboot.
A)
@reboot /home/user/startup.sh
13. A cron job runs a script, but output is missing. How do you capture it?
A) Redirect stdout & stderr to a log file:
0 1 * * * /home/user/backup.sh >> /var/log/backup.log 2>&1
14. A script works manually but fails in cron. Why?
A) Likely due to environment variables (PATH, HOME, etc.).
Solution:
- Use absolute paths in scripts (/usr/bin/python instead of python).
- Define environment variables at the top of crontab:
- PATH=/usr/local/bin:/usr/bin:/bin
15. How do you stop duplicate cron jobs from running if the previous one is still running?
A) Use a lock file or flock:
* * * * * flock -n /tmp/backup.lock /home/user/backup.sh
16. You need to schedule different jobs for different users. How do you manage this?
A) crontab -u username -e
Or edit /etc/crontab (system-wide) with an extra user field:
30 2 * * * root /root/db-backup.sh
17. A cron job needs to run every 30 seconds (cron doesn’t support < 1 min). How do you handle this?
A) Cron can’t do <1 min directly. Workaround:
* * * * * /script.sh
* * * * * sleep 30; /script.sh
18. How do you temporarily disable a cron job without deleting it?
A) Comment it out with # in crontab. Example:
# 0 2 * * * /home/user/cleanup.sh
19. How do you ensure email notifications are sent for failed cron jobs?
A) Set MAILTO at the top of crontab:
MAILTO=admin@example.com
0 1 * * * /home/user/script.sh