(Solved) How to find out if Process/Program was exited?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

(Solved) How to find out if Process/Program was exited?

#1 Post by LazY Puppy »

Hi.

Idea is to call a function to unload all loaded sfs modules each and every time after a process has been exited/finished.

I need to find out, if a process/program has been exited during the running time of puppy. Want to do this in bash - if possible.

How could this be achieved?

Thanks
Last edited by LazY Puppy on Thu 14 May 2015, 00:48, edited 1 time in total.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#2 Post by seaside »

You might use "watch" like this.

Code: Select all

watch -n 1 "ps u -C <process_name>"
When watch terminates, it could call an SFS unload command.

Cheers,
s

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#3 Post by LazY Puppy »

seaside wrote:You might use "watch" like this.

Code: Select all

watch -n 1 "ps u -C <process_name>"
When watch terminates, it could call an SFS unload command.

Cheers,
s
Thanks a lot.

I thought already of using watch, but when I'm entering watch --help into a terminal it gives only to options to me -n and -t. So I'd modified your line (removed <process_name>) and it gave me lot of more options - so I could find -A, since I need to watch all processes, counting them and make a diff using number of processes between to calls.

Currently I'm making the diff of running processes between to calls of watch by echoing results of watch into a text file. Doing a backup of this text file before calling watch again and overwriting the results text file.

Then I'm reading both files (while read LINE etc) and making a diff of counted lines. If counted lines of the backup text file is greater, say number of processes have been decreased, unloading of SFS will start.

I will try to use 'grep -n' or 'wc -l' like don570 has suggested right now in the other topic, to make this 'while read LINE' stuff a little smarter.

---

Edit:

Code 'wc -l' did it and I used at last option -e when calling watch.

Now everything works as intended.
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#4 Post by musher0 »

Hello, Lazy Schmurf. ;)

"watch" takes constant energy. Simpler to consult the ps database.
If the process is there, it's there, and we do something;if the process
isn't, it isn't, and we do something else.

Something like:

Code: Select all

process="opera"
[ "`ps | grep $process`" = "" ] && echo "Oh, the $process is not there. Don't do anything!" ||  echo "Wow, the $process is ongoing. Do something!" 
:)

More precise consultation is done with awk, with or without the "!".

Code: Select all

ps | awk '$4 ~ /opera/
Good luck.

musher0
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

User avatar
LazY Puppy
Posts: 1934
Joined: Fri 21 Nov 2014, 18:14
Location: Germany

#5 Post by LazY Puppy »

process="opera"
[ "`ps | grep $process`" = "" ] && echo "Oh, the $process is not there. Don't do anything!" || echo "Wow, the $process is ongoing. Do something!"
I have tested this with some apps that are not installed and so not running. It returns all the time: "Wow, the ProcessHere is ongoing. Do something!"
ps | awk '$4 ~ /opera/
This complains a ---> missing `''

Sorry...

...both of them aren't of any use over here (bash 4.1)
RSH

"you only wanted to work your Puppies in German", "you are a separatist in that you want Germany to secede from Europe" (musher0) :lol:

No, but I gave my old drum kit away for free to a music store collecting instruments for refugees! :wink:

musher0
Posts: 14629
Joined: Mon 05 Jan 2009, 00:54
Location: Gatineau (Qc), Canada

#6 Post by musher0 »

LazY Puppy wrote:
process="opera"
[ "`ps | grep $process`" = "" ] && echo "Oh, the $process is not there. Don't do anything!" || echo "Wow, the $process is ongoing. Do something!"
I have tested this with some apps that are not installed and so not running. It returns all the time: "Wow, the ProcessHere is ongoing. Do something!"
ps | awk '$4 ~ /opera/
This complains a ---> missing `''

Sorry...

...both of them aren't of any use over here (bash 4.1)
Well, don't use them!

Thanks anyway for revising me.

The 1st command line contained an oversight. It should have read:

Code: Select all

process=opera;echo;[ "`ps | grep $process | grep -v grep`" = "" ] && echo "Oh, $process is not running. Don't do anything!" || echo "Wow, $process is running. Do something!";echo
(Edit illustrated as working below.)

The 2nd command line contained a typo. It should have read:

Code: Select all

ps | awk '$4 ~ /opera/'
Mea culpa for those errors.

However, I think that the bash version has nothing to do with it.

BFN.

musher0
Attachments
process_running_OR_not_2015-05-14.jpg
Corrected line now works when &gt; grep -v grep &lt; is added.
Sorry about the oversight.
(34.85 KiB) Downloaded 59 times
musher0
~~~~~~~~~~
"You want it darker? We kill the flame." (L. Cohen)

Post Reply