LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (http://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Limit the number of times an application can be opened? (http://www.linuxquestions.org/questions/linux-desktop-74/limit-the-number-of-times-an-application-can-be-opened-4175455635/)

w00dy101 03-26-2013 06:12 AM

Limit the number of times an application can be opened?
 
Hi Guys

I'm new here and fairly new to Linux as well :)

anyways... skipping to the point now...

I need to be able to limit a desktop users ability to open Chrome on the desktop!

We run a web application off of chrome, and what our users seem to be doing is opening the chrome application multiple times each time a new user mans the workstation.

This is creating issues for the software and I need to figure out a way to to prevent this from happening...

Any ideas? does something exists built in to limit the number of times programs can be run?

Would some sort of bash script solve the problem?

I would be keen to see what you guys think!

Thanks in advance

goumba 03-26-2013 06:43 AM

A rather simplistic script to ensure a program is not started if it is already running:

Code:

#!/bin/sh

PROCESSNAME="changeme"

if [ ! -z "$PROCESSNAME" ] && [ ! -z "`ps -e | grep $PROCESSNAME`" ]
then
        # Replace the echo statement below with the desired command(s)
        echo $1 is not running
fi

Beware this an be a little tricky. You must know the process name as it shows in the process list, not search for the binary name. For example: under Debian, Firefox is known as Iceweasel, but ps -e lists the process as "firefox-bin". Firefox's process name under Mint is simply "firefox"

Also note that if you only use parts of the process name, it will evaluate as true. Of course this can be taken care of with a little awkery.

Also in my original test script, I used a parameter passed to the script rather than $PROCESSNAME, which would make this more generic, and possibly more useful. Simply delete the line "PROCESSNAME=" and change $PROCESSNAME to $1 in the conditionals.

Habitual 03-26-2013 07:51 AM

Code:

if $(pgrep chro > 0) ; then pkill -9 chro ; else echo "False" ; fi
works here.

Have fun!

w00dy101 03-26-2013 09:30 AM

Thank you both!

I will explore these options and adapt them to my needs.

Habitual 03-26-2013 05:39 PM

No worries, I enjoyed the challenge. ;)

w00dy101 03-27-2013 08:56 AM

Thanks again

The script below solved the problem.

Code:

#!/bin/bash

PROCESSNAME="chrome"
PROCID=`pgrep $PROCESSNAME`

        if [ ! -z "$PROCESSNAME" ] && [ ! -z "`ps -e | grep $PROCESSNAME`" ]
                then
                zenity  --warning --text="Chrome is already running (PID: $PROCID)"
                        if [ "$?" -eq "0" ]
                                then
                                zenity --question --text="Do you want to start a new session? This will close all existing Chrome Sessions"
                                        if [ "$?" -eq "0" ]
                                                then
                                                kill -9 $PROCID &&
                                                google-chrome --Kiosk
                                        fi
                        fi
        else
        google-chrome --Kiosk
        fi

Created a custom menu launcher, used the Chrome Icon for it, now the users cant open tens of thousands of Chrome apps!

Habitual 03-27-2013 07:51 PM

Good Job. I love zenity/yad stuff. :)


All times are GMT -5. The time now is 12:29 PM.