I'm trying to get cron to run this command every 10 minutes;
(In /home/pi/myst-myst/ DIR)
python myst.py `./monitor.sh`
I've tried pretty much everything to get it to work but cron won't execute it properly. Here is what I have at the moment;
*/1 * * * * /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
Any help would be much appreciated.
Is there an alternative to crontab I could use? Could I use a bash script to execute python and then use a cron for that bash script?
I've had problems calling both python and perl directly from cron. For perl it boiled down to LIBPATH defaulting to something insufficient.
I'd suggest wrapping your commands in a shell script and adding "set -x" to trace through the problem
#!/bin/sh
set -x
export PYTHONPATH=/my/python/modules:$PYTHONPATH
/usr/bin/python /home/pi/myst-myst/myst.py $(/home/pi/myst-myst/monitor.sh)
Call it directly to make sure it works, and then try calling via cron. Make sure to redirect both stdout and stderr to capture any error messages
*/10 * * * * /home/pi/myscript.sh > /home/pi/stdout 2> /home/pi/stderr
You could do something like
*/10 * * * * cd /home/pi/myst-myst/;/usr/bin/python /home/pi/myst-myst/myst.py $(./monitor.sh)
to change working directory before running the command.
Edit: replaced backticks
Does your script rely on any environment variables, such as PYTHONPATH?
If so, the environment will be missing when invoked by cron.
You can try:
*/1 * * * * PYTHONPATH=/my/python/modules/ /usr/bin/python /home/pi/myst-myst/myst.py `./monitor.sh`
Try this way:
*/1 * * * * /home/pi/myst-myst/myst.py `./monitor.sh`
And add the following in myst.py
#!/usr/bin/env python
Related
I have a python script that finds my grades in a webpage and sends me an email if any of my grades have been changed. Im now trying to run this script every hour with crontab.
The script works when running through terminal:
python3 /home/pi/Desktop/grades/script.py
In my script, I am trying to run crontab with * * * * * python3 /home/pi/Desktop/grades/script.py
I also added the shell to use: SHELL=/bin/bash
When the script is ran through the IDLE, it cannot find the crontab directory to schedule the scipt. I have the PATH variable set, and I searched about this and nothing has worked.
This could be couple of ways to handle this.
1. You must specify full path of python
* * * * * '/usr/bin/python3 /home/pi/Desktop/grades/script.py'
2. You should put shebang line in your script, ref examples.
#!/usr/bin/env python3 or #!/usr/bin/python3.
* * * * * /home/pi/Desktop/grades/script.py
Tried to follow the instructions from this question but it didn't work with me. As background info I can tell you that there's an instance of Python 2.7 installed on Mac by default and my instance is actually python 3.6, hence the Python3. I'm doing something wrong but I can't figure out what. I know that as is it will run every minute, I just want to make it work first and then change the time.
* * * * * cd /User/Users/cannopy/PycharmProjects/untitled python3 /User/Users/cannopy/PycharmProjects/untitled/test.py
EDIT:
After changing some things I managed to make it work with this:
* * * * * cd /Users/cannopy/PycharmProjects/untitled/ && ~/PycharmProjects/untitled/venv/bin/python test.py
Try,
* * * * * cd /User/Users/cannopy/PycharmProjects/untitled && python3 test.py >> cron_result.txt
A crontab entry can only contain a single line. You can combine commands with ; or && but ideally your Python script should not require to run in a particular directory.
If the script requires imports or data files from the directory where it is installed, maybe have it examine sys.argv[0] to discover its location.
I have a simple Python script that I am trying to setup as a cron job, but it refuses to run. It does run when I run it by itself calling it as:
python script.py
I have tried setting my evironment variables in the crontab, but I cant get it to work. My crontab looks like this:
# For more information see the manual pages of crontab(5) and cron(8)
# m h dom mon dow command
SHELL=/bin/bash
PATH=/home/netadmin/bin:/home/net/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/b$
*/2 * * * * PYTHONPATH=/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
Any Ideas on this?
You are mixing up two unrelated concepts:
the Python interpreter location, which is the path to the Python interpreter program (an executable file somewhere)
and the PYTHONPATH, which is a string indicating search locations (directories) for Python libraries. It is not the location of the Python interpreter, but rater a :-separated list of directories. If you don't know what it's useful for, don't use it!
If doing python script.py works, there is generally no need to tweak the PYTHONPATH. You can obtain the full path to the Python interpreter with which:
$ which python
/usr/bin/python
This will print the absolute path to the Python interpreter that you can use in your crontab:
*/2 * * * * /usr/bin/python /path/to/script.py >>/home/net/out.txt 2>&1
Don't tweak PYTHONPATH if you don't need to. If script.py relies on libraries that are not installed on the system, I encourage you to learn & use virtualenvs. It's easy and solves most Python library dependency issues.
You can create a shell script (we'll call it foo.sh for this example) which would look like this:
#! /bin/bash
/user/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
You need to make foo.sh executable, so you will need to run the following to do that:
chmod +x /home/net/path-to-script/foo.sh
Finally, you can add the shell script to a cron job by running this (which you seem familiar with):
crontab -e
Add a line as follows:
*/2 * * * * /home/net/path-to-script/foo.sh
That should do it, good luck!
Why are you setting environment variable PYTHONPATH, you can run it directly also I believe path for python would be usr instead of user try this
*/2 * * * * cd /home/net/path-to-script ; /usr/bin/python script.py >>/home/net/out.txt 2>&1
This line is incorrect, remove PYTHONPATH
*/2 * * * * PYTHONPATH=/usr/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
=>
*/2 * * * * /usr/bin/python /home/net/path-to-script/script.py >>/home/net/out.txt 2>&1
It's recommended to use Shebang however.
I'm very new to Unix and crontab. The only major issue I'm running into is pointing terminal to the python modules for the specific program I'm trying to run. From command line the program runs fine but won't from crontab.
The first cronjob sends me an email saying that the cronjob is running. The second(createUpdate) runs a script I've built, set to run each minute.
crontab -l returns:
*/1 * * * * python /Users/JohnDoe/Desktop/emailalert.py
*/1 * * * * PYTHONPATH =/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages; export PYTHONPATH; python /Users/JohnDoe/Desktop/createUpdate.py
Am I structuring PYTHONPATH correctly?
Should I break it out before the cron?
Is 'export PYTHONPATH' necessary?
EDIT
I forgot to add the error
/bin/sh: PYTHONPATH: command not found
Traceback (most recent call last):
File "/Users/JohnDoe/Desktop/createUpdate.py", line 1, in <module>
import beatbox
ImportError: No module named beatbox
The right way would be
*/1 * * * * PYTHONPATH=/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages python /Users/JohnDoe/Desktop/createUpdate.py
Please be aware of spaces in variable assignment. No semicolon and no need to export variables, since declaring them before the commands already makes them active for the command itself.
Use
*/1 * * * * PYTHONPATH=/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages python /Users/JohnDoe/Desktop/createUpdate.py
in your crontab. No semi-colons, PYTHONPATH only applies to that instance of python and your script.
(Further, I think you got the
/bin/sh: PYTHONPATH: command not found
error because you have a space between PYTHONPATH and the equal sign.)
You don't need to export PYTHONPATH. If you installed beatbox globally, you should be able to include the environment setting on the same line(in the same command) as the call to Python, like this:
*/1 * * * * PYTHONPATH=/Library/Frameworks/Python.framework/Versions/Current/lib/python2.7/site-packages python /Users/JohnDoe/Desktop/createUpdate.py
Also, make sure there aren't any spaces between the variable name, the equal sign and the value of the variable.
You should really be using virtualenv to create a sandboxed Python, so you don't have to install external libraries globally. If you install virtualenv and install your dependencies into it, you will be able to do something like this
*/1 * * * * /path/to/virtual/env/bin/python /Users/JohnDoe/Desktop/createUpdate.py
and not worry about global dependencies.
Here are the docs for virtualenv: http://www.virtualenv.org/en/latest/
*/1 * * * * pip install beatbox && python /Users/JohnDoe/Desktop/createUpdate.py
had a hard time to find a working solution and end up with this.
what happens to my script in python that does not run through crontab every minute.
My script has execute permissions and then calls two other scripts in python.
This is the content of my crontab (#crontab -l):
*/1 * * * * /usr/bin/rsm/samplesMonitor.py
Thank you guys.
Check /var/log/syslog for errors.
DIAGNOSTICS
cron requires that each entry in a crontab end in a
newline character. If the last entry in a crontab is
missing a newline (ie, terminated by EOF), cron will
consider the crontab (at least partially) broken. A
warning will be written to syslog.
Update: According to your log message, the script is running but returning an error code. Cron will email you the output, if you have a mail agent installed.
Try either:
install a mail agent, such as: apt-get install exim4
change your cron line to log to file, like so:
* * * * * /usr/bin/rsm/samplesMonitor.py 2>&1 >> /tmp/script.log
Update 2: I re-read your question and it acurred to me that maybe you are running into python import issues. You say that your script calls two other scripts. My suggestion would be to test running your script from /. Python has a default behavior to find imports in the current working directory, so make sure your script can run from any path location.
In the crontab, you can set the starting working directory by calling your script from within another shell process. For example:
bash -c "cd THE_WORKING_DIR;/usr/bin/rsm/samplesMonitor.py"
I believe it should be */1, not *\1.
It should be */1 instead of *\1 (forward slash instead of backslash). Also, make sure the path is correct; there usually are no subdirectories under /usr/bin.
If you want it to run every minute, just do this
* * * * * /usr/bin/rsm/samplesMonitor.py