Reputation: 2465
I am trying to run a cron script in python 3 so I had to setup a virtual environment (if there is an easier way, please let me know) and in order to run the script I need to be in the script's parent folder as it writes to text files there. So here is the long string of commands I have come up with and it works in console but does not work in cron (or I can't find the output..)
I can't type the 5 asterisks without it turning into bullet points.. but I have them in the cron tab.
cd usr/local/sbin/cronjobs && . virtualenv/secret_ciphers/bin/activate && cd csgostatsbot && python3 CSGO_STATS_BOT_TASK.py && deactivate
Upvotes: 0
Views: 1013
Reputation: 15296
It looks like you may have a stray .
in there that would likely cause an error in the command chain.
Try this:
cd usr/local/sbin/cronjobs && virtualenv/secret_ciphers/bin/activate && cd csgostatsbot && python3 CSGO_STATS_BOT_TASK.py && deactivate
Assuming that the virtualenv directory is in the cronjobs directory.
Also, you may want to skip the activate/deactivate, and simply run the python3 interpreter right out of the virtualenv. i.e.
/usr/local/sbin/cronjobs/virtualenv/secret_ciphers/bin/python3 /usr/local/sbin/cronjobs/csgostatsbot/CSGO_STATS_BOT_TASK.py
Edit in response to comments from OP:
The activate call is what activates the virtualenv. Not sure what the .
would do aside from cause shell command parsing issues.
Both examples involve the use of the virtualenv. You don't need to explicitly call activate. As long as you invoke the interpreter out of the virtualenv's directory, you're using the virtualenv. activate is essentially a convenience method that tweaks your PATH to make python3 and other bin files refer to the virtualenv's directory instead of the system install.
2nd Edit in response to add'l comment from OP:
You should redirect stderr, i.e.:
/usr/local/sbin/cronjobs/virtualenv/secret_ciphers/bin/python3 /usr/local/sbin/cronjobs/csgostatsbot/CSGO_STATS_BOT_TASK.py > /tmp/botlog.log 2>&1
And see if that yields any additional info.
Also, 5 asterisks in cron will run the script every minute 24/7/365. Is that really what you want?
3rd Edit in response to add'l comment from OP:
If you want it to always be running, I'm not sure you really want to use cron. Even with 5 asterisks, it will run it once per minute. That means it's not always running. It runs once per minute, and if it takes longer than a minute to run, you could get multiple copies running (which may or may not be an issue, depending on your code), and if it runs really quickly, say in a couple seconds, you'll have the rest of the minute to wait before it runs again.
It sounds like you want the script to essentially be a daemon. That is, just run the main script in a while (True)
loop, and then just launch it once. Then you can quit it via <crtl>+c
, else it just perpetually runs.
Upvotes: 1
Reputation: 168616
Try these commands. Hopefully you'll end up with a simpler, more understandable arrangement:
$ sudo apt-get install python3 # Just in case you haven't already
$ sudo apt-get install python3-pip
$ sudo pip3 install praw
$ vi CSGO_STATS_BOT_TASK.py
«Ensure that "#!/usr/bin/env python3" is the first line»
$ chmod +x CSGO_STATS_BOT_TASK.py
$ crontab -e
* * * * * /path/to/CSGO_STATS_BOT_TASK.py
Upvotes: 1