Check GUI Program Is Running In Linux

Overview

I have create a bash script to:

  1. Check if the program’s process is running
  2. If so just quit the script
  3. If not start up the program

Using this script and crontab it allows the script to be ran every x time checking to make sure the program is running.

Setup

Log into the user who will / has permission to run the script as well as the program. I shall use Azureus / Vuze as my example since this was the program I wrote it for. I also assume that you are using the downloaded version and not the one from the repository.

Script

Create an empty file. I shall use $home as in your user directory e.g /home/danny Paste the following script into the file:

#!/bin/bash
PROGRAM=’azureus’

if ps ax | grep -v grep | grep $PROGRAM > /dev/null
then
echo “$PROGRAM PROGRAM running, everything is fine”
exit
else
echo “$PROGRAM is not running”
echo “Starting $PROGRAM”
DISPLAY=:0.0 /home/danny/vuze/azureus &>/tmp/azureus.out

if ps ax | grep -v grep | grep $PROGRAM > /dev/null
then
echo “$PROGRAM PROGRAM running, everything is fine”
else
echo “Unable to start $PROGRAM”
fi
fi

myScript

Just to explain the script from top to bottom. If you are not interested skip this paragraph. The first line declares it’s a bash shell script and where bash can be found. PROGRAM is a variable holding the program name this can be changed to any other program. The if statement does a “ps” command which lists all the process running and doing other various pipes and filters down the list. If the program is found then it prints the message and exits. If it does not exist then it will print a message to say it’s not running and starting the program, The third line after the “else” statement is the command to start azureus. This can be changed to your relevant program. DISPLAY=:0.0 is only necessary if the program has a GUI. It runs the program what is usually the display on port 0, the one users usually see on the monitor. The remaining parts check if the program has started and prints the relevant message.

Save and close the file.

The script needs execute permissions. Run the following command to do this $ chmod +x /home/danny/myScript.sh

Crontab

crontab is the Linux scheduler. It does things based on time. There are two was to set this up.

GUI / Easy Way

Install gnome-schedule:
$ sudo apt-get install gnome-schedule

Once installed, go to System > Preferences > Scheduled tasks

Gnome Task Schedule

Click “New” from the toolbar and select “A task that launches recurrently”.

New Task

Give it a description and in the command field enter the path to the script. I untick the “No output” option so I can see any errors when we test the script later. Choose the appropriate “Time & Date” for often the script should be ran.

New Task Advanced

The problem with this method is the customizability of the schedule with every minute the smallest value. To enter a more suitable value click on the “Advanced” radio button and for every 10 minutes enter the */10 and leave all other boxes “*”(asterisk). Click the “Add” button to save the entry.

Highlight and click “Run Task” to test the script.

Command Line / Harder Way

Whilst logged into the user who will run the schedule edit the crontab: $ crontab -e

By default on Ubuntu Nano is the default editor. I use this template in crontab:
# +---------------- minute (0 - 59)
# | +------------- hour (0 - 23)
# | | +---------- day of month (1 - 31)
# | | | +------- month (1 - 12)
# | | | | +---- day of week (0 - 6) (Sunday=0 or 7)
# | | | | |

This should help you with entering values but to run the script every 10 minutes the line should be */10 * * * * /home/danny/myScript.sh

Save and exit crontab.

Summary

The script can be customized to check and run any program. Now hopefully any program crashes or if someone closes the program you want running it will automatically start it up and keep it up.

Cron job to start / stop Azureus – can not start azureus

CronHowTo – Ubuntu

Run crontab Every 10 Minutes

About Danny

I.T software professional always studying and applying the knowledge gained and one way of doing this is to blog. Danny also has participates in a part time project called Energy@Home [http://code.google.com/p/energyathome/] for monitoring energy usage on a premise. Dedicated to I.T since studying pure Information Technology since the age of 16, Danny Tsang working in the field that he has aimed for since leaving school. View all posts by Danny → This entry was posted in Linux and tagged , , , , , , , , . Bookmark the permalink.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.