LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Script causes Ubuntu to hang on shutdown (https://www.linuxquestions.org/questions/linux-newbie-8/script-causes-ubuntu-to-hang-on-shutdown-848330/)

zensunni 12-04-2010 10:04 AM

Script causes Ubuntu to hang on shutdown
 
I've got a bash script that is started at startup and always runs in the background & is running on an infinite loop.

But, it makes the OS hang on shutdown.

Is there any simple way to eliminate the hang while keeping the script?

catkin 12-04-2010 10:11 AM

Quote:

Originally Posted by zensunni (Post 4180505)
But, it makes the OS hang on shutdown.

That should not be possible ... maybe unless it hangs the init runlevel change. How are you starting it? Try putting an & at the end of the command line that starts it to ensure that the script that calls it continues.

zensunni 12-04-2010 12:41 PM

Your suggestion worked, but I can't make sense of this:

If I add the "&" and shutdown AFTER starting it up without the "&", it won't hang.
And if I delete the "&" and shutdown AFTER starting it up with the "&", it will hang.

Shouldn't the script's edits take effect AFTER I shutdown and reboot?

..so weird. Anyways, the problem is solved, save for the sequential mysteries. Thanks for the help.

gd2shoe 12-04-2010 06:34 PM

We still don't know how you're starting your script.

Maybe the script is getting started during shutdown for some reason?

arizonagroovejet 12-05-2010 05:08 AM

Quote:

Originally Posted by zensunni (Post 4180652)
If I add the "&" and shutdown AFTER starting it up without the "&", it won't hang.
And if I delete the "&" and shutdown AFTER starting it up with the "&", it will hang.

You don't give any indication of where you are adding or removing the &, but I'm thinking the script call repeatedly calls itself. Is that the case? If so, the behaviour you describe possibly be eliminated by calling the script once and wrapping it in a do while
Code:

while true;do

you script here

done

It will probably help if you post both the script and the script which is calling the script, if there is one.

zensunni 12-05-2010 09:33 AM

Yes, I should definitely be more specific about this. There's a lot I left out.

Here's the scripts in question:

after_hours_root.sh
Code:

#!/bin/bash

for (( ; ; ))
do

  z_hr=`date +%H`
  echo $z_hr
  if [ $z_hr -gt 21 ] || [ $z_hr -lt 5 ]; then
        sleep 960
          /sbin/shutdown -h now
  else
        sleep 600
  fi


done

after_hours_user.sh
Code:

#!/bin/bash

export DISPLAY=:0.0;
export  XAUTHORITY=$(ps h $(pidof X) | awk '{print $11}');


for (( ; ; ))
do

  z_hr=`date +%H`
  if [ $z_hr -gt 21 ] || [ $z_hr -lt 5 ]; then
          /usr/bin/xmessage -button ok -center "Its too late to be on the computer. You have 15 minutes." &
        sleep 900
          /usr/bin/xmessage -button ok -center "1 minute to Shutdown" &
        sleep 600
  else
        sleep 600
  fi

done

hourly_reminder.sh
Code:

#!/bin/bash

export DISPLAY=:0.0;
export  XAUTHORITY=$(ps h $(pidof X) | awk '{print $11}');

for (( ; ; ))
do

  sleep 3600
  #sleep 3
  /usr/bin/xmessage -button ok -center "Hourly reminder to get off the computer"

done

(I made two files named hourly reminder: starter script & the above one.)

And here's the script that starts it:

hourly_reminder.sh
Code:

sleep 60

/bin/su - joe -c "/bin/bash /home/joe/Scripts/hourly_reminder.sh" &
/bin/su - joe -c "/bin/bash /home/joe/Scripts/after_hours_user.sh" &
/bin/bash /home/joe/Scripts/after_hours_root.sh &

The line at the end...
Code:

/bin/bash /home/joe/Scripts/after_hours_root.sh &
...is where I have been adding and subtracting the "&".

The starter script is in /etc/init.d/

This is what I did when I added the starter script:
http://embraceubuntu.com/2005/09/07/...run-at-bootup/

(I'm not sure if the script is run because it's in the init.d folder, or because I used the "update-rc.d <script> defaults" command as per the guide above.)

barriehie 12-05-2010 10:58 AM

Have you thought about rewriting your init script to be LSB compliant? I've pulled my hair out when the machine got hung on boot! :) Give this a try; it will, should, start/stop the involved scripts. This is the script that should go in /etc/init.d/nohangshutdownctl Here's a link to init scripts. It's from the Debain wiki but since you're using Ubuntu it should work. Pay attention to the paths; I habitually put system wide scripts in /usr/local/bin...
Code:

#!/bin/bash

### BEGIN INIT INFO
# Provides:          nohangonshutdownctl
# Required-Start:    $all
# Required-Stop:
# Default-Start:    2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: nohangscripts start/stop control file.
### END INIT INFO


case "$1" in
    start)
        echo "Starting hourly_reminder.sh."
        /bin/su - joe -c "/usr/local/bin/hourly_reminder.sh &"
        echo "Starting after_hours_user.sh."
        /bin/su - joe -c "/usr/local/bin/after_hours_user.sh &"
        echo "Starting after_hours_root.sh"
        /bin/su - joe -c "/usr/local/bin/after_hours_root.sh &"
    ;;
    stop)
        echo "Shutting down process'."
        killall -s 9 hourly_reminder.sh after_hours_user.sh after_hours_root.sh
    ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
esac
exit 0



All times are GMT -5. The time now is 10:25 AM.