Crontab fails but command works in terminal

67 views Asked by At

I'm attempting to schedule an R script that does a scrape, some calculations and then emails a small group of people twice a day. I've gotten the script working well but I can't seem to get the crontab to work. I've given full disk access to cron, I've put the script in a folder where I've expanded the access as much as possible and I've put the execution of the R script into a shell script. Everything has as much permissions as I can give it. When I run the command in terminal it works fine but it doesn't work in cron. Can anyone help shed some light on why this isn't working?

The best I've been able to do is see R pop up in the activity monitor and then go away, so I'm assuming there's some issue with the script.

Current crontab:

0 * * * * /bin/bash /Users/billbachrach/cron_jobs/email_tests.sh

This works in terminal:

/bin/bash /Users/billbachrach/cron_jobs/email_tests.sh

Shell file:

#!/bin/sh
/usr/local/bin/Rscript /Users/billbachrach/cron_jobs/Craigslist_Good_Deal_Full_Script_V001.R

I'd share the R script but it's fairly complex. It runs on its own flawlessly and from terminal as well.

3

There are 3 answers

1
Jan-Peter Rühmann On

in which crontab you have entered it? Your own or the systemwide (/etc/crontab)? The system wide runs as root, as it´s entries by default run as user. therefore all entries of the script need permissions or must run with sudo. Try your crontab by using crontab -e.

0
Spacedman On

Probably impossible to give a correct answer without more knowledge of the script and your system, so only general hints might be useful here:

  • start with a minimal R script (a one liner) and confirm it works. By "works" I mean do something in the script you can check happened. Print a message to a log file, make a file in /tmp or something.
  • make the script more complex until it fails. The last thing you did was the problem
  • get the script to produce some output and make sure you can see it after it runs from CRON. Then you have a reliable method for debugging output and you can find out where the script failed.
  • check system logs for messages from CRON - on my system these are in /var/log/syslog but this may vary.
0
milkbro On

Are you sure this script hasn't been run once? According to your cron rules, it will run in the zeroth minute of every hour, not every minute. You can try to redirect an output (similar to echo xxx) to a specific file in a script to test whether the script is running.