Add cronjob with bash script - No crontab for root [duplicate]

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.


Solution 1:

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.