-1

Crontab script running from sh-3.2 (logged in as root) is:

* * * * * python /Users/tony/Documents/Python_Projects/Stock_Earnings_5.py #send an email every minute

Stock_Earnings_5 sends an email based on the logic.

Currently Stock_Earnings_5 works independently and sends out the email when manually running it from shell sh-3.2 (logged in as root).

crontab also works for "hello world" to terminal.

So in summary, crontab works for hello world program to terminal, and Stock_Earnings_5 works if called independently of crontab, but when crontab calls Stock_Earnings_5 it doesn't send email.

martineau
  • 119,623
  • 25
  • 170
  • 301
Tony D
  • 45
  • 6
  • This doesn't contain enough details to decide which FAQ it's a duplicate of; voting to close as incomplete. For starters, the syntax in `root`'s crontab is different (there is a sixth field which specifies which user to run as) but it's also plausible that your mail is not properly configured. – tripleee Jul 12 '20 at 16:19
  • Possible duplicate of https://stackoverflow.com/questions/22743548/cronjob-not-running which at least tells you what to check before you [edit] to provide more details. Probably you'll figure out what's wrong just by following that. – tripleee Jul 12 '20 at 16:20
  • Keep in mind that the environment in which cron runs has different variables that your user environment. Therefore, it might be (and usually is) necessary to provide the explicit path to your `python` executable. – S3DEV Jul 12 '20 at 16:22
  • @S3DEV, I don't understand what you mean. I have the path where the python script is saved. If I do python Stock_Earnings_5 from my current shell prompt it works just fine. Can you provide an example of what the right cron command would be? Thanks. – Tony D Jul 12 '20 at 16:31
  • @tripleee, went to your link. Still don't know what's wrong. How would I force the path to be correct if it's wrong? – Tony D Jul 12 '20 at 16:57
  • 2
    I think @S3DEV meant you need to change the replace the `python` in the `python /Users/tony/Documents/Python_Projects/Stock_Earnings_5.py...` line with the path to your system's python executable. – martineau Jul 12 '20 at 17:00
  • @martineau, how do I find where the path to my system's python executable is? Thanks. – Tony D Jul 12 '20 at 17:02
  • Not sure…maybe by typing the command `where python`. – martineau Jul 12 '20 at 17:07
  • 1
    @TonyD - `which python` - This will show the path to the default Python executable, which is loaded when you type `python` into the terminal. – S3DEV Jul 12 '20 at 17:09
  • 1
    @S3DEV, I did which python and modified the crontab script to : * * * * * /Users/tony/anaconda3/bin/python /Users/tony/Documents/Python_Projects/Stock_Earnings_5.py AND it WORKED!!! Thanks!!!! – Tony D Jul 12 '20 at 17:39
  • Excellent mate, glad to hear it. I’ll post as a proper answer if you’d like to accept the answer. – S3DEV Jul 12 '20 at 17:51
  • Duplicate of https://stackoverflow.com/questions/34141380/cronjob-command-not-found – tripleee Jul 12 '20 at 18:08
  • The first proposed duplicate does tell you a lot about how you would troubleshoot this. For a start, examine the system log files. If they are inconclusive, look in your inbox for an email message with an error message from `cron`, or redirect its standard error to a place it has write access to (append, don't overwrite; and of course, make sure you have read access to the generated output). – tripleee Jul 12 '20 at 18:10
  • See [Running a bash script from a cronjob not working](https://stackoverflow.com/q/45743633/850848). – Martin Prikryl Feb 18 '21 at 11:38

1 Answers1

2

To properly post my comment above:

Keep in mind that the environment in which cron runs has different (minimal) variables that your user environment. Therefore, it might be (and usually is) necessary to provide the explicit path to your python executable.

The path to your default python executable can be found using the which python command.

Example:

$ which python
/home/username/anaconda3/bin/python

Then, in cron use:

* * * * * /home/username/anaconda3/bin/python /full/path/to/myscript.py
S3DEV
  • 8,768
  • 3
  • 31
  • 42