It might be very simple question, but how could I run a python script on my fedora dist every 2 days?
Thanks
Antonis
It is a question on cron.
First is add a SHEBANG line on top of your python script.
#!/usr/bin/env python
Make your script executable with chmod +x
And do a crontab -e and add 0 0 */2 * * /path/to/your/pythonscript.py
Related
This is my crontab.
SHELL=/bin/bash
PATH=/home1/<user>/.pyenv/shims:/home1/<user>/.pyenv/bin:/usr/lib64/qt-3.3/bin:/usr/nhnkrb5/bin:/usr/bin:/bin:/usr/X11R6/bin:/home1/<user>/.local/bin:/home1/<user>/bin
HOME=/home1/<user>
# Do something
0 1 * * * /home1/<user>/.pyenv/shims/python /home1/<user>/folder/myscript.py >> /home1/<user>/folder/$(date "+%Y.%m.%d-%H.%M.%S").log 2>&1
It runs perfectly from the terminal no matter where I execute it from. I have tried every answer on this page, and my cron doesn't return any errors.
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working
I have also checked that my $PATH the cron runs from is identical to the one in my bash. Checked using * * * * * env > /tmp/env.txt
Please tell me what am I doing wrong.
It is better to add the python header at line 1 to your main script. For example if your main script file is app.py then it's content can be following:
!#/home1/<user>/.pyenv/shims/python
# Here goes your Python Script.
and then
$ chmod +x app.py
Now you can add
# Do something
0 1 * * * /home1/<user>/folder/myscript.py >> /home1/<user>/folder/$(date "+%Y.%m.%d-%H.%M.%S").log 2>&1
I ran into the same problem, script working when I am running manually but didn't work with crontab. This method worked.
I'm trying to execute a python script every minute a cron job. I can execute the command using the terminal.
My script can be execute by the following comand:
python /home/pi/Desktop/sensor_testing/dht11.py
and in sudo crontab -e I typed:
***** python /home/pi/Desktop/sensor_testing/dht11.py
any advice why this isn't working?
Take a look here, it's a good explanation about adding a shebang to your python script. it will allow you to run the script easily without calling python explicitly, the only thing you need to do is add a correct python path to the shebang and your set.
tl;dr from link:
for running python 3 script, add this to the top of your script:
#!/usr/bin/env python3
or this for python 2.7
#!/usr/bin/env python2
The time fields are space-separated, i.e.
* * * * * python /home/pi/Desktop/sensor_testing/dht11.py
Its should be simply:
Put the absolute python path on the top of your your script First:
#!/usr/local/bin/python # Just assuming this path
Make the File executable:
chmod +x /home/pi/Desktop/sensor_testing/dht11.py
then place in cron..
***** /home/pi/Desktop/sensor_testing/dht11.py
i'm trying to schedule my pythohn script into Centos 7 with cron.
On my script at start i have added this:
#!/usr/local/bin/python
and this is my cron file that i have create into folder that contain python file
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
i have try to run the script into terminal by chmod +x and works fine.
But whit this configuration when in terminal i set crontab .cron the job doesn't work. The log file set into cron file are not write and script not run. The script could be write some data into db and the db is always empty.
Any help???
Thanks
Type crontab -e in the shell, this will open the cron editor. Here copy paste the entire command.
*/5 * * * * /usr/local/bin/python /home/Documents/SCRAPE_PYTHON/SCRAPE.py &>> /home/Desktop/log.txt
Now press Esc key then type colon :wq! and press Enter key. This will install your cron. Your cron should run every 5 minutes.
Your standard output and error will be appended to /home/Desktop/log.txt.
I want to run a python file with bash script for a certain time of the day. I script.sh like this (ofcourse execute it with chmod +x before)
#!/bin/bash
python /home/user/file.py
and with crontab -e
I wrote
* 01 * * * /home/user/script.sh
How can I make it run on 1 o'clock?
Edit: I have to run python file from a script because I will add some other things later.
Put a shebang/directive in the python script you're trying to run.
Similar to the shell script you gave, this should be the first line in your python script:
#!/usr/bin/python
...or for python version 3:
#!/usr/bin/python3
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