The crontab -e command applies changes immediately upon saving the file in your text editor. If modifications appear to be ignored, the issue is rarely with the crontab syntax itself but rather with the environment in which the cron daemon operates or a misunderstanding of how the scheduler executes jobs. Common culprits include incorrect absolute paths, missing environment variables, and log file permission errors.
Understanding How Crontab Saves Changes
When you invokecrontab -e, the system creates a temporary copy of your current crontab file, opens it in your default editor (usually vi or nano), and waits for you to save and exit. Upon exiting the editor successfully, the cron daemon (cron or crond) automatically reads the updated file into memory. There is no separate "apply" button or command required after editing. The change is instantaneous.
If you believe the changes were not applied, verify that you actually saved the file and exited the editor cleanly. In vi, this means typing :wq. In nano, this means pressing Ctrl+O then Enter, followed by Ctrl+X. If the editor crashes or hangs, the new crontab may never have been written to disk. You can check the integrity of your crontab by running crontab -l to list the current contents. If your new entries appear here, they are definitely installed.
Common Causes for Apparent Failures
The most frequent reason users think their crontab edits failed is that the job runs but produces no visible output or fails silently. Cron jobs do not inherit the interactive shell environment. This means variables like$PATH, $HOME, and user-specific aliases are often unavailable unless explicitly defined within the crontab file or the script itself.
For example, if your script calls python instead of /usr/bin/python3, it will fail because /usr/bin might not be in the cron environment's $PATH. Always use absolute paths for both the executable and any files referenced by the job. Additionally, ensure that the script has executable permissions. A common mistake is creating a script but forgetting to run chmod +x script.sh. Without execute permissions, the cron daemon cannot run the file, resulting in a silent failure.
Another critical factor is the working directory. When cron starts a job, its current working directory is usually the user's home directory. If your script expects to be run from a specific project folder, it will fail unless you specify the path relative to the home directory or change directories within the command string using cd /path/to/dir && ./script.sh.
Diagnosing Silent Failures with Logs
To determine if a job is actually running and failing, or if it is not being scheduled at all, you must inspect the system logs. On most Linux distributions, cron activity is logged via syslog. You can view these logs in real-time by runningtail -f /var/log/syslog or journalctl -u cron.service -f depending on your init system. Look for lines containing "CRON" and your username.
If you see messages indicating that the job was executed, check the standard error output. By default, cron sends email notifications for stdout and stderr if the MAILTO variable is set. However, many modern systems disable mail delivery by default. A more reliable method is to redirect output to a log file within the crontab entry itself. For instance:
* * * * * /path/to/script.sh >> /tmp/cron.log 2>&1
This appends both standard output and standard error to /tmp/cron.log. Check this file after the scheduled time to see any error messages. If the file is empty, the job likely did not run. If it contains errors, they will guide you to the fix, such as missing dependencies or permission denied errors.
Verifying Schedule Syntax
Ensure that your schedule syntax is correct. The five fields represent minute, hour, day of month, month, and day of week. A common typo is placing a number outside the valid range, such as setting the minute field to 60. Also, remember that cron uses 24-hour format. If you want a job to run every minute, use* * * * *. If you suspect the timing is wrong, test with a very short interval, such as * * * * * (every minute) for a few minutes, then revert to the desired schedule.
If you are managing multiple users or complex setups, consider checking if the user is allowed to run cron jobs. While rare on modern systems, some configurations restrict cron access via /etc/cron.allow or /etc/cron.deny. If the user is not in the allow list and is in the deny list, or if neither exists but security policies are strict, the job may be silently dropped.
For more advanced automation needs, such as transferring files between systems, you might find related techniques useful in our guide on Automating File Transfers between Linux Server and Windows Machine using SSH and Expect Script in Crontab.
Frequently Asked Questions
Why does my crontab job work manually but not via cron?
It almost always works due to environmental differences. Manual execution uses your interactive shell profile, while cron uses a minimal environment. Use absolute paths for all commands and scripts, and define necessary variables directly in the crontab line or script header.How can I see what errors my cron job is producing?
Redirect both standard output and standard error to a log file in your crontab entry using> /path/to/logfile 2>&1. Then monitor that file or check /var/log/syslog for CRON-related entries to identify specific failure reasons.
Does restarting the cron service apply changes?
No, the cron daemon reads the crontab file only when it is saved viacrontab -e or when the daemon starts. Restarting the service is unnecessary for applying edits and should only be done if the daemon itself is unresponsive or stuck.
What if I need to run a job as a different user?
You cannot switch users within a standard user crontab. Instead, edit the root crontab usingsudo crontab -e and specify the username before the command, like username /path/to/script.sh. Be cautious with root-level tasks and ensure proper security practices.