Ubuntu – How to show a notification if a startup application (or process) failed to start automatically

autostartloggingnotificationscripts

I added a script to autostart (using Default applications for LXSession). Later, I moved the folder containing the script.

As result, the autostart command was silently failing.

How can I enable warning messages about failed autostart commands?

Best Answer

The solution below is not specifically for Lubuntu, only the way to set an application to autostart from GUI is a bit different, since Lubuntu does not have the Startup Applications application, like Ubuntu.

Check if a script has started within a certain time limit from login

With the script below, you can check if a script started successfully, within a certain time limit from login. If the startup(s) was unsuccessful, a message will appear, mentioning which of the scripts did not start successfully. You can use it to check on multiple scripts or processes in one step.

enter image description here

After the time limit has passed and sent its message, the script terminates

How to use

  1. Copy the script below into an empty file, save it as proc_check.py in a permanent :) location.
  2. In the head of the script, set the list of processes (script names) to keep an eye on, and set the time limit.
  3. Make sure the processes (to check) are not running, and test-run the script with the command:

    python3 /path/to/proc_check.py
    

    It should send a warning after the time limit has passed.

  4. If all works fine, add the command below to your Startup Applications:

    python3 /path/to/proc_check.py
    

Note:

If you have notify-send available (sudo apt-get install libnotify-bin), you can also uncomment the before-last line, to get a confirmation if all went well:

enter image description here

The advantage of that is that you will either get a notification that all went well, or a warning, mentioning which of the processes did not start up successfully.
You will notice then if you accidentally moved this script. :)

The script

#!/usr/bin/env python3
import subprocess
import getpass
import time

#--- set the processes (script names) to check below
procs = ["pscript_2.py", "monkey.py"]
#--- set the time limit below (seconds)
wait = 30
#---

# define the user to fetch the current user's process list
user = getpass.getuser()
# create an (empty) list of succesful process startups
succeeded = []

def get():
    return subprocess.check_output(["ps", "-u", user, "ww"]).decode("utf-8")

t = 1
while t < wait:
    # add succesful processes to the "succeeded" list
    for p in [proc for proc in procs if proc in get()]:
        succeeded.append(p)
    time.sleep(1)
    t = t+1

# list the failures
fails = [p for p in procs if not succeeded.count(p) > 2]
# if there are any, send a message
if len(fails) > 0:
    subprocess.Popen(["zenity", "--info", "--text", "failed to run: "+(", ").join(fails)])
# if all was successfull, send a message; n.b. comment out the line if notify-send is not available
else:
    # subprocess.Popen(["notify-send", "All processes started succesfully"])
    pass
Related Question