Automate Ubuntu Server Shutdown

power downSomebody on the Ubuntu Forums was asking how to get their server to shut down automatically when no other computers are online on the network.  This can be useful if you have Wake-On-Lan enabled on the server, and you're only using it for things like printer or file sharing.  I responded with a little php script I whipped up that does the job nicely.

You will need the php package and the nmap package to make this work

sudo apt-get install php nmap

Next, you'll need to know how to setup a cron job.  If you don't, check out this tutorial.

Here is the contents of the php file.
hostsup.php
 

<?php
// Call the nmap command
$output shell_exec('nmap -sP 192.168.1.*');

// Split up the output so we get the number of hosts up
$split1 explode("addresses ("$output);
$split2 explode(" hosts up"$split1[1]);
$hostsup $split2[0];

// If only x number of computer are on + router, shutdown.  Take in account the server being a computer!
if($hostsup == "2") {
    
$shutdown shell_exec('shutdown -h now');
}
?>

You'll need to edit the "192.168.1.*" to fit your network's IP range.  Also, change the $hostsup == "2" to the number of computers you want on when it shuts down.  The server counts as one, your router counts as one, and if you have any of those network printers, they also count.  If it gets an IP, count it.  Feel free to change the shutdown command to something else if you want to put the server into standby or what have you.

In your cron job, just set the command as

php -f /path/to/file/hostsup.php

Set the cron to run as often as you like.  I'd set it for every half-hour just to not use up too many resources, and still be effective.

What this script is doing is executing "nmap -sP 192.168.1.*".  This generates a list of nodes on the network that are currently online, and their IP addresses.  The next 3 lines parse through the information to only get the number of nodes online.  Next we check how many are online, if the numbers match then execute the shutdown command.  You'll need root or sudo access to use the shutdown command, so watch out.

122 reponses to "Automate Ubuntu Server Shutdown"

1. Its Really Helpfull

I was finding this type of article,which increases our knowledge related it field. Your article contains Topic Automate Ubuntu Server Shutdown Is very helpful.It's really nice  to get  server to shut down automatically when no other computers are online on the network.Thanks for giving this informative article.Memory foam mattress

2. Very useful code, you have

Very useful code, you have helped me greatly in providing this tutorial. Thank you.
Ben 10 Games

3. hi

I am always searching for the my stream articles .I liked your article very much , the reason is that  your article provides a very important information here. And keep writing such kind of articles. office chairs

4. Thanks for info

 Youre so cool!  I dont think Ive read anything like this before.  So good to find somebody with some original thoughts on this subject.  Thanks for starting this up.  This blog is something that is needed on the web, someone with a little originality.  Good job for bringing something new to the internet.
Thanks
Gist

5. nice work.

Great work.. thanks for sharing such a good piece of work. its really very helpfull for me.. it helped alot in many of my projects.
----------------------------------------------------------------------
logo designlogo designerlogos
 

6. Nice share

This blog is something that is needed on the web, someone with a little originality.  Good job for bringing something new to the internet.
Bredband

7. Is It Doable

This seems to be a good suggestion. But, it will be great if this suggestion can implement. Anyway, you may want to optimize this blog to make money online.

8. My thought

Normal
0

false
false
false

MicrosoftInternetExplorer4

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}

I recently came across your article and have been reading along. I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end. I would like to read newer posts and to share my thoughts with you.
sonnerie portable
 
 

9. You have a very good site,

You have a very good site, well constructed and very interesting i have bookmarked you hopefully you keep posting new stuff, many thanks
Process & Tools Consulting

10. I havent any word to

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.
IT Outsourcing

11. Great !!!!

Ok....now i understood y my server ws not working properly.Wake-on-LAN was not enabled on it.Cool....just a simple code solved my problems.I am already done with editing my IP address.
Double Mattress

12. Really Helpful

I was facing the same problem from long time . Thanks for sharing this wonderful article. Its really worked easily , I have told my friends also to read  "Automate Ubuntu Server Shutdown" article,which is very helpful.Thanks ones again.Memory Foam
 
 
 

13. Nice Post

Its my pleasure that I got an opportunity to comment on this post. Its a very nice post and I love it.
 
<a href="https://www.tank-prints.com">Business Cards</a>
 

14. Thanks for info

Always good to see, this was obvious a excellent post. In theory would like to be such a good writer too. You need time to creat that brilliant and in addition real effort to create a excellent article  Thanks
Table massage portable

15. Simulateur De Credit Immo

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the excellent wor

Simulateur De Credit Immo

16. Nice share

Very good post. Liked to read every bit of information shaed by the author.
register domain names
buy domains

17. Good post.

 Its very good post.I like it.
<a href="http://www.thebestcasinoguide.com">Online Casino</a>

18. This article is really

This article is really awesome and informative; I learned a lot helpful information from here, thanks for sharing with us.
Car Games
 
 

19. My discussion

Normal
0

false
false
false

MicrosoftInternetExplorer4

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin:0in;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}

This article gives the light in which we can observe the reality. This is very nice one and gives in depth information. Thanks for this nice article. Good post.....Valuable information for all.
sonnerie gratuite pour portable
 
 

20. Great Article

Its a great pleasure reading your blog. The blog content is powerful.Very Good.
 
<a href="http://www.unityrehab.com">Florida Drug Rehab</a>

<a href="http://www.careflorida.com">Addiction Treatment</a>

<a href="http://www.rehabflorida.com">Drug Rehab Florida</a>

 

21. Hi....

Thnaks for the nice information about server and php. We will look forward for future upates.
Guaranteed Payday

22. ASD

if you want to be rich and famous click on casino en ligne

23. It's Really Helpful

Thanks for this great article and sharing it.
I was finding this type of article which will helpful for our knowledge,because we like to learn  new things related server. As I have read article how to shut down our server when no other computers were  online on the network, It's very useful information for things like printer or file sharing computer. Thanks ones again for sharing this important things with us. mattress

24. cool

very nice tutoril, thanks for sharing.
 
dress up games
hannah montana games

25. Send flowers to Korea

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}

I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

/* Style Definitions */
table.MsoNormalTable
{mso-style-name:"Table Normal";
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-qformat:yes;
mso-style-parent:"";
mso-padding-alt:0in 5.4pt 0in 5.4pt;
mso-para-margin-top:0in;
mso-para-margin-right:0in;
mso-para-margin-bottom:10.0pt;
mso-para-margin-left:0in;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:"Times New Roman";
mso-fareast-theme-font:minor-fareast;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;}

Send flowers to Korea
 
 

26. Re: Automate Ubuntu Server Shutdown

I have been reading your post regularly. They are highly informative and helpful.

seo services

27. This is extremely helpful

This is extremely helpful info!!! Especially since you guys are offering it for free!! Very good listing. Everything is true. Thanx.
Dissertation Writing | Custom Dissertation

28. Nice

Thanks for sharing such a nice post with us...
disesrtation help thesis help

29. Contract Hire

 
Nice Informative Blog having nice sharing
including reasonable comments here...
 
Must Visit:
Contract Hire
 

30. Thank you for this very

Thank you for this very useful information.

dissertation | writing help

31. Its Really Great Idea

AutoFsck ensures that the automatic disk check will no longer inconvenience.There are several ways to automate the Ubuntu installation process.

Hidden Camera

32. good post

I read your blog it is very informative about Battlefield of the Mind.
I think your a great writer it very useful and helpful to me. I really liked and will see to more updates on your blog

SEO Services India

33. Hi...

I completely agree with your blog after shutting down in server all depend upon in switch routers and administrator this is really good technical article thank you for your posting we will wait for your future updates.
Newburgh Homes For Sale

34. Ubantu...!!

Technically sounds good. Let me try this at my end....Well thanks fo the information...!!!!!
brother tn 350 toner cartridge

35. Book Summaries

I completely agree with your blog after shutting down in server all depend upon in switch routers and administrator this is really good technical article thank you for your posting we will wait for your future updates.
Book Summaries

 
 
 

36. Book Summaries

I completely agree with your blog after shutting down in server all depend upon in switch routers and administrator this is really good technical article thank you for your posting we will wait for your future updates.
Book Summaries

 
 
 

37. Nice forun

This is really good read for me. must admit  that you are the one beast bloggers i ever saw. thanks for posting the informative article.
sweepstakes

38. Automate Ubuntu Server Shutdown

I agree with your conclusions and will eagerly look forward to your approaching updates.
incontinence products

39. Hi..

I recently came across your article and have been reading along.
I want to express my admiration of your writing skill and ability to make readers read from the beginning to the end.
I would like to read newer posts and to share my thoughts with you
 
Solar Training

40. injury lawyer

I am really interested in this program but I do not know much about it. After reading your article, i have more experiences to work with it. Your post is interesting and picturesque. I hope i can get your post in next time. Keep on!
injury lawyer

41. Ubuntu

Ubuntu is a popular Linux distribution that's a free and open source alternative to Windows. Install Ubuntu Server 9.10 either into a VritualBox machine, either on a normal hardware machine.
Auto Glass Pheonix

42. Ubuntu

Ubuntu is a popular Linux distribution that's a free and open source alternative to Windows. Install Ubuntu Server 9.10 either into a VritualBox machine, either on a normal hardware machine.
Auto Glass Pheonix

43. Ubuntu One is an awesome

Ubuntu One is an awesome online service that lets you synchronize your personal files and folders on a remote server. The shutdown utility provides an automated shutdown procedure for supersers to nicely notify users when the system is shutting down, saving them from system administrators, hackers, and gurus, who would otherwise not bother with such niceties.
ORLANDO RETAIL SPACE

44. Awesome

I admire time and effort involved in your high quality and informative articles. But at the same time they are simple to understand and helpful. I am looking forward to seeing your future updates.

chat roulette script
 

45. I agree.

I was looking for a sample code for that, thank you for sharing.
Dora games
 

46. I neglect to ask what he had

I neglect to ask what he had been trying to steal but Vic tells me who he is - the kid with the buzz cut and the earring, you know, he's back there right now. I am walking purposefully toward the front of the store when I catch sight of him walking in the same direction, about one hundred ps print review feet to my left. I hear this and I say something, without thinking, about wanting to help people with asthma.
 

47. answer this topic

We opine, you have to use the article submission for your outcome just about this good post, because that is really good enough and mustn’t be hidden from people!

48. reply

Different students do not really know the way to perform the humanities essay. Thus, at present time they could follow your incredible release about this post and just do the most perfect essay e'er.

49. Thanks for sharing! custom

Thanks for sharing!
custom dissertation | term paper

50. reply this topic

Some time ago, I did need to buy a good car for my organization but I didn't earn enough cash and could not order something. Thank goodness my father adviced to take the mortgage loans at trustworthy bank. Hence, I did so and was satisfied with my term loan.