LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   See if a program is already running in a script (https://www.linuxquestions.org/questions/linux-desktop-74/see-if-a-program-is-already-running-in-a-script-521096/)

tytower 01-20-2007 02:33 AM

See if a program is already running in a script
 
I have the following script in $HOME/.kde/autostart

#/bin/bash
if [ pidof -s kmail ]
then echo "KMail is running"
else
kmail
fi


if [ pidof -s firefox ]
then echo "Firefox is running"
else
firefox
fi


This is to start Kmail and Firefox on login

Is there any other way or a better way of doing this ?

How do I get a script to run when a user logs out?

druuna 01-20-2007 04:57 AM

Hi,

The autostart script needs a little work, but basically it is correct.

Things that need changing:

1) You need a valid hashbang.
This: #/bin/bash should be #!/bin/bash. The ! is missing in your example.

2) You need an ampersand (&) after the kmail and firefox 'start' command.
This: kmail should be kmail &. Same for the firefox line.
If you do not do this, the script will wait until the started program stops before continuing. If, for example, both kmail and firefox are not running your version of the script will start kmail and wait until kmail is stopped before it checks if firefox is running.

Here's a better version of your script:
Code:

#!/bin/bash
if [ pidof -s kmail ]
then
  echo "KMail is running"
else
  kmail &
fi

if [ pidof -s firefox ]
then
  echo "Firefox is running"
else
  firefox &
fi

About the running a script during logout question: Bash (I assume you use bash) already has a file to do that: .bash_logout
In general this file is not present, but you can create it and fill it with commands you want to execute during a logout.
This file is user specific, so you must create it for all users that need to execute something during logout.

The details can be found in the bash manpage.

Hope this clears things up a bit.

tytower 01-21-2007 03:15 PM

Thanks

Do you have any idea how I would get them to start on different desktops rather than both starting on desktop one at the same time?

druuna 01-21-2007 04:22 PM

Hi,

Quote:

Do you have any idea how I would get them to start on different desktops rather than both starting on desktop one at the same time?
That all depends on the desktop environment you are using and/or the program involved.

unSpawn 01-21-2007 05:27 PM

Two additions.
if [ pidof -s firefox ]
Doesn't work on my box even tho FF is running, however "pgrep" does. I prefer using pgrep because you can use simple globbing (which you AFAIK with "pidof" can't) and select by say SID's which comes in handy when you want to kill child processes.


About the running a script during logout question: Bash (I assume you use bash) already has a file to do that: .bash_logout
If you run X then, depending on your DM (say gdm) you have a /etc/X11/gdm/PostSession/Default script in which you can kill stuff after a user logged out. You also have /etc/X11/gdm/PreSession/Default for starting stuff.

tytower 01-22-2007 02:15 PM

Thanks - I thought something wasn't working with the script
Below is printout from terminal which shows in fact pid did not identify firefox was even running which it was

tytower@1[~]$ pgrep -u tytower kmail
5339
tytower@1[~]$ pidof -s firefox
tytower@1[~]$ pgrep -u tytower firefox
3785
tytower@1[~]$


this is the pid aux line returned that is relevent
tytower 3785 6.6 17.3 108616 42868 ? Sl 06:02 1:14 /usr/lib/firefox/firefox-bin -a firefox
it looks like pid will get it if just the program name is stored but if the path to the program is stored also it does not return the pid unless you put the whole matching string(?), so pgrep it is I think

I'm using kde so it has an autostart folder but not an autoclose that I can see
Kde uses Pager but I haven't found how to start on other pages yet

tytower 01-27-2007 01:26 AM

"Kde uses Pager but I haven't found how to start on other pages yet"

Iv'e progressed a little further and found a lead or two but cannot yet complete the commands

# xwininfo -name "Local Folders/inbox - KMail" | grep "KMail"
this finds the KMail window if it is running but I don't know how to start a new window
for Firefox

# dialog --msgbox "KMail is running already tried to start again from script" 10 30
this will put a box in the terminal window - I don't know how to make it appear on the desktop
or give it priority with a window on top of everything else

Anyone who can help please jump in!


All times are GMT -5. The time now is 04:18 PM.