I'm trying to schedule a simple python script on MacOS using crontab. I have seen lot of guides and answers to other questions here in SO but still can't get my task to work. Actually I'm not so familiar with terminal and this kind of stuff but it didn't seem so difficult so I'm hoping to solve it.
I followed two different approaches.
First approach:
I use the command crontab -e on the terminal
In VIM I press I for Insert and paste the following:
* * * * * /usr/bin/python mypath/ap.py
Press "esc" and ":qw" to close VIM window.
crontab -l shows my cron entry as expected, BUT nothing happens from the script (it is supposed to create a txt file).
Second approach:
I paste this line in the terminal:
* * * * * /usr/bin/python mypath/ap.py >> mypath/MyCronLog.txt 2>&1
In this way I can see a txt file where I can read the log and eventual errors. MyCronLog.txt shows: -bash: Applications: command not found or -bash: ap.py: command not found.
My python script is located in a folder on my dekstop (I don't know if it matters).
Moreover I run my python script from terminal and it works perfectly.
Any suggestion is appreciated!
EDIT 1:
The path of my python script is the following:
/Users/myname/Desktop/ap/ap.py
EDIT 2:
"/opt/anaconda3/bin/python" in place of "/usr/bin/python" but nothing has changed.
The problem seems to be in your python script. I guess the txt-file is created but in the wrong folder. When cron calls a python script, the working directory is NOT the directory of that script (see here). Look here for a workaround.
EDIT:
The behavior of your script might depend on the directory from which you call it (not an ideal way to write scripts). When you are in /Users/myname/Desktop/ap and call python ap.py, this might lead to something different than calling python ap/ap.py from /Users/myname/Desktop
You can verify this by calling this short script:
import os
print(os.getcwd())
I can imagine that your script crashes when called by cron while it works if you call it from the containing directory.
Related
I'm new to stackoverflow and really hope to get answers to my question about crontab.
I'm using MacbookAir in BigSur(don't know if it affects)And my crontab just couldn't run python scripts below.
f = open("test_crontab.txt",'w+')
f.write("Success")
f.close()
But I could run this script with Spyder.
I could edit crontab with crontab -e, and my code in vi crontab was
13 01 * * * /bin/zsh /Users/myusername/Documents/Lynn\'s\ Python\ work/shopee/test.py >>/Users/myusername/Desktop/out.log 2>>/Users/tzulingkuo/Desktop/err.log
But it failed to generate txt file.
The messages in err.log were like below:
/Users/*myusername*/Documents/Lynn's Python work/shopee/test.py:5: missing end of string
/Users/*myusername*/Documents/Lynn's Python work/shopee/test.py:6: unknown username ''
I've searched stackoverflow for two nights and couldn't find the solution. I major in finance and I have no friends studying computer science.
Could anyone help me 🥺
Any help is reaaaaally appreciated!
Since that's a python script, you need to run it in python, not /bin/zsh. zsh is trying to interpret it as a shell script, but the syntax is all wrong, so you get errors.
The simplest way to fix this to replace /bin/zsh in your crontab entry with /usr/bin/python (assuming you're using the built-in Python version 2; if you have installed Python version 3 and want to use that, run which python at the command line to find its path).
But it's generally better to add a shebang line to your python script, and let that control how it's run. Add this as the very first line of the script:
#!/usr/bin/python
(Again, if you want to use Python 3, replace the path part with the path for the Python 3 interpreter.) Then make the script executable with chmod +x /Users/myusername/Documents/Lynn\'s\ Python\ work/shopee/test.py, and just remove the /bin/zsh part from the crontab entry.
BTW, you're probably going to have trouble with paths. By default, cron jobs run with their working directory set to your home directory, so when the script opens test_crontab.txt, that's going to be interpreted as /Users/myusername/test_crontab.txt. If you want it down in the Documents/Lynn's Python work/shopee directory, you'll have to specify that explicitly.
You may also run into trouble with missing environment variables. If you're setting any environment variables in your shell initialization scripts that configure Python, add libraries, etc, those won't be set up in the cron job's environment.
I am trying to run a simple python script with cron and I cannot for the life of me get it to work. I am running Ubuntu 18.04.
I know there are many questions (and answers) on this. I have looked at a number of them and I still cannot get it to work.
My python script is:
#!/usr/bin/env python
from tkinter import messagebox
messagebox.showinfo('MessageBox','Hello world!')
I have followed Unable to Run Python Script In Cron and changed the permissions as follows:
chmod +x /home/brb/bugs/projects/cron/hello.py
I can successfully run this from the command line as follows:
brb#bishop:~$ /home/brb/bugs/projects/cron/hello.py
Inside crontab I have tried to execute the script in 2 different ways as follows (every 2 or 3 minutes to try and pin down which one works...)
*/2 * * * * /home/brb/bugs/projects/cron/hello.py
*/3 * * * * /usr/bin/env python /home/brb/bugs/projects/cron/hello.py
Nothing happens.
I followed http://www.jessicayung.com/automate-running-a-script-using-crontab/ and tried to inspect the error file as follows:
cd ../..
cd var/mail
nano $brb
and I observe a message in nano that says
[ Directory '.' is not writable ]
I am not sure which directory '.' is, perhaps it is var/mail. In any case, not sure how to make it writable. I will google chmod some more to get this writable I guess, but that still doesn't solve why cron is not working when it looks like all the examples I've seen...
EDIT:
I followed How to run an X program from outside the X session (e.g. from the console or SSH) as per the comment from triplee and, not really knowing what I am doing, I attempted to change my display permissions as follows:
brb#bishop:~$ export XAUTHORITY=/home/brb/.Xauthority
brb#bishop:~$ export DISPLAY=':0'
My cron job is still not work...
Your problem is twofold:
cronjob has does not know where it could be displaying a graphical app (which X server or DISPLAY to use.
This can be addressed by making sure DISPLAY is set in the environment of you script. Run echo $DISPLAY in a terminal running in your X session. :0 is a very likely it.
Even if it knows the correct DISPLAY, it does (should) not have access to it. If you are running the cronjob under the same user as the one running your target X session. If it is a different user, and it has access to your user's home, you could reuse it's authority file by setting XAUTHORITY to point to it.
You can also export necessary information from your running session (renewed with each session), type this into a terminal there: xauth list $DISPLAY and add it for your cron running user: xauth add displayname protocolname hexkey (using those three values as returned by xauth list.
I'm trying to run a python script on my raspberrypi using cron.
I did the following:
crontab -e # To edit a crontab job
After the cron file opened, I added the following line:
#reboot /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log # JOB_ID_!
If I understand the documentation correctly, this cron job should be executed every time after the system booted up. However in my case, when I reboot the computer, the script will not be executed.
What's strange:
I checked the log file and it's empty, so it seems like everything goes fine
If I run the given command manually (so basically write the following code to the terminal) it executes and works correctly: /usr/bin/python /home/pi/path/to/file/example.py > /home/pi/cronlogs/mylog.log
I guess I missed something really obvious but I can't see it. Please can I ask for any advise how to debug this. Thanks!
The cron definition looks correct; I just checked this on my Pi running Debian stretch and it works OK:
#reboot /usr/bin/python /home/pi/example.py > /home/pi/mylog.log
Some other possible reasons it might not work:
working directory issue (if you're using relative paths)
a long running script (being a scraping script it might take a while to complete) - you can check if it's still running using ps aux | grep python
the script does not output anything (would need some more details about the script)
Just to be sure you catch any errors from the script, redirect stderr to stdout by using 2>&1
I use a RaspberryPi 3 with UbuntuMate 16.04. On it, I want to start a little Python (3.5) program every midnight. For that I call a little shell script, so that I can change into the wanted directory comfortably.
crontab:
5 0 * * * /path/to/script/start.sh
start.sh (yes, it's executable):
#!/bin/bash
cd /path/to/wanted/workingDir/
python3.5 ControllerQueue.py
#also tried: python3.5 ControllerQueue.py &
Now if I execute the programm or script from the terminal, everything runs fine. But if I use the crontab it starts the script and stops right after. I also tried running the programm directly but get the same outcome. The paths are correct as I copied the workingDir path from the crontab-file and started it via the terminal.
Is there something I overlook?
As stofvl suggested, I saved the error output of my shell script. It turns out that I needed to add a display. My programm is divided into two scripts. One which provides a GUI and the other main application. The script only starts the main application, without the GUI, but it seems that this didn't matter.
This discussion helped me solve the problem.
Okay so I have a bash script that simply downloads a web page, and then I use python to pulls some data out of the downloaded page.
So my bash script is along the lines of
#!/bin/bash
html_file="web_page.html"
wget -O /home/michael/Documents/CS288/homework7/web_page.html http://markets.usatoday.com/custom/usatoday-com/html-mktscreener.asp?exchange=13\&screen=1
python hw_7_2.py $html_file
Now, when I just execute this bash script from the command line it runs fine, the wget runs and then my python script executes, however when I set it up as a cron job the wget will run but the python script never executes.
I have not really set up cron jobs so this I think may be the issue. This is basically what my crontab file looks like
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
* * * * * michael /home/michael/Documents/CS288/homework7/usatoday_runner.sh
try to replace the cron line with :
* * * * * michael /home/michael/Documents/CS288/homework7/usatoday_runner.sh > /tmp/why_is_this_failing.log 2>&1
the answer may be in the /tmp/why_is_this_failing.log
It's possible that your script doesn't have some environment variables set. When a cron job runs it doesn't have your normal profile information - it doesn't load your .profile/.bashprofile (simpler path, JAVA_HOME, etc) one possible option is to have the script source your .profile etc.
Cron frequently fails because of $PATH/working directory sorts of problems. You're setting the $PATH, but I wouldn't be surprised if neither your bash script nor your python script work if you aren't in the right directory.
Try using more absolute paths and see if that clears things up. Similarly, try running your cron command yourself from / or someplace and see if it works for you.