I have a python script I want to fire off every night at midnight. I'm using cron scheduler right now to do so, however, I can't figure out why it's not working. For now, I've been using close times (within the next minute or so) to test the cronjob, but I will ultimately want it to work for midnight.
Here is what I put in my crontab file (to run for 2:43pm), hosted on my ubuntu machine:
43 14 * * * root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py
I even put the:
#!user/bin/python
on top of all the .py files.
I also did:
chmod +x "scriptname".py
For each of the .py files and still no luck. Am I missing something blatantly obvious? I should note, this is my first time playing with cron tasks.
From your current crontab file, you're basically running root /home/grantmcgovern/Developer/Projects/StudyBug/Main.py every time.
If you want to run it as root, use sudo crontab -e and put 43 14 * * * /usr/bin/python /home/grantmcgovern/Developer/Projects/StudyBug/Main.py instead.
I think it is looking for the command "root" so the syntax is wrong, so it should be this...
43 14 * * * /home/grantmcgovern/Developer/Projects/StudyBug/Main.py
If you need it to run as root then I think you can use su like this:
43 14 * * * su root -c "/home/grantmcgovern/Developer/Projects/StudyBug/Main.py"
If you add it to the system crontab then I think it will anyway.
Related
I'm just setting up a cron tab/job on my Cent OS developement server.
Within my crontab I have the following. (Ignore the time setting, this was added about 15:32 UTC server time just to get the next scheduled run in).
34 15 * * * cd welcomeclient-0.0.5 && python3.6 main.py
In the command line cd welcomeclient-0.0.5 && python3.6 main.py works fine. welcomeclient-0.0.5 is under root in the droplet, and python3.6 is in /usr/bin.
Any suggestions?
Try using absolute paths in your crontab command:
34 15 * * * cd /foo/bar/welcomeclient-0.0.5 && /usr/bin/python3.6 main.py
or, assuming the main.py does not also make use of relative paths inside of it :
34 15 * * */usr/bin/python3.6 /foo/bar/welcomeclient-0.0.5/main.py
Looks like you're trying to change directory in crontab, just like omu_negru said you'll need to use full path instead. That's because crontab even if running under your name will not inherit your environmental variables such as $PATH.
Try this. First, go to the directory where your script is... and turn your main.py to an executable file so you don't have to call python main.py anymore. The simplest way to do so is...
$ chmod u+x main.py
Now if you do ls -l you'll see that you have "x" in the user section of permissions which will allow you to run it directly.
-rwxr--r-- 1 user user 0 Aug 17 17:55 main.py
Now you're ready to simplify crontab syntax to something like this...
34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py
I also like to capture the output from the script to a log file so it's easier to troubleshoot when things don't work our as planned, as follows:
34 15 * * * /foo/bar/welcomeclient-0.0.5/main.py & >> /foo/bar/main.log
The log should be added to log rotation, otherwise it will keep filling up and eventually make your system run out of space, but that's another topic already addressed on this site.
I want a python webscraping program to be run everyday at a certain time. For that i am using this command in the cron in ubuntu
28 22 * * * root /home/ahmed/Desktop python hello.py
It just doesnt work. there must be something wrong with it. can anyone help me please?
Try adding #!/usr/bin/python (called shebang line) to the top of your Python script and then
28 22 * * * root /home/ahmed/Desktop/hello.py
You have to make your script executable like this (run this as a separate command):
sudo chmod +x /home/ahmed/Desktop/hello.py
From the Shebang page on Wikipedia:
Under Unix-like operating systems, when a script with a shebang is run
as a program, the program loader parses the rest of the script's
initial line as an interpreter directive; the specified interpreter
program is run instead, passing to it as an argument the path that was
initially used when attempting to run the script.[8] For example, if a
script is named with the path "path/to/script", and it starts with the
following line:
#!/bin/sh then the program loader is instructed to run the program "/bin/sh" instead (usually this is the Bourne shell or a compatible
shell), passing "path/to/script" as the first argument.
If you don't want to change anything this will work as well:
28 22 * * * root python /home/ahmed/Desktop/hello.py
/home/ahmed/Desktop is (most probably!) not a valid command name. You want
28 22 * * * root python hello.py
or possibly
28 22 * * * root python /home/ahmed/Desktop/hello.py
depending somewhat on why you put that folder name there.
The syntax of a regular user's crontab is different. I can imagine no legitimate reason to run a scaping program as root. To run it from your own crontab you should use
28 22 * * * python /home/ahmed/Desktop/hello.py
(again possibly without the path name, or with the path somewhere else in the command line).
I have a really simple code with docopt which creates a directory. The program works perfectly like this:
dbb create_dir
I need to run this using crontab in ubuntu 12.04. I used crontab -e and added this line:
0 14 * * * dbb create_dir
which should run the code on 2pm every day. My problem is this doesn't work. I checked
0 14 * * * mkdir test_dir
and it worked. So I thought the problem is not with the cron and as I could run the code without cron, I guess the main problem is the combiniation of these two. Is there any way to fix this? Thanx
So my friend figured it out. When I type "echo $PATH", I get this:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
I added "echo $PATH" to the crontab and the result was:
/usr/bin:/bin
So the paths are not the same. So I had to use
/usr/local/bin/dbb create_dir
instead of dbb create_dir
crontab fails to execute a Python script. The command line I am using to run the Python script is ok.
These are solutions I had tried:
add #!/usr/bin/env python at the top of the main.py
add PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin at the top of crontab
chmod 777 to the main.py file
service cron restart
my crontab is:
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
*/1 * * * * python /home/python_prj/main.py
and the log in /var/log/syslog is:
Nov 6 07:08:01 localhost CRON[28146]: (root) CMD (python /home/python_prj/main.py)
and nothing else.
The main.py script calls some methods from other modules under python_prj, does that matter?
Anyone can help me?
The main.py script calls some methods from other modules under python_prj, does that matter?
Yes, it does. All modules need to be findable at run time. You can accomplish this in several ways, but the most appropriate might be to set the PYTHONPATH variable in your crontab.
You might also want to set the MAILTO variable in crontab so you get emails with any tracebacks.
[update] here is the top of my crontab:
www:~# crontab -l
DJANGO_SETTINGS_MODULE=djangocron.settings
PATH=...
PYTHONPATH=/home/django
MAILTO="cron-notices#example.com"
...
# m h dom mon dow command
10-50/10 * * * * /home/django/cleanup_actions.py
...
(running cleanup actions every 10 minutes, except at the top of the hour).
Any file access in your scripts? And if so, have you used relative paths (or even: no explicit path) in your script?
When run from commandline, the actual folder is 'your path', where you start the script from. When run by cron, 'your path' may be different depending on environment variables.
So try using absolute paths to any files you access.
Check the permissions of the script. Make sure that it's executable by cron-- try chmod +x main.py.
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