Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have seen similar questions here, but I didn't get my answer. Maybe it's because I am new to all this and just don't understand. I want my app to work mostly as an indicator. And if user would start it again it would check if it is already running, if it is then give all the input data to that process and quit.

  • So first I need to check if it is running. I saw the answer where you can make a file witch when the program starts and then check if it exists... But what if someone would delete it? Can't I just ask the OS if there is process named "myApp" or something?
  • The next thing I don't really get is how to communicate with the process. How do I give it the input data and what is it going to do with it? Does it work just like starting a new app, through the main() method?

I am trying to create this using Quickly. So it would be nice if you can give me some python examples or link to something like that.

share|improve this question
    
P.S. I already know that I can get the information about process through command line with something like this ps -A | grep myapp but how do I do that in program? –  Viesturs Jul 21 '12 at 12:34
    
You could always have it listen on a dbus interface. Then when it tries to bind to that if it runs a second time, it can bail out and you know it's already running. Just an observation. –  hbdgaf Jul 21 '12 at 12:42
    
OK I was looking at this question a bit wrong. What I actually needed was not the process but the DBus. DBus allows programs to communicate at run time and as @aking1012 suggested from there I can find out if the program is actually running. It is all described here dbus-python tutorial. In short: First I need to export object to dbus, so it would be available and then I can call it's methods from my application over dbus and pass whatever variables I want. It is all written in given tutorial. –  Viesturs Jul 21 '12 at 16:05
    
Maybe it would be wise to use Stackoverflow and then for Linux specific stuff, Unix & Linux? –  hexafraction Jul 22 '12 at 19:53
    
I already found my answer and submited it down below :) –  Viesturs Jul 22 '12 at 20:00

2 Answers 2

Actually there is a package named as python-psutil for giving the process information using python codes .

you can get the package at here http://packages.ubuntu.com/lucid/python-psutil

There is one more helpful package to you named as PSI

PSI is a Python package providing real-time access to processes and other miscellaneous system information such as architecture, boottime and filesystems. It has a pythonic API which is consistent accross all supported platforms but also exposes platform-specific details where desirable.

More information here : https://bitbucket.org/chrismiles/psi/wiki/Home

one more link : http://stackoverflow.com/questions/2703640/process-list-on-linux-via-python

share|improve this answer
    
Thank You for your answer! OK now I understand how to find a process. But I still don't get how to pass the input data to that process. As far as I understand it has to do with signals. –  Viesturs Jul 21 '12 at 12:57
    
you mean you want to do some operations to that process or something else ? –  AgentCool Jul 21 '12 at 13:03
    
I want to give the input data from the command line to the process. Then the process should react - if there is actually something given, it should check if it is valid and execute the program for it, if nothing is given it should open the main window of the program. –  Viesturs Jul 21 '12 at 13:06
1  
OK, but your answer was really helpful :) –  Viesturs Jul 21 '12 at 13:10
1  
Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference. Imagine the linked site goes down for maintenance or forever - your answer would become worthless. –  hexafraction Jul 22 '12 at 19:54
up vote 1 down vote accepted

I found out that I really needed DBus to achieve what I needed. So here is what I actual needed to do:

  1. Check if my service is on dbus
  2. If it is pass all my input variables to it and exit
  3. if it isn't create my dbus service and start my program
    in Python it would look something like this:


# using quickly...  
#     
#   __init__.py  
# # # # # # # # # # # # #  
import dbus  
import sys  
from gi.repository import Gtk  
# import whatever else you need...  

from my_app import MyAppDBusService
# import whatever else from your app...

def main():
    bus = dbus.SessionBus()

    # Check if my app is running and providing DBus service
    if bus.name_has_owner('com.example.myApp'):
        #if it is running pass the commandline variables to it and exit

        #get my service from DBus
        myService = bus.get_object('com.example.myApp', '/com/example/myApp')
        #get offered method from DBus
        myMethod = myService.get_dbus_method('my_method', 'com.example.myApp')
        #call the method and pass comandline varialbes
        myMethod(sys.argv)
        #exit
        sys.exit(0)
    else:
        #if not running
        #run my DBus service by creating MyAppDBusService instance
        MyAppDBusService.MyAppDBusService()

        #do whatever with sys.argv...
        #...

        Gtk.main()

# MyAppDBusService.py
# # # # # # # # # # # # # #

import dbus
import dbus.service
from dbus.mainloop.glib import DBusGMainLoop
#import whatever else you need...

# use the dbus mainloop
DBusGMainLoop(set_as_default = True)

class MyAppDBusService(dbus.service.Object):
    def __init__(self):
        # create dbus service in the SessionBus()
        dbus_name = dbus.service.BusName('com.example.myApp', bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, dbus_name, '/com/example/myApp')

    # offer a method to call using my dbus service
    @dbus.service.method('com.example.myApp')
    def my_method(self, argv):
        #do whatever with argv...
share|improve this answer
    
I have no idea how to fix indents in this hope someone finds this useful :) –  Viesturs Jul 22 '12 at 19:51
    
Please paste your code as-is, don't add any br tags or formatting, then highlight it all in the text box on the edit page, press Ctrl+K and save. This may help your indent issue. –  hexafraction Jul 22 '12 at 19:55
    
whell if I just paste my code it looks awful. Some text is really big some parts it understands as code - it is weird. –  Viesturs Jul 22 '12 at 20:13
1  
Please paste it, then select it and press Ctrl+K or click the <?> code icon on the editing toolbar above the input box once you have highlighted the code. –  hexafraction Jul 22 '12 at 20:14
    
I did that, but it didn't entirely work, so this is as good as i could get it. –  Viesturs Jul 22 '12 at 20:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.