7

I have a deployment script and in it I'm trying to add a cron job. This is the code I am using which I found on stackoverflow. I'm running debian 8.

# ADD CRON
crontab -l > mycron
echo "10 * * * * cd /var/www/test/ && ./test" >> mycron
crontab mycron
rm mycron

When I run these commands I get the reply: no crontab for root

What am I doing wrong here, and how can I get it so I can add this cron job using a bash script? Thank you.

Jimmy
  • 239
  • 3
  • 7
  • 21
  • Check if you have `root` in `/etc/cron.deny`. – ott-- Apr 30 '15 at 20:50
  • @ott-- I don't have that file at all, just ```cron.d cron.daily cron.hourly cron.monthly crontab cron.weekly``` – Jimmy Apr 30 '15 at 21:00
  • Hate to ask a dumb question, but are you root when you execute these commands? Also instead of the raw commands please provide the transcript of their run. – mdpc Apr 30 '15 at 21:09
  • @mdpc I am root yes. I will upload the full script when I get to my laptop but it's just that command with a ```#!/bin/bash``` header – Jimmy Apr 30 '15 at 21:13
  • @JennyD my reply in the answer below solved it which doesn't appear on you link – Jimmy May 02 '15 at 19:35
  • Typically when scripting and packaging (and running as root) dropping a new file with a cron entry in `/etc/cron.d` is easier to maintain. – HBruijn May 04 '15 at 04:34

1 Answers1

9

Sounds like (a) root's crontab is initially empty and (b) the -e option is set in the shell.

If the user's crontab file is empty then crontab -l exits with status 1.

If shell is running with -e option then it will exit immediately on failure (defined as exiting with non-zero status).

Look for errexit in the output of echo $SHELLOPTS to check for this setting. Turn it off in the current shell with set +e.

Paul Haldane
  • 4,457
  • 1
  • 20
  • 31
  • The problem was there was no file automatically created in /var/spool/crontab/root so I made a blank one in my script and then the code above worked fine. – Jimmy May 01 '15 at 06:26
  • echo $SHELLOPTS -> braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor – Dr.jacky Mar 10 '17 at 10:05
  • 1
    Just in case : Ubuntu stores root contab under `/var/spool/cron/crontabs/root` – Cédric Françoys Dec 07 '18 at 14:48