⏰ Command: crontab (The Job Scheduler)
The crontab (Cron Table) is a system-wide service used to schedule commands or scripts to run automatically at specific intervals—minutes, hours, days, or months.
1. The "Why"
For a developer and student, crontab is the key to "set it and forget it" automation.
- Database Backups: Automatically export your Supabase data or Preader database every night at 2:00 AM.
- System Maintenance: Run
sudo updatedbfor yourlocatecommand every morning so your file search is always fresh. - Content Generation: If you are writing , you could schedule a script to check for new tech news or compile your LaTeX drafts into PDFs every Sunday.
- Quantum Simulations: Schedule heavy Quantum Computing calculations to run during the night when you aren't using your PC.
2. The Syntax (The "Cron Expression")
A crontab entry consists of six fields. The first five define the time, and the sixth is the command.
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of the Week (0-6) (Sunday=0)
| | | +------- Month (1-12)
| | +--------- Day of the Month (1-31)
| | +----------- Hour (0-23)
| +------------- Minute (0-59)
3. Basic Commands
| Command | Action |
|---|---|
crontab -e |
Edit your crontab file (opens your default editor, like VS Code or Vim). |
crontab -l |
List your current scheduled jobs. |
crontab -r |
Remove all your scheduled jobs (be careful!). |
crontab -i |
Prompt before deleting with -r. |
4. Practical Examples for Your Workflow
A. Run a script every day at Midnight
0 0 * * * /home/ahmed/scripts/backup_projects.sh
B. Run a "updatedb" every 6 hours
0 */6 * * * sudo updatedb
C. Run a task only on weekdays (Monday-Friday) at 8:00 AM
0 8 * * 1-5 /home/ahmed/scripts/start_dev_env.sh
D. The "Reboot" Trick
If you want a script to run every time you turn on your Arch Linux system:
@reboot /home/ahmed/scripts/init_hyprland_settings.sh
5. Common Pitfalls & Solutions
- The PATH Problem: Cron runs in a very minimal environment. It might not know where
javaorpythonis. Always use absolute paths for commands and files (e.g.,/usr/bin/python3instead of justpython3). - Logging Output: Since there is no terminal attached to a cron job, you won't see "print" statements or errors. Redirect output to a log file:
* * * * * /path/to/script.sh >> /home/ahmed/cron.log 2>&1 - Permissions: Ensure the script you are trying to run is executable:
chmod +x script.sh.
6. Pro-Tips
- Cron.help: Websites like Crontab.guru are excellent for verifying your timing expressions before you save them.
- Arch Linux Context: By default, Arch doesn't come with a cron daemon installed. You likely need to install
cronieand enable it:sudo pacman -S cronie && sudo systemctl enable --now cronie.service - Systemd Timers: On modern Linux, many people are moving from
crontabto systemd timers. They provide better logging (viajournalctl) and can wait for the network to be online before running.
7. Summary Reference
| Goal | Expression |
|---|---|
| Every Minute | * * * * * |
| Every Hour | 0 * * * * |
| Every Sunday at Noon | 0 12 * * 0 |
| 1st of every month | 0 0 1 * * |