running cron job with sys argument - python

I am trying to run a python file that takes in one argument (CSV file). It works when i run the script in the terminal but it gives errors when i run it in cron.
This is the line that i run in the terminal:
python nb2.py my_csv_file.csv
And here is my code that i am trying to run in cron:
42 13 * * * /usr/local/bin/python2.7 ~/nb/Development/code/nb2.py ~/nb/Development/code/my_csv_file.csv &> /tmp/June_QB_cat.log
The error says that it cannot find a sqlite table file which is already in the code folder.

Note that when you run the command in the terminal, you're in the ~/nb/Development/code directory already and so your current working directory is that; when you run in it cron, it is not. I would suggest either doing (in your cron job) cd ~/nb/Development/code && python nb2.py my_csv_file.csv &> /tmp/logfile.txt or doing an os.chdir("~/nb/Development/code") as the first step in your code. (Also, I'd suggest doing /home/username instead of ~ just in case you aren't running cronjob as your username at some point, but given the error you get, that sounds like it's not an issue)

You can get the path of a file relative to the current script with
import os.path
relative_path = os.path.join( os.path.dirname(__file__), "sqlitetable" )

Related

Run external program when script is run from cron

I have a python script that works fine when executed from command line. But when I try to run it from cron, it does not work.
command = "rsync -avHP --delete --bwlimit=800 rsync://mirror.nsc.liu.se/CentOS/7.8.2003/ /home/me/reposync/centos"
proc=subprocess.run(command.split(), capture_output=True)
The cronjob runs. The cronfile looks something like this:
PATH=/home/me
40 13 * * * me cd $PATH && ./reposync.py sync 2> /tmp/test.txt
But I get this error (yes, twice) from print(proc.stderr.decode('utf-8')):
-avHP: rsync: command not found
It seems like the problem has to do with not finding rsync but I don't know what to do about it.
The output from /tmp/test.txt:
FileNotFoundError: [Errno 2] No such file or directory: 'rsync'
I have tried adding shell=True to the subprocess.run but it does not seem to make a difference. Or well it does not throw that exception, but I still get the same error when printing stderr from proc.
I suppose that I could solve it by including the absolute path to rsync but it feels like it's a bad idea. But I could be wrong about that. If that is the right approach, please explain why.
In your crontab, you are overwriting your $PATH variable, which now contains only the directory /home/me. Your rsync executable now cannot be found, since it is not in your home directory.
Change your crontab entry by removing the PATH=... line and calling your script with it's full path:
40 13 * * * me /home/me/reposync.py sync 2> /tmp/test.txt
You might have to define shell in your crontab.
The question has been asked previously.
crontab: python script being run but does not execute OS Commands

Running python script from crontab?

I need some help running a python script from crontab:
The script looks for subfolders from current path and does something to them, also extracts a zip file located in the same folder of the script into each found subfolder.
When I go with cd /folder/folder then python script.py is all good. But when run it with crontab it runs in users home folder and not where the script is placed.
To overcome this I placed in crontab something like this:
* * * * cd /folder_of_scrpit/ && /python_path/python script.py >> log.txt
and works as needed but feels weird, is there a better way of achieving this?
You can cd in crontab the way you do it. Or you can call os.chdir() in your script. In the latter case you can write the directory in the script or pass it as a command line argument: /python path/python script.py /folder/folder.

Referencing ini file fails in cron

I have a python script that queries a database. I run it from the terminal with python3 myscript.py
I've added a cron task for it in my crontab file
*/30 9-17 * * 1-5 python3 /path/to/my/python/script\ directory\ space/myscript.py
The script imports a function in the same directory that parses login info for a database located in database.ini in the same directory. The database.ini is:
[postgresql]
host=my-db-host-1-link.11.thedatabase.com
database=dbname
user=username
password=password
port=10898
But currently cron outputs to the file in my mail folder:
Section postgresql not found in the database.ini file
The section is clearly present in the database.ini file, so what am I missing here?
Instead of running "python3 myscript.py" in the directory where it is present, try running it from some other directory (like home directory). Most likely you will see the same issue.
Note that cron's current-working-directory is different on different systems. So, the safest method is to explicitly switch to the directory where your script is and run the command there:
cd /path/to/my/python/script\ directory\ space/ && python3 myscript.py
Try this:
import os
...
change --> filename=database.ini
for --------> filename=os.path.dirname(__file__)+'/database.ini'

Can't execute a cron job

I am creating a cron job to execute a python script
hello.py
a = 'a cron job was executed here'
text_file = open('output_hello.txt', 'w')
text_file.write(a)
text_file.close()
Works fine if I execute via terminal, I am on ubuntu 15.10.
My cron job file is:
* * * * * /usr/bin/python /home/rohit/hello.py
(excluding the #)
I am a root user and creating the job in /var/spool/cron
The issue is that it is not executing the script. I don't know why.
One does not simply modify the crontab, you run the command:
crontab -e
and edit from there. Execute the above command using sudo if you want it to run as root.
Assuming your paths are correct, your script may not have the right environment or it may not be executable. Ensure your script starts with:
#!/usr/bin/python
And also that you then give execute permission to that script:
chmod a+x hello.py
Ensure you use crontab -e and if you have any doubts about your syntax, you can find more info here:
https://help.ubuntu.com/community/CronHowto

Add CRON via shell to run Python

My cron doesn't seem to execute every 5 mins. Can anyone show me where I have gone wrong?
I made it executable using this command:
chmod +x /etc/utilities/poll.py
I can run it manually with this command:
cd /etc/utilities
python poll.py
When I run it like this I get an error:
root#li453-78:~# /etc/utilities/poll.py
-bash: /etc/utilities/poll.py: Permission denied
This is the command I use to add it to shell (via my automatic deployment script):
crontab -l | { cat; echo "*/5 * * * * /etc/utilities/poll.py"; } | crontab -
The start of my python file is like this:
#!/usr/bin/env python
So, could someone please enlighten me about how I should be adding the cron to my debian server via shell so that it executes?
Using the help from here, for whatever reason, even though I had the code correct to make the script executable, this line didn't seem to fire in my deployment script, meaning that all I had to do was run it afterwards to make it executable and then everything worked.
Lesson learnt: If you need to do this, the code above works

Categories

Resources