Cannot run Python from Cron - python

I have a python script that will read the temperature of from a probe on the GPIO pins of a Raspberry-Pi, and will append that temperature to a log file. Running the script form terminal with sudo permissions works fine:
sudo python /home/pi/temp.py
I've attempted to run the script every 15 minutes from sudo's crontab file with the line:
*/15 * * * * python /home/pi/temp.py
This fails, with the output being
Traceback (most recent call last):
File "/home/pi/temp.py", line 8, in <module>
subprocess.call(['modprobe', 'w1-gpio'])
File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I know the issue is with the modprobe subprocess call, but I can't identify what exactly. In my script, I have the following code related to the issue:
import subprocess
subprocess.call(['modprobe', 'w1-gpio'])
subprocess.call(['modprobe', 'w1-therm'])

This is because cron has its own PATH variable and doesn't use the same path that you do.
For that reason, it would be advisable to call any programs that you use (especially through python's subprocess) with an absolute path to the executable
You could do which modprobe on the commandline to find where modprobe lives (probably in /bin/), and then change your call in subprocess.py to subprocess.call(['/bin/modprobe', 'w1-gpio'])

Related

Python script does not run on crontab

I want to run a python script periodically, so I use crontab. This is what my crontab -e looks like:
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/local/opt/python/libexec/bin
*/1 * * * * PYTHONPATH=/usr/local/lib/python/2.7/site-packages /usr/local/opt/python/libexec/bin/python /Users/username/Desktop/processes/test.py
The python version I am using is downloaded from home-brew.
As I've seen in many cases, I can run the test.py manually, but crontab -e does not seem to work. I might have included extra things, but I don't think these should affect the output.
The error I get through cat /var/mail/username is this one:
Traceback (most recent call last):
File "/Users/username/Desktop/processes/test.py”, line 34, in <module>
subprocess.call(['bash', commands_txt_shortened]) # Run the txt file to retrieve the images.
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1025, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

Error while using Python wifi module

When i try to run the scan operation using the wifi library as mentioned in the documentation, i get the following error.
(lsbaws)Keshav:bin root# wifi scan
Traceback (most recent call last):
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/bin/wifi", line 202, in <module>
args.func(args)
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/bin/wifi", line 51, in scan_command
print_table([[cell.signal, cell.ssid, 'protected' if cell.encrypted else 'unprotected'] for cell in Cell.all(args.interface)])
File "/Users/Keshav/Documents/Github/Webserver/webserver/lsbaws/lib/python2.7/site-packages/wifi/scan.py", line 29, in all
stderr=subprocess.STDOUT)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 566, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 709, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1326, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
I figured that this python wifi library does not work on mac.
An alternative to do wifi scanning is using the tool that comes in mac called airports.
To start using the tool -
$ cd /usr/sbin
$ sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
The airports -I flag is useful
Also just typing airports will open up the partly explicit manual page
Refrence link - Airports Example

How to run bash 'tc' command within python?

I want to us traffic control of the Linux kernel with Python to simulate lost, corrupt and duplicate packages. I'm already able to configure this with the Linux Terminal, but I have to use python.
bash cmd works:
tc filter show dev eth1
python doesn't work:
>>> subprocess.call(["tc", "filter", "show", "dev", "eth1"])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Thanks.
The python subprocess doesn't know about your shell environment. So provide absolute path to your command, something like:
subprocess.call(["/sbin/tc", "filter", "show", "dev", "eth1"])
find the exact location with command which tc in your shell.
The basic way if you don't need any special control is to use os.system() to launch a command as if you were in your shell command line (no need to specify the full path if in your $PATH):
import os
os.system("tc filter show dev eth1")
This should work exactly as if you did in your cmd:
$ tc filter show dev eth1

How do you run another program from a python script?

This is my code so far
from subprocess import call
call("/Users/oscar/Desktop/Controlled Assessment/Currency Converter.py")
When I run it I get this error message
Traceback (most recent call last):
File "/Users/oscar/Desktop/Controlled Assessment/ISBN Checker.py", line 10, in <module>
call("/Users/oscar/Desktop/Controlled Assessment/Currency Converter.py")
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 470, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 744, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 1394, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
Since it looks like you're trying to execute another Python script, why not import it?
import Currency_Converter
And then run the modules you want in there? Take the space out of the file name and replace it with an underscore.
Check the interpreter directive from your script.
It should start with:
#!/usr/bin/env python
print 'your python script...'
That means that when you call this script directly, it will be interpreted by python binary.
You can get more info on http://en.wikipedia.org/wiki/Shebang_(Unix)
use subprocess.Popen
import sys, subprocess
subprocess.Popen([sys.executable, "myscript.py"])
This should run myscript.py within the Python command line.

How to delete bash history in python script on Mac?

I want to delete bash history with a python script on my Macbook Pro.
I know two ways to delete bash history with bash shell
1.rm ~/.bash_history
2.history -c
But these command does not work in python script with subprocess:
1.rm ~/.bash_history
import subprocess
subprocess.call([‘rm’, ‘~/.bash_history'])
error:
rm: ~/.bash_history: No such file or directory
2.history -c
import subprocess
subprocess.call(['history', '-c'])
error:
File "test.py", line 8, in
subprocess.call(['history', '-c'])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >524, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >711, in init
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line >1308, in _execute_child
raise child_exception
Any ideas?
You have two questions here:
First, python doesn't understand ~, you need to expand it:
subprocess.call(['rm', os.path.expanduser('~/.bash_history')])
Second, history is a shell built-in. Use the shell to invoke it:
subprocess.call(['bash', '-c', 'history -c'])

Categories