Need some help with pre and post scripts.


edgex004

Advanced Member
Joined
Jan 5, 2008
Messages
1,219
I have been working for a while to get a pre and post script working that will shutdown QJoyPad as I start the PND, and then turn it back on as I quit, but only if it was running in the first place.

This is what I have written:

 PND_pre_script.sh


#!/bin/bash

if [ ! "$EXENAME" = "../../../usr/bin/links" ]; then

# Stop Qjoypad


killall qjoypad
KILLMARKER="$?"
QJOYKILLED="/home/edged/joykilled"
if [ "$KILLMARKER" == "0" ]; then
echo "YES" > $QJOYKILLED
else
echo "NO" > $QJOYKILLED
fi

fi 
PND_post_script.sh


#!/bin/bash

if [ ! "$EXENAME" = "../../../usr/bin/links" ]; then

# Restart Qjoypad

QJOYKILLED="/home/edged/joykilled"
KILLMARKER=`cat $QJOYKILLED`
if [ "$KILLMARKER" == "YES" ]; then
notify-send "QJoyPad Started"
echo "NO" > $QJOYKILLED
nohup pqr qjoypad > /dev/null 2>&1 &
fi

fi

At the moment, the scripts seem to run fine on their own, but when they are run by the PND, they have some funny behavior.

The behavior looks like this:

1. Run QJoyPad

2. Run PND with the aforementioned pre and post scripts. Qjoypad is killed.

3. Exit PND, and QJoyPad starts again.

4. Run PND again, and now QJoyPad is both killed and restarted prior to PND launch. **Notification from the post script is once again displayed prior to the PND starting.**

5. Exit the PND again, and now QJoyPad is not restarted.

Adding an "exit" line after "nohup pqr qjoypad > /dev/null 2>&1 &" will cause the first run to work okay, but then the second run will have error number 4, but then QJoyPad will continue to run during and after the PND is run.

I suspect that for some reason, launching a separate PND in the background from within a post script causes that post script to run again prior to the launch of other PNDs. Or something.

If the scripts are assigned to multiple PNDs, you can run first.pnd just fine, but then the second.pnd will exhibit the incorrect behavior if it is run afterward.

Honestly I have been pouring over this and just can't figure out what is going wrong. I know my methodology is crummy, but I've just been trying various different methods to see if I can get it to work before I try to make the code have decent error handling.

Any help would be greatly appreciated.
 
Last edited by a moderator:
The root cause: when you kill qjoypad from a startup process of another PND, the system gets confused and executes the post script of your PND as a post script for qjoypad.

The quick fix:


#!/bin/bash

if [ ! "$EXENAME" = "../../../usr/bin/links" ]; then

# Stop Qjoypad

killall qjoypad
KILLMARKER="$?"
QJOYKILLED="/home/edged/joykilled"
sleep 1
if [ "$KILLMARKER" == "0" ]; then
echo "YES" > $QJOYKILLED
else
echo "NO" > $QJOYKILLED
fi

fi

Sleeping allows the post script to run harmlessly, before you put "YES" to your magic file.

Another solution would be to modify the post script itself, adding a condition statement detecting it is run by qjoypad. I'll leave the implementation out of the topic however.
 
Last edited by a moderator:
There's someting else with this post script of yours: PNDs using it will never be dismounted from /mnt/utmp. I don't have time to investigate the exact cause, but I bet it is related to the fact that you start one PND from inside of another, and that PND (i.e. qjoypad) keeps running when the main PND is terminated.
 
Thank you so much! That is one of the theories I had about the cause, but couldn't think of what to do. Your ideas are exactly what I needed! I also noticed the issue of not unmounting the /mnt/utmp/, but wasn't sure what to do about that either.


I will also look into a modification that tries to manually unmount /mnt/utmp/, but that will probably be harder.


Thanks again.
 
Last edited by a moderator:
2 options here :

1) before killing qjoypad, cd into its /mnt/utmp directory (so the PND dont get umounted). to restart it, make sure you're still in qjoypad umtp directory, and start it from there (not using pnd_run).

2) cd somewhere outside of your utmp directory (like /tmp) and start the qjoypad pnd using nohup
 
I believe the qjoypad being restarted was an unsedirable side-effect. The second suggestion is spot on target though.

Manually unmounting the directory from post script would probably not be possible at all, because pnd_run process (the one which executes your script) would still own that directory.
 
Last edited by a moderator:
Yes the restarting of QJoyPad while the host PND is running was not desired. It should remain off until the PND is exited. I think these suggestions all make perfect sense and I will let you know how they work.


Once I get these working, I will try to figure out a way to implement them for Pandafe as well, as it does not seem to support the pre and post scripts out of box, but I think I have an idea for a work around.
 
Last edited by a moderator:
They work!! Exciting!

Here are the final scripts:

 PND_pre_script.sh

Code:
#!/bin/bash

if [ ! "$EXENAME" = "../../../usr/bin/links" ]; then

# Stop Qjoypad


killall qjoypad
KILLMARKER="$?"
QJOYKILLED="/home/edged/joykilled"
sleep 1
if [ "$KILLMARKER" == "0" ]; then
  echo "YES" > $QJOYKILLED
else
  echo "NO" > $QJOYKILLED
fi

fi 
PND_post_script.sh
Code:
#!/bin/bash

if [ ! "$EXENAME" = "../../../usr/bin/links" ]; then

# Restart Qjoypad

QJOYKILLED="/home/edged/joykilled"
KILLMARKER=`cat $QJOYKILLED`
if [ "$KILLMARKER" == "YES" ]; then
  notify-send "QJoyPad Started"
  echo "NO" > $QJOYKILLED
  cd /tmp
  nohup pqr qjoypad > /dev/null 2>&1 &
fi

fi
Thanks again. I will let you know if I get them working with Pandafe as well.
 
Last edited by a moderator:
Back
Top