0

So i have a file success.sh

 python3 /home/ubuntu/foo.py

and this runs in Cron with:

* * * * * /home/ubuntu/success.sh >> /tmp/cron_output

where foo.py is simply

 print("DSVSDVDSVSDFDS")

however as soon as I try to run what i actually want to run and change success.sh to the following:

   cd "/home/ubuntu/Amazon to Ebay v1.7/Catalogue 2/" && python3 "/home/ubuntu/Amazon to Ebay v1.7/Catalogue 2/ebay-price-arbitrage-bot.py"

No output is being recorded in the file...

I'm wondering if it is acutally running or not. Why isn't anything being outputted?? success.sh runs fine as a standalone...

EDIT:

I've changed my crontab to the following:

  * * * * * /home/ubuntu/success.sh > /tmp/cron_output 2>&1

So it outputs errors. Now its showing that a module is not found in my py script.

    Traceback (most recent call last):
    File "/home/ubuntu/Amazon to Ebay v1.7/Catalogue 2/ebay-price-    arbitrage-bot.py", line 23, in <module>
    from paypal import PayPalInterface
    ModuleNotFoundError: No module named 'paypal'

but it works fine as a standalone, so i dont know whats going on.

kianwhatt
  • 13
  • 3
  • it is only using absolute paths, as indicated by the above code. – kianwhatt Mar 03 '20 at 19:29
  • your right, that is a typo. correcting now. – kianwhatt Mar 03 '20 at 19:35
  • Please post the full text of the error. – stark Mar 03 '20 at 19:37
  • @stark done. // – kianwhatt Mar 03 '20 at 19:45
  • When using `import`, python looks for modules in `sys.path` (`import sys` to get at `sys.path`). If you can find the module in one environment (e.g., command line) and cannot find it in another environment (e.g., a cron job), then `sys.path` is likely different in the two environments. See also the `PYTHONPATH` environment variable. Cron jobs typically have a very minimal environment (`printenv` and the man pages will help). – Juan Mar 03 '20 at 20:50
  • My guess is you have PYTHONPATH in your environment, and/or you are running as a different user (you did not say what user the cron job is running as), and/or your shell initialization configuration (.profile, etc.) is not run by the cron job. You should remove the 'ubuntu' tag - this is not specific to ubuntu (and may not be appropriate for stackoverflow, vs. unix.stackexchange.com for instance). – Juan Mar 03 '20 at 20:50

2 Answers2

0

Since you are mentioning the full path for python file, you dont need to do cd to that path.

Also, can you check if the path is correct? Because, && operator works such that the python3 command is executed only when the cd command is a success. So, if the path was incorrect, the cd would have failed and the python3 was never executed.

Alternatively, you can use ';' instead of '&&' which guarentees the execution of python3 irrespective of the outcome of the 'cd' command.

Karthik TU
  • 151
  • 1
  • 8
  • i've tried as you suggested and got rid of cd.....i also added redirection for any errors in my crontab....and now the output is showing that a module is not being imported in my py script. but it runs fine as a standalone! – kianwhatt Mar 03 '20 at 19:30
  • Can you try providing the absolute path for python3 executable? – Karthik TU Mar 03 '20 at 20:14
  • Looks like CRON environment is not able to find the python3 executable. – Karthik TU Mar 03 '20 at 20:15
0

Python looks for modules in the current directory and in the standard module path. When you run from cron, the process is not in your home directory so the path needs to be added. In the python file do:

import sys
sys.path.insert(0, "/path/to/your/paypal/module")
stark
  • 12,615
  • 3
  • 33
  • 50