Why is my Cronjob giving me this error? - python

/bin/sh: /Users/alex/filename.py: No such file or directory
I have set a cronjob and it runs at the right time but the script isn't getting executed. When I check the mail I get that error. Both the script and the cronjob file are in:
/Users/alex
My cronjob file looks like this:
45 19 * * * /Users/alex/filename.py
I know the script works because I can launch it manually from the terminal.
Where am I screwing this up?

Try this:
45 19 * * * /path/to/python /Users/alex/filename.py
where path/to/python is something like /usr/bin/python (default on OS X I believe).

Are you sure it Users and not users? Does your filename.py have execute permission for cron?

Related

Scheduling a python 3.6 script in using crontab/cron

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.

Running cronjob (Python write file) on EC2

I have a simple Python script on EC2 to write a text to a file:
with open("/home/ec2-user/python/test.txt", "a") as f:
f.write("test\n\n")
That python script is here:
/home/ec2-user/python/write_file.py
When I run that script manually ('python /home/ec2-user/python/write_file.py') - new text is being written to a file ('/home/ec2-user/python/test.txt').
When schedule same script using Cronjob - no data is being added to a file. My Cronjob looks like this:
* * * * * python /home/ec2-user/python/write_file.py
I verify Cronjob is running and suspecting some ENV parameters are not the same during Cronjob execution (or something else is happening). What could be the case and how to fix it in a simple way?
Thanks.
I recently began using Amazon's linux distro on ec2 instances and after trying all kinds of things for cron all I needed was:
sudo service crond start
crontab -e
This allowed me to set a cron job as "ec2-user" without specifying the user or env variables or anything. For example, this single line worked:
0 12 * * * python3 /home/ec2-user/example.py
I also posted this here.
On crontab
HOME=/home/ec2-user/
* * * * * python /home/ec2-user/python/write_file.py
alternatively (testing purpose)
* * * * * root /home/ec2-user/python/write_file.py
On your write_file.py add shebang (first line)
#!/usr/bin/env python
BONUS: (feel free to ignore) my first response debug method for cron on minute tasks:
import os
while True:
print os.popen('pgrep -lf python').read()
executed from shell will pop out all python modules called by cron. After 60 secs you know whats going on.
it's bette to specify the absolute path of the python interpreter because the command may not be recognized by the system. Or you can add the shebang line to your script #!pathToPythonInterpreter at the first line of your script
make sure that the user who planning the execution of script have the right to read and write to the file
make sure that you are not using the system corntab /etc/crontab to planify because then you have to specify the user who will execute the command.
I hope that this will help you

Cron task python script not working

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.

how to run the python program using cron scheduling

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).

Cronjob with docopt

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

Categories