{"id":801,"date":"2025-12-10T18:20:53","date_gmt":"2025-12-11T00:20:53","guid":{"rendered":"https:\/\/kop.lat\/blog\/?p=801"},"modified":"2025-12-10T18:20:54","modified_gmt":"2025-12-11T00:20:54","slug":"how-to-use-crontab-in-linux","status":"publish","type":"post","link":"https:\/\/kop.lat\/blog\/how-to-use-crontab-in-linux\/","title":{"rendered":"How to use crontab in Linux"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">1. Understand the basics<\/h3>\n\n\n\n<p><code>crontab<\/code> is the tool used to schedule recurring tasks (jobs) on Linux systems. Each user has their own crontab file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. List current cron jobs<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -l<\/code><\/pre>\n\n\n\n<ul>\n<li>If no jobs exist, it will show &#8220;no crontab for &#8221; or an empty output.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. Edit your crontab<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>crontab -e<\/code><\/pre>\n\n\n\n<ul>\n<li>The first time you run this, it will ask you to choose an editor (nano, vim, etc.).<\/li>\n\n\n\n<li>This opens your personal crontab file for editing.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. Crontab syntax (6 fields)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>MINUTE HOUR DOM MONTH DOW   COMMAND\n 0-59   0-23 1-31 1-12 0-7    \/path\/to\/command or script<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Field<\/th><th>Allowed values<\/th><th>Special characters<\/th><\/tr><\/thead><tbody><tr><td>Minute<\/td><td>0\u201359<\/td><td>, &#8211; * \/<\/td><\/tr><tr><td>Hour<\/td><td>0\u201323<\/td><td>, &#8211; * \/<\/td><\/tr><tr><td>Day of month<\/td><td>1\u201331<\/td><td>, &#8211; * \/ ? L W<\/td><\/tr><tr><td>Month<\/td><td>1\u201312 or JAN\u2013DEC<\/td><td>, &#8211; * \/<\/td><\/tr><tr><td>Day of week<\/td><td>0\u20137 (0 and 7 = Sun)<\/td><td>, &#8211; * \/ ? L #<\/td><\/tr><tr><td>Command<\/td><td>Full path required<\/td><td><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">5. Common examples<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Schedule<\/th><th>Crontab line<\/th><\/tr><\/thead><tbody><tr><td>Every minute<\/td><td><code>* * * * * \/path\/to\/script.sh<\/code><\/td><\/tr><tr><td>Every 5 minutes<\/td><td><code>*\/5 * * * * \/path\/to\/script.sh<\/code><\/td><\/tr><tr><td>Every day at 3:30 AM<\/td><td><code>30 3 * * * \/path\/to\/backup.sh<\/code><\/td><\/tr><tr><td>Every Monday at 6:00 AM<\/td><td><code>0 6 * * 1 \/path\/to\/weekly-report.sh<\/code><\/td><\/tr><tr><td>Every day at 00:00<\/td><td><code>0 0 * * * \/path\/to\/daily-task.sh<\/code><\/td><\/tr><tr><td>Twice a day (8 AM &amp; 8 PM)<\/td><td><code>0 8,20 * * * \/path\/to\/script.sh<\/code><\/td><\/tr><tr><td>First day of every month<\/td><td><code>0 2 1 * * \/path\/to\/monthly.sh<\/code><\/td><\/tr><tr><td>Reboot<\/td><td><code>@reboot \/path\/to\/startup-script.sh<\/code><\/td><\/tr><tr><td>Yearly<\/td><td><code>@yearly \/path\/to\/annual-task.sh<\/code> (same as <code>0 0 1 1 *<\/code>)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">6. Best practices<\/h3>\n\n\n\n<ul>\n<li>Always use <strong>absolute paths<\/strong> for commands and scripts:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  \/usr\/bin\/python3 \/home\/user\/myscript.py<\/code><\/pre>\n\n\n\n<ul>\n<li>Redirect output to avoid mail spam:<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  *\/10 * * * * \/home\/user\/backup.sh &gt;&gt; \/var\/log\/backup.log 2&gt;&amp;1<\/code><\/pre>\n\n\n\n<p>or silence completely:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  *\/10 * * * * \/home\/user\/backup.sh &gt; \/dev\/null 2&gt;&amp;1<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">7. System-wide cron jobs (requires root)<\/h3>\n\n\n\n<p>System cron files are located in:<\/p>\n\n\n\n<ul>\n<li><code>\/etc\/crontab<\/code><\/li>\n\n\n\n<li><code>\/etc\/cron.d\/<\/code><\/li>\n\n\n\n<li><code>\/etc\/cron.hourly\/<\/code>, <code>\/etc\/cron.daily\/<\/code>, <code>\/etc\/cron.weekly\/<\/code>, <code>\/etc\/cron.monthly\/<\/code><\/li>\n<\/ul>\n\n\n\n<p>Example <code>\/etc\/crontab<\/code> line:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>15 4 * * * root \/usr\/local\/bin\/system-maintenance.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">8. Useful commands summary<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Command<\/th><th>Purpose<\/th><\/tr><\/thead><tbody><tr><td><code>crontab -l<\/code><\/td><td>List your cron jobs<\/td><\/tr><tr><td><code>crontab -e<\/code><\/td><td>Edit your cron jobs<\/td><\/tr><tr><td><code>crontab -r<\/code><\/td><td>Remove all your cron jobs<\/td><\/tr><tr><td><code>sudo crontab -u user -e<\/code><\/td><td>Edit another user&#8217;s crontab (as root)<\/td><\/tr><tr><td><code>sudo systemctl status cron<\/code><\/td><td>Check if cron daemon is running<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">9. View cron logs (location varies by distro)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code># Ubuntu\/Debian\njournalctl -u cron -f\n\n# RHEL\/CentOS\/Fedora (older)\ngrep CRON \/var\/log\/syslog\n# or\ngrep CRON \/var\/log\/cron\n\n# Many systems\nsudo tail -f \/var\/log\/cron<\/code><\/pre>\n\n\n\n<p>The task will now run automatically at the specified schedule.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Understand the basics crontab is the tool used to schedule recurring tasks (jobs) on Linux systems. Each user has their own crontab file. 2. List current cron jobs 3. Edit your crontab 4. Crontab syntax (6 fields) Field Allowed values Special characters Minute 0\u201359 , &#8211; * \/ Hour 0\u201323 , &#8211; * \/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":227,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,40,111],"tags":[144,5],"_links":{"self":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/801"}],"collection":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/comments?post=801"}],"version-history":[{"count":1,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions"}],"predecessor-version":[{"id":802,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/posts\/801\/revisions\/802"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media\/227"}],"wp:attachment":[{"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/media?parent=801"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/categories?post=801"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kop.lat\/blog\/wp-json\/wp\/v2\/tags?post=801"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}