LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 01-20-2007, 02:33 AM   #1
tytower
Member
 
Registered: Jun 2006
Location: Oz
Distribution: Mandriva 10.0,10.1,10.2,2006,Mepis 6.0,Opensuse10.2,Puppy 2.14
Posts: 250

Rep: Reputation: 30
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?
 
Old 01-20-2007, 04:57 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 01-21-2007, 03:15 PM   #3
tytower
Member
 
Registered: Jun 2006
Location: Oz
Distribution: Mandriva 10.0,10.1,10.2,2006,Mepis 6.0,Opensuse10.2,Puppy 2.14
Posts: 250

Original Poster
Rep: Reputation: 30
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?
 
Old 01-21-2007, 04:22 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 01-21-2007, 05:27 PM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
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.
 
Old 01-22-2007, 02:15 PM   #6
tytower
Member
 
Registered: Jun 2006
Location: Oz
Distribution: Mandriva 10.0,10.1,10.2,2006,Mepis 6.0,Opensuse10.2,Puppy 2.14
Posts: 250

Original Poster
Rep: Reputation: 30
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

Last edited by tytower; 01-22-2007 at 02:30 PM.
 
Old 01-27-2007, 01:26 AM   #7
tytower
Member
 
Registered: Jun 2006
Location: Oz
Distribution: Mandriva 10.0,10.1,10.2,2006,Mepis 6.0,Opensuse10.2,Puppy 2.14
Posts: 250

Original Poster
Rep: Reputation: 30
"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!

Last edited by tytower; 01-27-2007 at 01:31 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Running another script from a gtk+ program shandy^^^ Programming 1 02-03-2006 10:53 AM
Bash Script - How do I make sure my program is still running? nro Linux - Newbie 4 08-28-2005 01:57 PM
Running shell script within a C or C++ program Quantum0726 Programming 2 06-15-2005 09:14 PM
running a program from shell script Suinatsa Programming 10 04-14-2005 11:25 AM
Running a program/script from an email smegware Programming 4 12-01-2004 11:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration