Why is my crontab not working if I excute a python file? - python

My request in crontab is to excecute a file pyhton every minute which write a new line in another txtx file. (Just doing pyhton3 on this file works). The problem is that when I try to do the same thing with crontab, it doesn't work. It is showing an empty file after some minutes.
Here is what I have written for now:
* * * * * /usr/bin/python3 /home/lubuntu/Documents/exercices/npexercice4/code/checkDiskSpace.py
Edit: Does it have something related to want is showing in my logs:
Feb 16 09:21:02 infravm CRON[11440]: (CRON) info (No MTA installed, discarding output)
Feb 16 09:21:02 infravm CRON[11441]: (CRON) info (No MTA installed, discarding output) ```

Related

I run crontab in every min but it cannot run the shell and show the result I want

I want to run the shell script every min.
Here is my shell script:
#!/bin/sh
python3 /home/ubuntu/environment/temp.py
Here is my temp.py:
f= open("temp.txt","w+")
Here is my crontab:
SHELL=/bin/sh
* * * * * /home/ubuntu/environment/temp.sh
However, there is no temp.txt file there after I save the crontab file.
In syslog, it shows:
Jul 13 11:58:01 ip-172-31-29-117 CRON[9344]: (ubuntu) CMD (/home/ubuntu/environment/temp.sh)
Jul 13 11:58:01 ip-172-31-29-117 CRON[9345]: (root) CMD (/home/ubuntu/.c9/stop-if-inactive.sh)
Where am I wrong?
It's unlikely your current direction when temp.py is being run, if it is being run, is what you think it is. Try writing to /tmp/temp.txt so you know where you should be looking.
Also valuable would be checking root's email to see if you have messages from cron about temp.sh failing because it doesn't know what this python3 you speak of is.

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.

How else can I troubleshoot this cron job?

I've been working (and searching) to get this cron job / python script running for some time now. However, it's obviously not working. I'm not sure how else to troubleshoot why, and I've tried several things I've found here in other SO questions.
path to script: /home/phil/cron_jobs/octoStatus.py
I would like cron to run every minute.
Crontab.txt file:
*/1 * * * * python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt
The octolog.txt was to capture the STDOUT and STDERR info. Output of "sudo tail /var/log/cron"
Jul 3 10:20:00 bsd /usr/sbin/cron[83876]: (root) CMD (/usr/libexec/atrun)
Jul 3 10:20:00 bsd /usr/sbin/cron[83877]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
Jul 3 10:21:00 bsd /usr/sbin/cron[83903]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
Jul 3 10:22:00 bsd /usr/sbin/cron[83934]: (phil) CMD (python /home/phil/cron_jobs/octoStatus/octoStatus.py &> octolog.txt)
It appears to be running each minute as desired. However, the expected results of the script are not occurring. octolog.txt is also not being created. When I manually run the exact statement shown in the cron-log, everything works correctly as expected, and the octolog.txt file is created.
I am running this on FreeBSD, and I went to look at the /var/log/syslog, but it doesn't exist. I'm new to FreeBSD, but I'm not sure that means a lot in this situation, but I thought I'd mention it.
I'm not sure what other info would be helpful, as I'm stuck. Thanks. Phil
First off, to run your script every minute, you don't need */1. Cron runs every minute by default, so:
* * * * * /path/to/command
Next, your redirection may be broken. The bash man page has the &> format listed under "Redirecting Standard Output and Standard Error", so I assume that's what you're trying to do. But FreeBSD's /bin/sh is not bash. So:
* * * * * /path/to/command >/path/to/output.txt 2>&1
This sends stdout to your file, and duplicates stderr to stdout.
This brings us to:
* * * * * python /home/phil/cron_jobs/octoStatus/octoStatus.py > octolog.txt 2>&1
Note also according to man 5 crontab, you can set a MAILTO variable in your crontab file which will direct output/errors from your jobs to an email address.
Beware that the PATH used by cron may not include /usr/local/bin, where python is installed. If your octoStatus.py script includes a "shebang", then you may be able to execute it directly. Otherwise, you will either have to provide the full path to your python binary, or add a PATH variable to your crontab (akin to the MAILTO I mentioned above). In all cases, you can get instructions as to format by reading man 5 crontab.
I would recommend you directly write to file from your python script with append mode.
Also just guessing but I think you should give absolute path to output file as stated in comment something like python home/phil/cron_jobs/octoStatus/octoStatus.py &> /home/phil/cron_jobs/octoStatus/octolog.txt

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

noob question on executing cron job

I have a cron job like the following:
07 14 * * 1-5 python /home/foo/cronscript.py
The script:
if __name__ == '__main__':
f = open('/home/foo/cronpass.txt','w')
f.write('abc')
f.close()
Checking the syslog I suppose the command did run, but with an error:
Aug 29 14:07:01 ubuntuserver CRON[16490]: (www-data) CMD (python /home/foo/cronscript.py)
Aug 29 14:07:01 ubuntuserver CRON[16488]: (CRON) error (grandchild #16490 failed with exit status 1)
Question: what does the error means? Does it means an error occurred while trying to execute the script, or that there is an error in my script?
What could be the error?
The usual error with crontab tasks, is that the environment in which they run don't have all the env. vars. you're accustomed to. Maybe here, PATH is not set to all the usual directories, and cron does not find the executable python. You should write the full path to it. as follows.
07 14 * * 1-5 /usr/bin/python /home/foo/cronscript.py

Categories