Can't execute a cron job - python

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

Related

crontab won't run os.system python command

Using ubuntu's 16.04 crontab and #reboot to run python3 script. The script runs properly on reboot as I see the logged output. However, my script's os.system command is not running. It runs fine if ran outside of crontab. My scripts are all executable.
crontab -l output:
SHELL=/bin/bash
#reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &
scheduler.py code:
#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')
When I logged the output of the os.system command , there was no output.
As a side note, I am running python schedule commands to check the general health of a webserver. crontab doesn't seem to be the right tool for this so I just use crontab to start my python scheduler on reboot.
I am using flask as the webserver, and would use gunicorn and systemctrl if I could get it to work... but it didn't so this is my workaround.
The point is that, the command called by os.system is not in default path.
For example, tcpdump is not in /usr/bin/.
So, you can solve the problem by adding the full path of the command.
I was facing the same issue when we try to run python script directly in crontab it just by passes the os.system() commands.
Make launcher.sh:
#!bin/bash
cd /home/pi/
sudo python example.py
Then, make your script executable:
chmod 755 launcher.sh
And at last, add your script to crontab:
crontab -e
and add this line at the end:
#reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)

running cron job with sys argument

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

Using cron to start a python script at every boot

I tried using cron to start python script , at boot (as mentioned in the link).Running a python script At Boot using cron
I made a python script "hello.py":
#!usr/bin/env python
import time
print "Hello World!"
time.sleep(10)
Then chmod +x hello.py.
I checked,if it running or not,it is giving me output.
I used crontab -e command and added the line #reboot python /home/pi/hello.py &.
Reboot using sudo reboot , but nothing happened! I am newbie .Although I read many discussions but I am not able to fix that!
Generally when I want to verify whether a cronjob ran or not, I redirect all output to a log file like so:
12 0 * * * python /Path/To/File.py > /Path/ToLog/log 2>&1
Then you can check this log timestamp and for its contents

Using Crontab with Python

I'm trying to schedule a python script to run every minute or every hour.
What I did so far :
crontab -e
In the crontab file I added
* * * * * /usr/bin/python /path/to/script/script.py
After I save the file I get the message
crontab: installing new crontab
However crontab is not running the script.
Any ideas?
Thanks,
Diez
I found the problem, I will put here the solution maybe it helps someone.
I tried putting in crontab -e * * * * * /usr/bin/python /path/to/script/script.py >>/tmp/script.out 2>&1 as Rafal suggested however the output was blank.
I read a lot about crontab last night and I found out that if you use a script with crontab and you write your output in a certain file you will need to modify your script and input the exact path for the file. If you don't put the full path the script will work when you run it manually. The script will still work if you run it with crontab but it won't know where to place its output.
So modifying
with open('output.txt', 'a') as f
with
with open('/path/to/file/output.txt', 'a') as f
did the job for me.
Thanks,
Diez
Depending on the script (e.g. scripts which prints a message to the console) you won't be able to distinguish whether the script is running or not.
You can assure that crontab is running correctly by typing in the console:
tail -f /var/log/syslog

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