I try to run my python scrip at startup but it doesn't work.
Here is my python script(doesn't work):
#!/usr/bin/env python
import paho.mqtt.publish as publish
from datetime import datetime
t = str(datetime.now())
print t
with open("/home/james/mqtt/log.txt", "a+") as f:
f.write("it works " + t + "\n")
Here is my python script(works):
#!/usr/bin/env python
from datetime import datetime
t = str(datetime.now())
print t
with open("/home/james/mqtt/log.txt", "a+") as f:
f.write("it works " + t + "\n")
Here is my rc.local files(also try crontab and setting up service in /ect/init.d):
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# /bin/mqtt_test.py &
# mosquitto_sub -t "mqtt"
/home/james/mqtt/script.sh
# /etc/mqtt/mqtt_test.py
exit 0
It look like by importing paho.mqtt.publish can make my script stop working, I am new to Linux, I have no idea why. Can someone help me out? Thanks for your help.
Ubuntu 16.04
Let me know if you need more info.
I have faced this problem myself. For me the problem was path. I could get it working by using a shell script to launch a python script and launch the shell script from crontab.
Here is my launcher.sh. You may not use sudo if you do not want. home/pi/record_data is the path where my file resides.
cd /
cd home/pi/record_data
sudo python record_video.py
In this case record_video.py is the python file I want to run at startup. In the crontab edit, I added this line below.
#reboot sh /home/pi/record_data/launcher.sh &
Do try this out if it work for you :) Good luck.
I have not got the logging the error into files working yet though.
it looks to me like you need to set/change the write permissions of the file before your python scrypt do this:
f.write("it works " + t + "\n")
for you is working because (maybe you are the owner of the file).
typical linux file permission is described as:
do use the chmod with the property flags, so linux has the rights to write the file too, please refer to the ubuntu help :)
Related
I am trying to test a simple cronjob and when I check if the cronjob has executed, it shows that it has but no file is being created as part of the task.
I have the following script, testjob.py which needs to be executed:
#!/usr/bin/env python3
import datetime
with open('testcron.txt', 'a') as outfile:
outfile.write('\n' + str(datetime.datetime.now() + 'myname'))
This is the cronjob:
#!/usr/bin/env python3
from crontab import CronTab
my_cron = CronTab(user='myname')
job = my_cron.new(command = 'python /Users/myname/Desktop/customers/cronjob/testjob.py')
#schedule job to run every 2 minutes
job.minute.every(1)
my_cron.write()
How can I troubleshoot this?
link to image with my crontab running via crontab -e: https://i.stack.imgur.com/pj9Pt.png
You can set up a cron job via crontab -e, which is much better than creating a python script to create a cron job. But anyways.
You can first troubleshoot by actually running your python script and seeing if there are any errors, and if the file is even created.
make sure the user for that cron job has the right permissions to execute the script.
try executing the command: python /Users/myname/Desktop/customers/cronjob/testjob.py directly from your terminal.
Judging from your response, the reason why its not working is because your script isn't able to open the text file.
When you're executing the script via: python /Users/myname/Desktop/customers/cronjob/testjob.py, the location of your text file "testcron.txt" depends entirely on where you are executing the script from.
So basically, unless "testcron.txt" is located in the same path / directory from where you are executing the script, its not going to work.
You can fix this by changing your cron tab to first navigate to where your text file is, and then run the python script.
For example, if your "testcron.txt" file is located in /Users/myname/Desktop/customers/cronjob/ then write your cron job as:
cd /Users/myname/Desktop/customers/cronjob && python ./testjob.py
You can instead of running the cron job with the python command run it like a shell script.
testjob.py:
#!/usr/bin/env python3
import datetime
with open('testcron.txt', 'a') as outfile:
outfile.write('\n' + str(datetime.datetime.now() + 'myname'))
Cronjob:
#!/usr/bin/env python3
from crontab import CronTab
my_cron = CronTab(user='myname')
job = my_cron.new(command = '/Users/myname/Desktop/customers/cronjob/testjob.py')
#schedule job to run every 2 minutes
job.minute.every(1)
my_cron.write()
Make sure you run chmod a+x /Users/myname/Desktop/customers/cronjob/testjob.py first to make the python executable.
Not sure what I'm doing wrong whether it's the code, the directory, or something else. Please help!
from crontab import CronTab
my_cron = CronTab(user='bgoldberg')
job = my_cron.new(command='python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py')
job.minute.every(1)
my_cron.write()
And here's the writeDate.py script:
import datetime
with open('dateInfo.txt','a') as outFile:
outFile.write('\n' + str(datetime.datetime.now()))
The writeDate.py script just writes the current timestamp to a txt file and it works fine when run separately. When I run python scheduleCron.py, it runs without error but it seems that it's not running the writeDate.py script because no txt file is created. When I enter crontab -l it correctly shows the job that was created: ***** python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py
Not sure what I'm doing wrong...
This is a cron "gotcha". Cron uses the command
python /Users/bgoldberg/PythonScripts/FunStuff/writeDate.py
which you expect to write to your current working directory, but cron will write to /var/log/syslog or some variation of this by default. It is trying to write to some place you don't have the permissions to (but it won't die), so you need to specifiy the absolute path of your output file.
changing your line in writeDate.py to write to an absolute path:
with open('/Users/bgoldberg/dateinfo.txt', 'a') as outFile:
will solve your problem.
This is my Python script (main.py):
#! /usr/bin/env python
import time
# ..... some file imports from the same folder .....
try:
# .... Some setup code
while True:
if turnOffRequestHandler.turnOffIsRequested():
break;
time.sleep(1)
except BaseException as e:
pass
finally:
# ..... Some code to dispose resources
And the way I try to invoke it on each startup was to first edit rc.local to become similar to this:
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
printf "My IP address is %s\n" "$_IP"
fi
python3 /home/pi/Desktop/ProjectFolder/sample/main.py &
exit 0
and then make my python script executable by navigating to the containing directory and executing the following command:
chmod 755 main.py
And then, I expected after a reboot of the system to get my script running. I can not tell if it runs or not. What I can tell is that it is supposed to call some web endpoints. I am now wondering if it actually got executed but the wifi just did not get connected yet.
How could I diagnose that? Because, when I try to execute manually (after the system booted up and Wifi got connected) like this:
pi#raspberrypi:~ $ /etc/rc.local
it is getting it started and everything works as expected.
EDIT: Is it possible to be something related to the fact that the script that I try to execute makes a reference inside of it to files which are located in the same folder (which is different than /etc/..)?.
I would try simple bash script with 'echo "something" >> to_file'.
you can redirect your output (like print "something") to a file while using the script.
change your script like this:
python3 /home/pi/Desktop/ProjectFolder/sample/main.py &> logfile.txt
and in your file use normal print to print in your file with 1 problem.
you have to flush the output while your code is running in order to see output file:
import sys
sys.stdout.flush()
edit
if you are using python 3.3 or above, there is an alternaive approach - print has argument to flush the output.
I created a python script that I would like to execute at each startup. I modified the etc/rc.local, but I don't get the script to run.
etc/rc.local addition (I added the sleep thinking it may help):
(sleep 10; /usr/bin/python3 /home/pi/mower-gps-tracking/app/gps_logger.py)&
imports in the different python scripts (I don't know if it matters):
from ftplib import FTP
import os
import serial
import time
import threading
from gpiozero import LED, Button
When I start the etc/rc.local manually via a ssh command, it runs fine.
Any idea what I'm missing ?
Check if the script (rc.local) is executable ie. has the 'x' attribute set.
And you will probably need a
#!/bin/bash
as line #1 of your script.
Hope this helps.
And as per Barmar above, probably more approp in the Unix forums.
I tried this on my linux box no issues .. As a test create a script that will wrap your script, have it write to a tmp file so you can see if it actually runs. Use nohup and & in your scripts.
/etc/rc.local:
nohup /root/script.sh &
/root/script.sh:
#!/bin/bash
echo "I'm starting something .." > /tmp/startup_thing.log
/root/gps_logger.py
note: all scripts need to be chmod a+x file.ext. if it's not running check for the file /tmp/startup_thing.log . If it's there then you have some other issue. If it isn't then rc.local isn't working.
#!/usr/bin/python
import requests, zipfile, StringIO, sys
extractDir = "myfolder"
zip_file_url = "download url"
response = requests.get(zip_file_url)
zipDocument = zipfile.ZipFile(StringIO.StringIO(response.content))
zipinfos = zipDocument.infolist()
for zipinfo in zipinfos:
extrat = zipDocument.extract(zipinfo,path=extractDir)
System configuration
Ubuntu OS 16.04
Python 2.7.12
$ python extract.py
when I run the code on Terminal with above command, it works properly and create the folder and extract the file into it.
Similarly, when I create a cron job using sodu rights the code executes but don't create any folder or extracts the files.
crontab command:-
40 10 * * * /usr/bin/sudo /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
also tried
40 10 * * * /usr/bin/python /home/ubuntu/demo/directory.py > /home/ubuntu/demo/logmyshit.log 2>&1
Notes :
I check the syslog, it says the cron is running successfully
The above code gives no errors
also made the python program executable by chmod +x filename.py
Please help where am I going wrong.
Oups, there is nothing really wrong in running a Python script in crontab, but many bad things can happen because the environment is not the one you are used to.
When you type in an interactive shell python directory.py, the PATH and all required PYTHON environment variable have been set as part of login and interactive shell initialization, and the current directory is your home directory by default or anywhere you currently are.
When the same command is run from crontab, the current directory is not specified (but may not be what you expect), PATH is only /bin:/usr/bin and python environment variables are not set. That means that you will have to tweak environment variables in crontab file until you get a correct Python environment, and set the current directory.
I had a very similar problem and it turned out cron didn’t like importing matplotlib, I ended up having to specify Agg backend. I figured it out by putting log statements after each line to see how far the program got before it crapped out. Of course, my log was empty which tipped me off that it crashed on imports.
TLDR: log each line inside the script