Python scripts hang on exception until ctrl-c - python

I have an issue with running python scripts on one server running RH Linux with Python 2.6.6. When I run any script and it produces an exception, the script hangs until I press CTRL-C and then it prints the traceback information. It happens when I run the script from the command-line without calling python directly, making use of the shebang on the first line of the script. If I execute the script by calling python , I don't have the same hanging issue. I've searched and seen similar issues, but they were related to specific libraries where this happens with all python scripts. I've tried a different servers with the same script and didn't have the issue. I have included a simple script that I have been testing with. It tries to open a non-existent file.
#!/usr/bin/env python
tempfile = open('noexists.txt','r')
When I execute the code as "test.py" on the command-line, I get the following response:
~/bin$> test.py
^CTraceback (most recent call last):
File "/export/home/jwd3/bin/test.py", line 2, in <module>
tempfile = open('noexists.txt','r')
IOError: [Errno 2] No such file or directory: 'noexists.txt'
If I execute it as "python test.py" then I get the following response:
~/bin$> python test.py
Traceback (most recent call last):
File "test.py", line 2, in <module>
tempfile = open('noexists.txt','r')
IOError: [Errno 2] No such file or directory: 'noexists.txt'
The difference is difficult to tell here, but it is very evident when executing. Look at the first sample output and notice the "^C" before the Traceback. Until I hit CTRL-C, the script was just hanging. The second sample output returns the traceback of the exception imediately without hanging.
I've tried moving the script to a new location, changing the shebang from #!/usr/bin/python to #!/usr/bin/env python and different scripts. In all cases they behave the same. Any help would be greatly appreciated. I do not want to call all python scripts using "python " format.

Thanks to the help from the suggestions above, I was able to find the solution to the issue. After running strace, I found the code was hanging on a socket connection. Searching for the same socket connection info, I found another issue already posted on Stack Overflow that was similar. I then found a second issue that was also very similar. Both issues had the same suggestion. Apparently the Python script was conflicting with a service called abrtd. The suggestion was to restart this service (abrtd). After restarting it, the issue with the Python script hanging was resolved.

Related

Faulty Python script after using SFTP

I have a running python program I wrote in thonny.
Everything worked perfectly fine, when I ran it from the IDE and the terminal.
I run SSH on the Raspberry Pi I'm using for this application to start my program. Then I tried using SFTP to download the files the program puts out.
This worked too, but when I tried to start the program again, it suddenly errored out at (4th line, rest for context):
#PYGAME SETUP FOR SOUNDS
pygame.init()
pygame.mixer.init()
shuttersound = pygame.mixer.Sound('Shutter.wav')
savesound = pygame.mixer.Sound('Save.wav')
with:
Traceback (most recent call last):
File "/home/pi/Desktop/PictographBox/PictographBox_v1.1.py", line 19, in <module>
shuttersound = pygame.mixer.Sound('Shutter.wav')
pygame.error: Unable to open file 'Shutter.wav'
although the sound file remained unchanged.
I could still run the program perfectly fine from thonny.
I ran "dos2unix" on my script and it worked as expected again.
So I tried to use SFTP again and that produced the exact same problem on the same line (again fixed by "dos2unix").
I need to prevent this from happening, as downloading the files is crucial.
I first thought it was my FTP client, but I tried three different clients which produced the exact same result.
I didn't even open or otherwise touch the script with SFTP, just went into the folder.
Do I have a problem in my script?
All line breaks look fine.
No other files have problems, only my script and only after opening it's folder via SFTP.

Access file system from Python on boot-up cron

Please consider the following problem:
I have a Python script that runs on a linux machine (Raspberry pi 3, running Rasbian Jessie) on boot.
This script has been added to sudo crontab -e
The script itself starts with no problem but is unable to load in a certain file that is in the same directory as the script (Desktop), I have any print statements/issues going into a log file. Which reads as follows:
Traceback (most recent call last):
File "/home/pi/Desktop/mainServ.py", line 18, in <module>
mouth_detector = dlib.simple_object_detector(mouth_detector_path)
RuntimeError: Unable to open mouthDetector.svm
I assume this is because the script has no access to a filesystem, or perhaps LXDE/Desktop at boot time? I could very well be wrong on this.
Any solutions to this problem will be greatly appreciated.
Whenever you execute a script via crontab be ready for environment variables to be different. In this case you can simply use the whole path in the file you are trying to reference.
To see what the current environment variables are from within Python, use:
import os
os.environ
You may find there are other differences between the crontab environment and whatever interpreter environment you are using for testing.

cx_Oracle, and Library paths

Pretty new to all this so I apologize if I butcher my explanation. I am using python scripts on a server at work to pull data from our Oracle database. Problem is whenever I execute the script I get this error:
Traceback (most recent call last):
File "update_52w_forecast_from_oracle.py", line 3, in
import cx_Oracle
ImportError: libnnz11.so: cannot open shared object file: No such file or direct ory
But if I use:
export LD_LIBRARY_PATH=/usr/lib/oracle/11.2/client64/lib
Before executing the script it runs fine but only for that session. If I log back in again I have to re-set the path. Anything I can do to make this permanent? I'm trying to use Cron as well to automate the script once a week. It was suppose to automate early Monday morning but it didn't run.
EDIT: Just had to add the path to my .bashrc file in the root directory.
Well, that was pretty simple. I just had to add it to the .bashrc file in my root directory.

Find a program directory from within after turning to .exe using py2exe

I am writing a code which I want to have details to its own directory. e.g. C:\Users\User\Desktop\helloworld.py
Is there a way to find it from within the program using pywin32 or any other module?
If so, once I turn it into .exe using py2exe will it still be able to find it's own directory(C:\Users\User\Desktop\dist\helloworld.exe)
I tried the following code:
print os.path.dirname(os.path.realpath(sys.argv[0]))
This works but once I turn it into a .exe file it doesn't manage to run.... What other solutions can there be?
The error I get in the log file is the following error:
Traceback (most recent call last): File "finddirectory.py", line 4, in EOFError: EOF when reading a line
You can find that information on the py2exe wiki:
http://www.py2exe.org/index.cgi/WhereAmI

running command in background

I am using python subprocess module to run some command and store its output in background. The command is deployed on my machine. Now whenever i run the command from shell prompt it works fine. But when I try to run the same command using subprocess module it gives following error
The command to be executed is vxswadm listswitch all
process = subprocess.Popen('vxswadm listswitch all > tmp.txt &',shell=True)
>>> Traceback (most recent call last):
File "/usr/bin/vxswadm", line 30, in <module>
l.uname = os.getlogin()
OSError: [Errno 25] Inappropriate ioctl for device
Can anyone help me out to fix this error . Any suggestions will be helpful. Thanks in advance
Tazim
The problem is likely due to the bash shell terminating immediately after the & and sending the SIGHUP singal to all of it's subprocesses (standard shell behavior).
You can use the subprocess module to directly execute the command and can redirect the output to tmp.txt yourself by first opening the file and then by passing it's file handle to the stdout argument of the Popen call.
There is a problem with os.getlogin() and subprocessing and python.
See http://code.activestate.com/lists/python-list/288845/
You need to use something else, such as:
pwd.getpwuid(os.getuid()).pw_name (Unix only)
See also the discussion on a portable way to get the username.
Try changing it to ['vxswadm', 'listswitch', 'all', '>', 'tmp.txt','&'] and/or changing shell to False.
I think it might be the shell bit, though (if that fixes it).
You may also try adding stdin=subprocess.PIPE, stdout=subprocess.PIPE, though I doubt that will affect it.

Categories

Resources