[Tutor] commands module
I've been trying to do something that I thought was going to be relatively straight-forward, but so far I haven't found a good solution. What I'm trying to do is discover a pid on a process and kill it. The way that I thought that I could do it is something along the lines of: import commands program = someprogram a = commands.getoutput('ps ax|grep %s ' % (program)) Then, I'd parse the returned info get the pid and kill it, probably via another command. However, what happens is that the ps ax portion truncates the listing at 158 characters. It just so happens that the unique name that I need in the list comes after that. So, works from the bash shell, but doesn't work using getoutput. I have also tried variations on a theme. For example, I created a shell file and tried to route into a file: ps ax|grep my_program_name ps.output No luck. Do you have any suggestions? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] commands module
I've been trying to do something that I thought was going to be relatively straight-forward, but so far I haven't found a good solution. What I'm trying to do is discover a pid on a process and kill it. The way that I thought that I could do it is something along the lines of: import commands program = someprogram a = commands.getoutput('ps ax|grep %s ' % (program)) Then, I'd parse the returned info get the pid and kill it, probably via another command. However, what happens is that the ps ax portion truncates the listing at 158 characters. It just so happens that the unique name that I need in the list comes after that. So, works from the bash shell, but doesn't work using getoutput. What's the result of getoutput(); ie, what is a? Note that bash and commands.getoutput() are not the same, since the latter executes 'sh -c', which is slightly different. I don't expect that'll solve your problem. Does the -w option help? I'm guessing it won't, since the truncation seem to be due to some odd character causing an EOF or something (I tried myself here, both on Linux OS X, without problems). Evert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] commands module (Forwarded back to list)
Evert Rol wrote: I've been trying to do something that I thought was going to be relatively straight-forward, but so far I haven't found a good solution. What I'm trying to do is discover a pid on a process and kill it. The way that I thought that I could do it is something along the lines of: import commands program = someprogram a = commands.getoutput('ps ax|grep %s ' % (program)) Then, I'd parse the returned info get the pid and kill it, probably via another command. However, what happens is that the ps ax portion truncates the listing at 158 characters. It just so happens that the unique name that I need in the list comes after that. So, works from the bash shell, but doesn't work using getoutput. What's the result of getoutput(); ie, what is a? Note that bash and commands.getoutput() are not the same, since the latter executes 'sh -c', which is slightly different. I don't expect that'll solve your problem. Does the -w option help? I'm guessing it won't, since the truncation seem to be due to some odd character causing an EOF or something (I tried myself here, both on Linux OS X, without problems). Evert I accidentally sent this directly rather than to the list: Thanks for your reply. When I said 158 characters I was trying to say each _line_ of the shell command ps ax was truncated to 158 characters, not that the _total_ returned was 158. Your question got me thinking about it, and I found in my set variables: COLUMNS=158, which corresponds pretty well. So, I tried setting COLUMNS equal to 500 (arbitrarily large) prior to going into python. It seems to change back to 158 automatically however. For example, when I go into python, import commands, and execute commands.getoutput('set') I find that COLUMNS is back to 158. So, I think my problem is that I don't know how to alter the set variable so that it will stick long enough for the ps ax command to execute properly. == End of forwarded message part. Finally, I have solved the problem, because I discovered a width option on the ps command, which I hadn't been aware of before. For example: commands.getstatusoutput('ps ax -l --width 500') works very well by over-riding any defaults. Thanks for your help. ds ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] commands module (Forwarded back to list)
So, I tried setting COLUMNS equal to 500 (arbitrarily large) prior to going into python. It seems to change back to 158 automatically however. For example, when I go into python, import commands, and execute commands.getoutput('set') I find that COLUMNS is back to 158. So, I think my problem is that I don't know how to alter the set variable so that it will stick long enough for the ps ax command to execute properly. I've had this problem before (setting/altering shell variables when executing a command from Python, albeit using os.system() instead). What I'd use is: commands.getoutput('COLUMNS=500 ps ax') or similar. In your case, you've been able to solve in another way (and better, since environment independent, though still shell/ps- variant dependent), but sometimes one may need to set/change shell variables, like paths to dynamic libraries. The above should work (for more variables, just specify them all before the actual command, whitespace separated). ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] commands module
Evert Rol wrote: So, I tried setting COLUMNS equal to 500 (arbitrarily large) prior to going into python. It seems to change back to 158 automatically however. For example, when I go into python, import commands, and execute commands.getoutput('set') I find that COLUMNS is back to 158. So, I think my problem is that I don't know how to alter the set variable so that it will stick long enough for the ps ax command to execute properly. I've had this problem before (setting/altering shell variables when executing a command from Python, albeit using os.system() instead). What I'd use is: commands.getoutput('COLUMNS=500 ps ax') or similar. In your case, you've been able to solve in another way (and better, since environment independent, though still shell/ps-variant dependent), but sometimes one may need to set/change shell variables, like paths to dynamic libraries. The above should work (for more variables, just specify them all before the actual command, whitespace separated). Thank you for mentioning that. I'm pretty weak on bash, and had monkeyed around with such things as set COLUMNS=500 on the previous line in the shell program version, but hadn't actually looked up that syntax yet, when I stumbled across the ps parameter that eventually I selected. Thanks again. ds ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor