User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

shutdown.cgi won't exit before shutdown

Sun Jul 31, 2016 9:39 pm

HTML redirect never happens.
I want a reboot button that goes to this cgi shell script. It should redirect to another page and then shutdown the computer.

Code: Select all

#!/bin/sh

echo "content-type: text/html\n\n"

echo "<html>"
echo "<head>"
echo "<title>please wait...rebooting</title>"
echo "<meta http-equiv='refresh' content='1;url=/tools/'>"
echo "</head>"
echo "<body>"
echo "please wait..."
echo "</body>"
echo "</html>"


# These two will shutdown the pi but HTML redirect does not happen

# shutdown -k now 2>&1 </dev/null &
# OR
# shutdown -k now

Antikythera

User avatar
DougieLawson
Posts: 39297
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: shutdown.cgi won't exit before shutdown

Sun Jul 31, 2016 10:12 pm

Your HTTP headers are wrong

Code: Select all

#!/bin/bash

echo -e "Content-type: text/html\n\n"
echo -e "<html><head>"
echo -e "<meta http-equiv=\"refresh\" content=\"1; url=/tools/\">"
echo -e "</head><body><h1>Redirect ...</h1></body></html>"
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: shutdown.cgi won't exit before shutdown

Sun Jul 31, 2016 10:56 pm

No, the refresh works with single quotes and double quotes.

I tried with both " and '.

Now without the shutdown command, the page will refresh/redirect. It waits for the shutdown command to finish.
To prove that, I did a shutdown -k now and it redirect/refreshes fine.

I tried putting the shutdown command in another sh script and it still hangs.

The script waits for shutdown to finish. But of course if shutdown finishes then apache won't work either.
Antikythera

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: shutdown.cgi won't exit before shutdown

Fri Aug 05, 2016 4:16 am

Here we go again:

This page runs big 'o emacs and redirects page:

Code: Select all

#!/bin/sh

echo "content-type: text/html\n\n"

echo "<html>"
echo "<head>"
echo "<title>please wait...rebooting</title>"
echo "<meta http-equiv=\"refresh\" content=\"1;url=/tools/\">"
echo "</head>"
echo "<body>"
echo "please wait..."
echo "</body>"
echo "</html>"

#sudo shutdown -r --no-wall now

sudo /usr/bin/emacs /tmp/hello &



This page runs reboot and fails to redirect page:

Code: Select all

#!/bin/sh

echo "content-type: text/html\n\n"

echo "<html>"
echo "<head>"
echo "<title>please wait...rebooting</title>"
echo "<meta http-equiv=\"refresh\" content=\"1;url=/tools/\">"
echo "</head>"
echo "<body>"
echo "please wait..."
echo "</body>"
echo "</html>"

sudo shutdown -r --no-wall now

#sudo /usr/bin/emacs /tmp/hello &
Antikythera

Heater
Posts: 16091
Joined: Tue Jul 17, 2012 3:02 pm

Re: shutdown.cgi won't exit before shutdown

Fri Aug 05, 2016 5:49 am

Am I missing a point here?

How on earth is your browser supposed to download the new page you have redirected it to when the server has just been shutdown?
Memory in C++ is a leaky abstraction .

Heater
Posts: 16091
Joined: Tue Jul 17, 2012 3:02 pm

Re: shutdown.cgi won't exit before shutdown

Fri Aug 05, 2016 6:27 am

Can you try using :

sudo shutdown -r --no-wall +1

i.e. shutdown 1 minute after issuing the command. This is just to give time for the browser to reload the redirection page.

If that works we can think about how to reduce the delay.

By the way, you should not be using sudo in scripts like that.
Memory in C++ is a leaky abstraction .

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: shutdown.cgi won't exit before shutdown

Mon Aug 08, 2016 3:29 pm

Heater wrote:Am I missing a point here?

How on earth is your browser supposed to download the new page you have redirected it to when the server has just been shutdown?
Its still up even after shutdown -h now. You still have a few seconds to load several pages. Try it.
Antikythera

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: shutdown.cgi won't exit before shutdown

Mon Aug 08, 2016 3:34 pm

Heater wrote:Can you try using :

sudo shutdown -r --no-wall +1

i.e. shutdown 1 minute after issuing the command. This is just to give time for the browser to reload the redirection page.

If that works we can think about how to reduce the delay.

By the way, you should not be using sudo in scripts like that.
sleep n also hangs the browser's refresh.
but yes 1 minute delay works. strangely it works even if I run something large like emacs without the &. of course it never shows in ps.

lastly, I put sudo because the pi is a networked device but not normally connected to the internet. just connects to a dead end access point.
Antikythera

Return to “General programming discussion”