1

I want to run a python script that produces a desktop notification on start-up in Ubuntu 12.04. How can I do this?

I tried some common ways. On start-up in the "ps aux" command my program is listed, but no graphic interface is seen. Normal scripts run, but scripts with graphic/network interface pose a problem.

  • My bash script is essentially to run a Python script that echoes a system command.
  • If I manually initiate the program later, it runs fine.

import codecs
import time
import urllib2
import os
st = "NULL"
date = time.strftime("%d-%m")
try:
    st = urllib2.urlopen("XYZ.html").read()
except:
    pass
if st != "NULL":
    st.split("XYZ")[0].split("</table>")[0]
    if date in st:
        new = st.split(date)[1:]
        events = ""
        for j in new:
            events += (j.split("</td><td>")[1].split("</td></tr>")[0] + "\n")
        events = events[:-1]
    else:
        events = "Nothing special"
    to_send = "notify-send -i A/B/C.ico " + "'Calender " + date + "-2015' '" + events +"'"
    os.system(to_send)
else:
    to_send = "notify-send -i A/B/C.ico " + "'Calender " + date + "-2015' 'Check Internet connection'"
    os.system(to_send)
exit(0)
  • Maybe you could add, what you tried to start it automatically? – Jakob Lenfers Sep 6 '15 at 5:56
  • Please post the command / program you run, that works "normally". – Jacob Vlijm Sep 6 '15 at 8:05
  • Sorry for the delay. Can you please help me out now? It is not an efficient code though. – PS Srinivasan Sep 10 '15 at 4:33
  • Edited the title a bit, since "Graphic script" might give the wrong impression of what the question is about. Feel free to revert if you don't agree. – Jacob Vlijm Sep 10 '15 at 6:14
  • Hi PS, did you manage? Please let me know. – Jacob Vlijm Sep 10 '15 at 18:56
1

Running commands on log in

This is typically one of these cases where a command breaks if it runs too soon in the log in process. The desktop is not fully loaded yet ans the command cannot be run.

Add to your Startup Applications a break before the script starts, and it will run fine:

enter image description here

/bin/bash -c "sleep 15 && python2 /path/to/script.py"

I tested your script both with and without the break, and indeed failed if I ran it without, but succeeded with the break.

A few remarks

  • Be aware that if you don't use the shebang (#!/usr/bin/env python) you must precede the path to the script by python to run it. No matter if it is executable or not.
  • On slower systems (or on a VM) , the break of 15 seconds might not be sufficient. If so try to start with a relatively long break, experimenting "down" to the limit.

  • About the script; didn't really look into it, but I definitely wouldn't use os.system any more, use subprocess.Popen() instead, and oh, you can save a line by removing the first os.system(to_send) and un- indent the second one, since you will run the command in both cases after the if / else route . :)

  • @PSSrinivasan I am on my phone and quite handicapped... But you should simply use Startup Applications (from Dash)!! – Jacob Vlijm Sep 11 '15 at 13:49
  • Works like a charm! Thanks a lot. This actually means a lot to me. Know what! I actually before posing my question here tried to put a delay thinking that the network adapter takes time to start. But the problem was I put the delay in the python program and in the "crontab -l" code. And it didn't work. – PS Srinivasan Sep 11 '15 at 13:54
  • Sure.. Sorry I did not do it earlier. I am new here. Thanks again – PS Srinivasan Sep 11 '15 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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