LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-13-2002, 08:32 AM   #1
Bert
Senior Member
 
Registered: Jul 2001
Location: 406292E 290755N
Distribution: GNU/Linux Slackware 8.1, Redhat 8.0, LFS 4.0
Posts: 1,004

Rep: Reputation: 46
launching a process if it doen't exist (Folding@HOME)


Hey!

This is really more of a BASH issue than a Folding@HOME one:

I'd like to launch Folding@HOME like this:

./FAH3Console.exe > /dev/null &

so that it runs in the background and drops all the output.

I'd like to extract the PID to a file .pid file, and delete the file when the process stops. How do I do that?

so:

if [ -ne $HOME/Folding/Console.pid ]; then
<launch FAHConsole>
fi

but also when the process stops rm Console.pid to satisfy this condition. This way multiple logons will not launch multiple processes.

There's a simple answer there somewhere I think - can anyone give any clues?

Thanks
Bert
 
Old 11-13-2002, 09:29 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Since you've got +500 posts here's a clue. Call the script something easy like "fahctl" and run it like "sh -x <filename>" to see if it suits your purposes and works properly.

No Bash manuals where hurt during this exercise, void where probibited by shell/law, no funds returned after running, warranty void before publication, no tinkering on premises and YMMVVM as usual.

#!/bin/sh
# warning: nasty obfuscation and array abuse ahead :-]
pid=($(/sbin/pidof FAH3Console.exe) $HOME/Folding/Console.pid $HOME/Folding/FAH3Console.exe "FAH3Console: ")
msg=(echo "already running" "stopped but pidfile exists" "already stopped" "running but pidfile went limbo" "$(basename $0) [start|restart|stop]" stopped)

s() { if [ -f "${pid[1]}" -a "${pid[0]}" != "" ]; then ${msg[0]} ${msg[1]}; exit 0; fi; if [ -f "${pid[1]}" -a "${pid[0]}" = "" ]; then ${msg[0]} ${msg[2]}; rm -f ${pid[0]}; fi; ${pid[2]} > /dev/null &; # if it doesn't put it's PID in $pidf, use: ${pid[0]} > $pidf; }

k() { if [ ! -f "${pid[1]}" -a "${pid[0]}" = "" ]; then ${msg[0]} ${msg[2]}"; exit 0; elif [ -f "${pid[1]}" -a "${pid[0]}" = "" ]; then ${msg[0]} ${msg[1]}; rm -f ${pid[1]}; exit 0; else if [ ! -f "${pid[1]}" -a "${pid[0]}" != "" ]; then ${msg[0]} ${msg[4]}; kill -s TERM "${pid[0]}"; else kill -s TERM "${pid[0]}"; rm -f ${pid[1]}; ${msg[0]} ${msg[6]}; fi; fi; }

${msg[0]} -n "${pid[3]}"
case "$1" in
start) s;;
stop) k;;
restart) $0 stop; $0 start;;
*) ${msg[0]} ${msg[5]} ; exit 1
esac

Last edited by unSpawn; 11-13-2002 at 09:31 AM.
 
Old 11-13-2002, 01:17 PM   #3
Bert
Senior Member
 
Registered: Jul 2001
Location: 406292E 290755N
Distribution: GNU/Linux Slackware 8.1, Redhat 8.0, LFS 4.0
Posts: 1,004

Original Poster
Rep: Reputation: 46
... that looks more like a perl script!

Your answer is a thing of beauty and wonderment, unSpawn. I tried running it and just like most perl scripts, I'm still not sure what it does.

I added the following lines to my ~/.bash_profile:

Code:
if [ -e $HOME/Folding\@HOME/FAH.pid ]; then
        echo "";
        echo "Folding@Home client is already running";
else
        $HOME/Folding\@HOME/FAH3Console-Linux.exe > /dev/null &
        /sbin/pidof -s FAH3Console-Linux.exe > $HOME/Folding\@HOME/FAH.pid
        echo "";
        echo "Folding@Home client has started"
fi
Then I added the following lines to my ~/.bash_logout:
Code:
/sbin/kill -9 pid $($HOME/Folding\@HOME/FAH.pid);
rm -rf $HOME/Folding\@HOME/FAH.pid;
Which appears to do the job.

It was the pidof command I didn't know about.

Thanks
Bert
 
Old 11-13-2002, 02:45 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Thnx, I'm trying to raise my Bash scripting mana so I can't shun writing scripts. Heh, no. On second thought I'd consider myself having a lucky case of CSD or Compulsory Scriptwriting Disorder... Which is way better than having a MTD: Microsoft Transmitted Disease :-]

Btw[0], you only check for pidfile existence (-e) but this app could leave a pidfile and not be running right?
Btw[1], if you ran the script like I said as "sh -x <filename>" You'd see *exactly* what it does. It ain't a mistery.
 
Old 11-13-2002, 03:02 PM   #5
Bert
Senior Member
 
Registered: Jul 2001
Location: 406292E 290755N
Distribution: GNU/Linux Slackware 8.1, Redhat 8.0, LFS 4.0
Posts: 1,004

Original Poster
Rep: Reputation: 46
Yes, it's an optimisation prob.
If another user logs on and the .pid file exists, it won't generate a new process. But when the original owner logs off, the process stops and there's no Folding process running anymore. Hmmm.

I'm thinking of changing ownership of a global file, so that the most recent user to login gets ownership of FAHowner.id file. When the user exits, check if they own the file. If they do, kill the process. If they don't leave it running.

This scenario doesn't cater for multiple logins from a single user though. Aaagh!

The other option is of course just to have multiple processes (one for each user) but then ... well, there's not much point in having this script. Hmmm.

I'll sleep on it.

Thanks
Bert
 
  


Reply



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
Folding@Home jdblick SUSE / openSUSE 5 06-23-2005 07:52 AM
folding@home amosf General 1 02-19-2005 12:03 AM
Turn off folding@home Kensai Linux - General 0 08-01-2004 03:12 PM
Folding@Home Team jon1591 LQ Suggestions & Feedback 14 06-26-2004 10:10 AM
HELP! my /root/DCOPserver_myhostname:_0 doen't exist! totalcommand Linux - Newbie 4 10-08-2001 07:00 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:41 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