使用crontab -e编辑定时任务时出现写入错误,如何解决?
This error usually pops up when crontab can't write its temporary file to the /var/spool/cron directory—most often due to permissions, leftover temp files, or disk space issues. Here's how to fix it step by step:
1. Check and Correct Permissions on /var/spool/cron
The /var/spool/cron directory needs specific permissions to let crontab work properly. First, check the current setup:
ls -ld /var/spool/cron
The output should look something like this (varies slightly by distro):
drwx-wx--T 2 root crontab 4096 Aug 10 12:34 /var/spool/cron
If permissions or ownership are off, fix them with these commands:
sudo chown root:crontab /var/spool/cron sudo chmod 1730 /var/spool/cron
The 1730 permission sets the directory to be writable by root and the crontab group, with the sticky bit enabled to prevent users from deleting each other's crontabs.
2. Clean Up Leftover Temporary Files
Sometimes a failed crontab edit leaves behind a temporary file that blocks subsequent writes. Delete any leftover temp files in the directory:
sudo rm -f /var/spool/cron/#tmp.*
This removes all files starting with #tmp. in the cron spool directory.
3. Check for Disk Space Issues
If the partition containing /var/spool/cron is full, crontab can't write the temp file. Check disk usage with:
df -h /var/spool/cron
If the used percentage hits 100%, free up space by deleting unnecessary files, expanding the partition, or moving data to another drive.
4. Verify User's Crontab Access
Less likely, but worth checking if the above steps don't work:
- If
/etc/cron.allowexists, your username must be listed in it. - If
/etc/cron.denyexists, your username must NOT be listed in it.
Edit these files with sudo if needed (note: most systems don't have these files by default, so this only applies if they've been manually created).
After trying each step, run crontab -e again to see if the error is resolved.
内容的提问来源于stack exchange,提问作者sampath kv

