LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   launching a process if it doen't exist (Folding@HOME) (https://www.linuxquestions.org/questions/linux-software-2/launching-a-process-if-it-doent-exist-folding%40home-35368/)

Bert 11-13-2002 08:32 AM

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

unSpawn 11-13-2002 09:29 AM

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

Bert 11-13-2002 01:17 PM

... 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. :D

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

unSpawn 11-13-2002 02:45 PM

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.

Bert 11-13-2002 03:02 PM

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


All times are GMT -5. The time now is 01:42 AM.