Crontab No Longer Runs Raspberry Pi Python Script in Background But Still Runs Manually - python-2.7

I am able to run my Python script in the background to control two servos by automatically launching the script at boot up using sudo crontab -e. I modified the script so I am now continually writing the servos current position to a horz.txt and vert.txt file and using those files to initialize the steppers to their home position so I can find home position again after a power loss. The script works fine with the horz.txt and vert.txt code that I added when I manually launch the script from the command line on the black screen using sudo python mystepper6.py, but it doesn't launch automatically at startup nor does it show up as running when I type ps ax on the command line. I added a little extra code just to wiggle the servos before the main program starts and and the servos automatically wiggle as programmed in the sudo crontab -e but then it just stops and won't continue to find the home position. It seems to be something with the new code but I don't know what it could be. My sudo crontab -e line is #reboot (sleep2;python /home/pi/mystepper6.py) &. Below is the script for mystepper6.py.
The script below was shortened to show the relevant lines due to limited space available in this post. The sudo reboot hangs with no error message near the bottom where it is commented 'move to home position' (it doesn't move). I am able to print adjustv and adjusth integers prior to that point so the variables are properly assigned a value from the .txt files at that point. When I manually run mystepper6.py from the command line it runs perfectly.
I have spent waaay too many hours on this problem and have purchased two e-books to no avail. Please help.
import RPi.GPIO as gpio # import library RPi.GPIO gpio=use general purpose input output pin names
import time # import time library
PINSh = [27,10,18,23] # variable 'PINS' holds a list of gpio pin numbers
SEQAh = [(27,),(10,),(18,),(23,)]
PINSv = [4,17,22,24] # variable 'PINS' holds a list of gpio pin numbers
SEQAv = [(4,),(17,),(22,),(24,)]
DELAY = 0.01 # time between motor steps (too small of a number then the motor stalls)
alpha = 140 # horizontal full scale viewing angle in motor counts 128 counts = 360 degrees
beta = 30 # was 15 vertical full scale viewing angle in motor counts 128 counts = 360 degrees
gpio.setmode(gpio.BCM) # tells RPi.GPIO we want to use pin names (BCM is the mfg)
for pin in PINSh: # pin is a variable name assigned a new value each loop; PINS is a list
gpio.setup(pin, gpio.OUT) # this says we want to use 'pin' as an output
for pin in PINSv: # pin is a variable name assigned a new value each loop; PINS is a list
gpio.setup(pin, gpio.OUT)
def stepper(sequence, pins): # def says 'stepper' is the function name (like a variable), then parameters are inside ()
for step in sequence:
for pin in pins:
if pin in step:
gpio.output(pin, gpio.HIGH)
else:
gpio.output(pin, gpio.LOW)
webcam_horz_home = open("horz.txt", "a")
webcam_horz_home.close()
webcam_vert_home = open("vert.txt", "a")
webcam_vert_home.close()
# load last position prior to power down
webcam_horz_home = open("horz.txt", "r")
rows = webcam_horz_home.readlines();
for row in rows:
adjusth = int(row)
webcam_horz_home.close()
webcam_vert_home = open("vert.txt", "r")
rows = webcam_vert_home.readlines();
for row in rows:
adjustv = int(row)
webcam_vert_home.close()
counter = 0 # move to home position
while counter < adjustv:
stepper(SEQAv, PINSv)
counter = counter + 1
# CONTINUAL PAN AND TILT OPERATION (box pattern):

I just tested this on my Pi running Raspbian and it worked.
First, I created a python script called some_script.py in the primary user's home directory.
Script contents:
import sys
import time
with open("/home/username/some_script.py.out", "w") as f:
f.write(str(time.time()) + " " + "some_script.py RAN!\n")
Then, in the terminal, I set the executable bit on the file which allows it be executed:
sudo chmod +x some_script.py
Then, also in the terminal, I typed sudo crontab -e and added the following line at the bottom of the root user's crontab:
#reboot sleep 2; /usr/bin/python /home/username/some_script.py
I then rebooted, cded to /home/username/ and confirmed that python had run at reboot and written to a file:
cat some_script.py.out
1458062009.53 some_script.py RAN!
UPDATE / SOLUTION
After confirming that the OP was able to successfully replicate the steps above, I was fairly confident that this was not an issue stemming from varying implementations of cron on different *nix systems or some other edge case. As expected, he could run python via cron, he could use the #reboot feature and he could write to disk without any sort of permission issues.
However, it still didn't work. What did solve the issue was simply to use absolute paths rather than relative paths.
Changing
webcam_horz_home = open("horz.txt", "a")
to
webcam_horz_home = open("/home/pi/horz.txt", "a")
and
webcam_vert_home = open("vert.txt", "a")
to
webcam_vert_home = open("/home/pi/vert.txt", "a")
got OP's steppers purring again :)
EXPLANATION / BACKGROUND
When executing scripts or commands via the crontab of other users (in this case root), paths relative to the regular user's home directory (eg. ~/.bashrc) will no longer be valid or will point to a different file with the same filename if it exists. The same "issue" is sometimes seen when people run servers that call scripting languages such as PHP (often executed by the www-data user).
On a Raspberry Pi, these things can cause a bit of a catch-22 when working with GPIOs because the GPIO device is locked down and not accessible to unprivileged users without a change of permissions and groups.
If OP ran his script as root by adding the cron entry via sudo crontab -e, he would have access to the GPIO device (since root has access to almost everything). However, the relative paths in his python script that implicitly reference the home folder of the non-privileged user pi would no longer be valid (since home now means the folder /root which is the home folder of the root user).
If OP ran his scripts via the non-privileged pi user's crontab by adding the cron entries via crontab -e (without sudo), the paths would be valid (home or ~/ would now mean /home/pi/) but then he wouldn't have access to the GPIO device since pi is pretty unprivileged. Note: the #reboot feature is not available in all cron implementations (some embedded and stripped down *nix distros don't have it) and often it doesn't work for users other than root.
This means that OP had two options (there might be more; i'm no Linux guru):
place the files vert.txt and horz.txt in the home folder of the root user.
simply change the paths specified in the python script and keep running the script #reboot in root's crontab.
No. 2 is probably better since it keeps the home folder of root empty and keeps OP's files in pi's home folder. root should generally not be messed with unless there's no other options such as using sudo.

Related

gdb run program in a loop until a breakpoint is reached then display stacktrace

I am trying to debug a very sporadic issue in my application. If ran ~1000 times my application surely hits a certain line it shouldn't and I would like to view the stack.
I tried using a gdb script cmd.gdb for this:
set logging overwrite on
set pagination off
set $n = 1000
break file.c:496
while $n-- > 0
ignore 1 9
condition 1 global_var == 10
run
end
How should I modify this script in order to print the stack when the breakpoint is reached?
I tried adding this after "run":
if $_siginfo
bt
loop_break
end
but it doesn't seem to work.
Actually, I have a Github repo with a Python-GDB extension, which does exactly the same thing as You have described, but with some more functionality.
You can just clone the repo:
git clone https://github.com/Viaceslavus/gdb-debug-until.git
and feed the python script to GDB with the following command inside GDB:
source <python script path>
Then, according to your example, you should run the next command:
debug-until file.c:496 --args="" --var-eq="global_var:10" -r=1000
*some remarks:
file.c:496 here is a starting breakpoint
"--args" parameter contains the arguments for your program
"--var-eq" is a debugging event, where 'global_var' is a variable name and '10' is a value
and finally the "-r" option specifies the number of times the program will be ran.
So all together this command will run your program 1000 times and will immediately notify You when the 'global_var' will be equal to 10.
Any additional information about the project could be found here:
https://github.com/Viaceslavus/gdb-debug-until.git in the README file.

How to run simultaneously four Python scripts from another scripts

I ‘m new with the Python world. I have four Python Script, and now during the testing phase, i have to run each of one from differenti console istances. My question is: is possible to create an unique script Python, and from it execute at the same time, the 4 scripts.
I’am working with a publisher/subscriber architetture, so i have one publisher and three sbuscriber.
Personally, I wouldn't run them from python. Create a batch file (windows) or a bash script(linux) and run all four of them as a background process so that they don't have to wait for each other to complete
In python you can try to use something like this:
import os
# list with the name of your scripts
scripts=["script_1.py", "script_2.py","script_3.py"]
for i in range (len(scripts)):
print "Executing ", scripts[i]
# string with script parameters
# in this case they are identical for everyone
script_parameters="-p parameters"
# build the command as if you typed it in the terminal
#
my_command = "python"+" "+scripts[i]+" "+script_parameters
print my_command
# run your command on the operating system
os.system(my_command)
I do not know if this is what you were looking for, but I hope you find it useful

Receiving back string of lenght 0 from os.popen('cmd').read()

I am working with a command line tool called 'ideviceinfo' (see https://github.com/libimobiledevice) to help me to quickly get back serial, IMEI and battery health information from the iOS device I work with daily. It executes much quicker than Apple's own 'cfgutil' tools.
Up to know I have been able to develop a more complicated script than the one shown below in PyCharm (my main IDE) to assign specific values etc to individual variables and then to use something like to pyclip and pyautogui to help automatically paste these into the fields of the database app we work with. I have also been able to use the simplified version of the script both in Mac OS X terminal and in the python shell without any hiccups.
I am looking to use AppleScript to help make running the script as easy as possible.
When I try to use Applescript's "do shell script 'python script.py'" I just get back a string of lenght zero when I call 'ideviceinfo'. The exact same thing happens when I try to build an Automator app with a 'Run Shell Script' component for "python script.py".
I have tried my best to isolate the problem down. When other more basic commands such as 'date' are called within the script they return valid strings.
#!/usr/bin/python
import os
ideviceinfoOutput = os.popen('ideviceinfo').read()
print ideviceinfoOutput
print len (ideviceinfoOutput)
boringExample = os.popen('date').read()
print boringExample
print len (boringExample)
I am running Mac OS X 10.11 and am on Python 2.7
Thanks.
I think I've managed to fix it on my own. I just need to be far more explicit about where the 'ideviceinfo' binary (I hope that's the correct term) was stored on the computer.
Changed one line of code to
ideviceinfoOutput = os.popen('/usr/local/bin/ideviceinfo').read()
and all seems to be OK again.

Weird Behavior in Windows XP with Python snippet

I wrote a snippet that would automatically copy a file from a source directory to a path on an usb. Because drive letter names are assigned by the PC independent o the slot I figured I would GetLogicalDrives() and if the path to the usb directory is in any of those drives then it would copy (I hope I'm making some sense). Here is the piece of code I wrote in Python:
import itertools, ctypes, string, sys, os.path, shutil
def drive_list():
drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
drive_list = list(itertools.compress(string.ascii_uppercase,
map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
return drive_list
for drive in drive_list():
path = drive + ":\\targer_directory\target_file.ext"
if drive not in ["C", "D", "E"]:
if os.path.exists(path) == True:
shutil.copy2(r'C:\source_directory\source_file.ext', path)
Whenever I run this script I get a bunch of error messages saying:
"Exception Processing Message c0000013 Parameters 75b1bf7c 4 75b1bf7c 75b1bf7c"
I think this probably means that I have some "ghost drives" in my pc. Any help in bypassing this behavior is deeply appreciated.
Note: The code runs at the end and the copy job is done succesfuly, but not until the error messages are cleared which is not the objective if I want to perform automatic backups.
I'm not sure whether this will help or not. But you can try this!
We were receiving the same error on execution of a windows application (an .exe file) and the below steps resolved it.
Goto Registry Editor
(Run -> type 'regedit' -> hit enter)
Expand HKEY_LOCAL_MACHINE
Expand System
Expand CurrentControlSet
Expand Control
Select Windows
Double click on ErrorMode in the right side pane
Set the Value Data as 2
Click OK
Restart your system

Condor output file updating

I'm running several simulations using Condor and have coded the program so that it outputs a progress status in the console. This is done at the end of a loop where it simply prints the current time (this can also be percentage or elapsed time). The code looks something like this:
printf("START");
while (programNeedsToRum) {
// Run code repetitive code...
// Print program status update
printf("[%i:%i:%i]\r\n", hours, minutes, seconds);
}
printf("FINISH");
When executing normally (i.e. in the terminal/cmd/bash) this works fine, but the condor nodes don't seem to printf() the status. Only once the simulation has finished, all the status updates have been outputted to the file but then it's no longer of use. My *.sub file that I submit to condor looks like this:
universe = vanilla
executable = program
output = out/out-$(Process)
error = out/err-$(Process)
queue 100
When submitted the program executes (this is confirmed in condor_q) and the output files contain this:
START
Only once the program has finished running its corresponding output file shows (example):
START
[0:3:4]
[0:8:13]
[0:12:57]
[0:18:44]
FINISH
Whilst the program executes, the output file only contains the START text. So I came to the conclusion that the file is not updated if the node executing program is busy. So my question is, is there a way of updating the output files manually or gather any information on the program's progress in a better way?
Thanks already
Max
What you want to do is use the streaming output options. See the stream_error and stream_output options you can pass to condor_submit as outlined here: http://research.cs.wisc.edu/htcondor/manual/current/condor_submit.html
By default, HTCondor stores stdout and stderr locally on the execute node and transfers them back to the submit node on job completion. Setting stream_output to TRUE will ask HTCondor to instead stream the output as it occurs back to the submit node. You can then inspect it as it happens.
Here's something I used a few years ago to solve this problem. It uses condor_chirp which is used to transfer files from the execute host to the submitter. I have a python script that executes the program I really want to run, and redirects its output to a file. Then, periodically, I send the output file back to the submit host.
Here's the Python wrapper, stream.py:
#!/usr/bin/python
import os,sys,time
os.environ['PATH'] += ':/bin:/usr/bin:/cygdrive/c/condor/bin'
# make sure the file exists
open(sys.argv[1], 'w').close()
pid = os.fork()
if pid == 0:
os.system('%s >%s' % (' '.join (sys.argv[2:]), sys.argv[1]))
else:
while True:
time.sleep(10)
os.system('condor_chirp put %s %s' % (sys.argv[1], sys.argv[1]))
try:
os.wait4(pid, os.WNOHANG)
except OSError:
break
And my submit script. The problem ran sh hello.sh, and redirected the output to myout.txt:
universe = vanilla
executable = C:\cygwin\bin\python.exe
requirements = Arch=="INTEL" && OpSys=="WINNT60" && HAS_CYGWIN==TRUE
should_transfer_files = YES
transfer_input_files = stream.py,hello.sh
arguments = stream.py myout.txt sh hello.sh
transfer_executable = false
It does send the output in its entirety, so take that in to account if you have a lot of jobs running at once. Currently, its sending the output every 10 seconds .. you may want to adjust that.
with condor_tail you can view the output of a running process.
to see stdout just add the job-ID (and -f if you want to follow the output and see the updates immediately. Example:
condor_tail 314.0 -f