🐧 Crontab Command Guide (Linux Scheduled Tasks)

🐧 Crontab Command Guide (Linux Scheduled Tasks)
Operating System(s)

The crontab command is used to schedule tasks (cron jobs) to run automatically at specific times or intervals. It’s part of the standard Linux scheduling system powered by the cron daemon.

📌 What is Crontab?

A crontab (cron table) is a file that contains scheduled commands. Each user can have their own crontab, allowing automation like:

  • Running backups
  • Updating systems
  • Executing scripts

▶️ Basic Crontab Commands

Edit your crontab

 

crontab -e

 

List current cron jobs

 

crontab -l

 

Remove all cron jobs

 

crontab -r

 

Edit another user’s crontab (root only)

 

sudo crontab -u username -e

 

⏱️ Crontab Syntax Explained

Each cron job follows this format:

 

* * * * * command
│ │ │ │ │
│ │ │ │ └── Day of week (0–7) (Sun = 0 or 7)
│ │ │ └──── Month (1–12)
│ │ └────── Day of month (1–31)
│ └──────── Hour (0–23)
└────────── Minute (0–59)

 

🧪 Common Examples

Run a script every day at 2:30 AM

 

30 2 * * * /home/user/script.sh

 

Run every 5 minutes

 

*/5 * * * * /home/user/script.sh

 

Run every Sunday at midnight

 

0 0 * * 0 /home/user/script.sh

 

Reboot system every day at 3 AM

 

0 3 * * * /sbin/reboot

 

📂 Log Output (Important)

By default, cron jobs don’t show output. Redirect logs like this:

 

0 2 * * * /home/user/script.sh >> /home/user/cron.log 2>&1

 

✔ Helps with debugging
✔ Captures errors

⚠️ Common Issues (and Fixes)

Script doesn’t run

  • Use full paths (cron doesn’t load your environment)
  • Example:

 

/usr/bin/python3 /home/user/script.py

 

Permission denied

 

chmod +x script.sh

 

Cron service not running

 

sudo systemctl status cron

 

Start it if needed:

 

sudo systemctl start cron

 

🔐 Crontab Locations (Advanced)

  • User crontabs:

    /var/spool/cron/crontabs/

     

  • System-wide cron:

    /etc/crontab

     

🧠 Pro Tips

  • Always test commands manually first
  • Use logging to track failures
  • Avoid running heavy jobs too frequently
  • Use crontab.guru (online tool) to validate syntax

🔄 When to Use Cron

  • Automated backups
  • Log rotation
  • Software updates
  • Monitoring scripts
Force Cache (Faster Mirror)

Linux Commands

Files

tar · pv · cat · tac · chmod · grep ·  diff · sed · ar · man · pushd · popd · fsck · testdisk · seq · fd · pandoc · cd · $PATH · awk · join · jq · fold · uniq · journalctl · tail · stat · ls · fstab · echo · less · chgrp · chown · rev · look · strings · type · rename · zip · unzip · mount · umount · install · fdisk · mkfs · rm · rmdir · rsync · df · gpg · vi · nano · mkdir · du · ln · patch · convert · rclone · shred · srm · scp · gzip · chattr · cut · find · umask · wc · tr · hash  · compgen

Processes

alias · screen · top · nice · renice · progress · strace · systemd · tmux · chsh · history · at · batch · free · which · dmesg · chfn · usermod · ps · chroot · xargs · tty · pinky · lsof · vmstat · timeout · wall · yes · kill · sleep · sudo · su · time · groupadd · usermod · groups · lshw · shutdown · reboot · halt · poweroff · passwd · lscpu · crontab · date · bg · fg · pidof · nohup · pmap

Networking

netstat · ping · traceroute · ip · ss · whois · fail2ban · bmon · dig · finger · nmap · ftp · curl · wget · who · whoami ·· iptables · ssh-keygen · ufw · arping · firewalld