running python file with a bash script - python

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

Related

execute python script with cron job

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

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

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

bash file is not working for executing python file on fedora

I had created a python script for example test.py and path of the file is /Desktop/test.py
i want to run the file using cron jobs, so decided to create a bash script with name test.sh with the code below
test.sh:
#!/bin/bash
cd /Desktop/test.py
python test.py 2>log.txt
but this not working, when i tried to test it like below i am getting error as given below
sh-4.2$ python test.sh
File "test.sh", line 4
python test.py 2>log.txt
^
SyntaxError: invalid syntax
If this works fine then i can open cron tab with crontab -e and can execute with the following command
/2 * * * * /path/to/bashscript/test.sh
Can anyone make this work will appreciated.........
because you are trying to run a bash file through python!
error: python test.sh
you should instead bash test.sh
You need no bash-script for that.
You can do all things you do in the script in crontab:
*/2 * * * * cd ${HOME}/Desktop/; python test.py 2> log.txt

running a python script with cron

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

Categories

Resources