Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Add Cron Jobs to A Specific User in a Linux System
This article will teach you how to schedule cron jobs for specific users in a Linux system. Cron is a time-based job scheduler that allows you to execute commands or scripts automatically at specified times and dates.
General Syntax of a Cron Job
MIN HOUR Day of month Month Day of Week Command 0-59 0-23 1-31 1-12 0-6 linux command or script
The cron format consists of five time fields followed by the command to execute. Each field represents a different time unit, allowing precise scheduling control.
Viewing Existing Cron Jobs
To see the list of cron jobs for a specific user, use the -l (list) option −
# crontab -u test1 -l
no crontab for test1
Adding Cron Jobs to a Specific User
To add or edit cron jobs for a specific user, use the -e (edit) option with the -u (user) flag −
# crontab -u test1 -e
no crontab for test1 - using an empty one Select an editor. To change later, run 'select-editor'. 1. /bin/ed 2. /bin/nano <---- easiest 3. /usr/bin/vim.basic 4. /usr/bin/vim.tiny Choose 1-4 [2]: 2
After selecting an editor, you'll see the cron template with helpful comments explaining the format and providing examples.
Example − Scheduling a Backup Job
Here's how to schedule a backup script to run at specific times for the test1 user −
00 09,18 * * * /home/test1/backup_files.sh
Field Breakdown
| Field | Value | Meaning |
|---|---|---|
| Minute | 00 | At the 0th minute (start of hour) |
| Hour | 09,18 | At 9 AM and 6 PM |
| Day of Month | * | Every day of the month |
| Month | * | Every month |
| Day of Week | * | Every day of the week |
This cron job will execute the backup script /home/test1/backup_files.sh twice daily at 9:00 AM and 6:00 PM.
Common Cron Job Examples
| Schedule | Cron Expression | Description |
|---|---|---|
| Daily at midnight | 0 0 * * * | Every day at 12:00 AM |
| Weekly backup | 0 2 * * 0 | Every Sunday at 2:00 AM |
| Every 30 minutes | */30 * * * * | Runs every 30 minutes |
| Weekdays only | 0 8 * * 1-5 | Monday to Friday at 8:00 AM |
Key Points
Use
crontab -u username -eto edit cron jobs for a specific userUse
crontab -u username -lto list existing cron jobs for a userUse
crontab -u username -rto remove all cron jobs for a userEnsure the user has proper permissions to execute the scheduled commands
Output from cron jobs is typically sent via email unless redirected
Conclusion
Adding cron jobs to specific users allows for automated task execution with proper user context and permissions. This is particularly useful for user-specific backups, maintenance tasks, and scheduled data processing. Always test your cron expressions and ensure proper file permissions before deploying production cron jobs.
