6

I'm trying to set up a cron job under my user. I run crontab -e, make my edits, and try to save and exit. I receive the following error message /var/spool/cron/: mkstemp: Permission denied.

Relevant output from ls -al /var/spool/cron/crontabs

drwxr-xr-x 2 root crontab 4096 Nov  4 10:09 .
drwxr-xr-x 5 root root    4096 Nov 19  2014 ..
-rw-rw-rw- 1 greg crontab   91 Nov  4 11:04 greg
-rw------- 1 root crontab 1231 Oct 29 16:18 root

I can directly edit the greg file and save that but I still can't seem to get the job to run, even if I restart cron after updating it. What do I need to do to fix this problem?

The output from ls -lha $(which crontab) is:

-rwxr-sr-x 1 root crontab 36K Feb  8  2013 /usr/bin/crontab

The output from groups greg is:

greg : greg adm sudo crontab lpadmin sambashare
19
  • What does ls -lha $(which crontab) say? crontab should be setuid root becaue it will create temporary files in /var/spool/cron. Commented Nov 4, 2015 at 19:39
  • Does user greg belong to group cron?
    – eyoung100
    Commented Nov 4, 2015 at 19:39
  • @eyoung100 Yes, greg is part of the crontab group. I'm assuming thats what you meant? Commented Nov 4, 2015 at 19:49
  • Have you logged out and back in since you added your user?
    – eyoung100
    Commented Nov 4, 2015 at 19:50
  • @UlrichSchwarz I think have posted the output of that ls command to the question. I think it is set to root. Commented Nov 4, 2015 at 19:50

3 Answers 3

13

This will fix your immediate problem:

chmod u=rwx,g=wx,o=t /var/spool/cron/crontabs

But, if you can download packages, a more robust way to fix this is to use apt-get to reinstall the appropriate package:

root@ubuntu# dpkg-query -S /var/spool/cron/crontabs
cron: /var/spool/cron/crontabs
root@ubuntu# apt-get install --reinstall cron

after first making sure any local changes you've made to /etc/init/cron.conf, /etc/default/cron, etc. are copied somewhere and then reapplied.

4

The solution mentioned by Mark Plotnick didn't work for me, however after adding my user to the crontab group it worked:

sudo adduser myname crontab

You'll need to logout and login again for this change to be applied.

0

In Ubuntu 22.04:

crontab ~/admin/scripts/crontab.txt

/var/spool/cron/: mkstemp: Permission denied

$ ls -lha $(which crontab)

-rwxr-sr-x 1 root crontab 39K Mar 23 2022 /usr/bin/crontab

~$ ls -lha /var/spool/cron/crontabs

ls: cannot open directory '/var/spool/cron/crontabs': Permission denied

~$ sudo ls -lha /var/spool/cron/crontabs

total 8.0K

drwx-wx--T 2 root systemd-journal 4.0K Mar 2 2023 .

drwxr-xr-x 3 root root 4.0K Jul 22 09:48 ..

So we need 2 groups for user executing in order to get privileges:

sudo usermod -a -G systemd-journal <user>
sudo usermod -a -G systemd-journal <user>

Logoff and logon again with user ...

crontab ~/admin/scripts/crontab.txt

crontab -l

0 */1 * * * ~/admin/scripts/elasticsearch_flush.sh > /dev/null 2>&1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .