Jump to content
Science Forums

Basic Python Script Question


Recommended Posts

Hi all,

 

I wrote this following little code to run at startup. When I run it from the terminal it all works fine (but there is no point of the program (or better only 2/3, since I use it to open 3 programs among which a terminal)), but when I run it as executable (i.e double click and select run) the first time it opens only a terminal and only the second time it opens rhythmbox firefox and another terminal window.

 

As to the reason for this script I just started teaching myself python and though a good way to see how scripts work is actually to write one from scratch... (I know that most likely this could be done in an easier way just in bash, but easy is not the scope:-) )

 

Also, obviously it never greets me when run by double click (no screen to print 'hello' to)

#! /usr/bin/env python

print 'hello'
import os,subprocess
from subprocess import Popen, PIPE

os.system('gnome-terminal')
os.system('/usr/bin/rhythmbox &')


#-b shows the whole output from top not only that fitting to terminal window
#-n1 no refreshing of top just run once

proc = subprocess.Popen(["top -b -u sandros -n1 "], shell=True, stdout=PIPE) 
topout=[""]
for line in proc.stdout:
  # print line
   topout.append(line)

i=0
for item in topout:
   if item.find("firefox") != -1:
       print "firefox is running"
       i=1
       break# stop loop if firefox found

#    else:
if i==0:
       os.system('firefox')

   

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...