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.

116 reponses to "Automate Ubuntu Server Shutdown"

1. reply

Have no a lot of cash to buy a building? Do not worry, because it's achievable to get the home loans to resolve such problems. Thus take a bank loan to buy all you want.

2. Hello :)

Great post thanks for sharing :)
poker en ligne

3. Nice work

I had spent a good deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that.
 
Cute quotes - Birthday poems

4. reply

Specialists tell that business loans aid people to live the way they want, because they are able to feel free to buy necessary things. Furthermore, various banks offer term loan for different classes of people.

5. Considerably, the article is

Considerably, the article is in reality the greatest on this noteworthy topic. I agree with your conclusions and will eagerly look forward to your next updates. Saying thanks will not just be sufficient, for the wonderful clarity in your writing. I will immediately grab your rss feed to stay privy of any updates. Pleasant work and much success in your business dealings!
Trading Seminars
 

6. RE

. Article on shutdown Ubuntu is great.I really looking it.Its really very helpful for me. seo services link building services directory submission social bookmarking submission

7. .Article on Ubuntu server

.Article on Ubuntu server shutdown is great.I was really looking for it.Its really very helpful to me.

Funeral Insurance

8. respond this post

Thank you, it's very astounding information it might be very helpful for students. For example last year when I had a difficult of time at the end of semester with a ceaseless flow of academic assignments and homework, I had a inspiring idea to buy it somewhere and than use plagiarism checker. I was so dead that I did not care for what can happen when my academic work was written by different person. To my awesome surprise, research paper was estimable the price I paid for it. I was so charmed with the quality and now everytime i use this service.

9. This PHP is really Helpful

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 PHP is really Helpful for me and my friend :P
 Display Stands

10. 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.
 

Contract Hire Deals

 

11.  Very informative post, thank

 Very informative post, thank you for sharing.
online pharmacy no prescription
online pharmacy prescription needed

12. .Article on Ubuntu server

.Article on Ubuntu server shutdown is great.I was really looking for it.Its really very helpful to me.

Funeral Insurance

13. Great Tutorial

Your tutorial about how to shutdown the server automatically was awesome and great. I have learned lot of things from your post. Thanks for sharing.
free stuff

14. thank for outstanding

thank for outstanding tutorial. Pamela from bluehost review | fatcow review

15. By learning these

By learning these technologies, you open up so much more possibilities than
if you narrow yourself to a select few set of components.

Law Firm

 

16. Great tip

Thanks for this tip. I have a small network at home (about 4computers connected) and I want this type of set up. Thanks for sharing by the way.
custom essayscustom essay

17. RE

Ubuntu One is a great online service that allows you to synchronize your personal files and folders on a remote server. seo services link building services

18. Nice share

Very good post. Thanks for sharing such a nice information.
toronto web designer web design company toronto web designers

19. good work

Acegi does get a little interesting though because the various filters rely upon Spring's dependency injection, but the standard way of configuring servlet filters into web.xml does not support Spring dependency injection. (The web.xml schema doesn't have anything for dependency injection, nor should it.) Acegi uses a pretty neat trick to give us injectable servlet filters. online bingo bonus

20. reply this topic

Students’ life time seems to be very strange thing and sometime people must do one thing at the same time, then you are lack of time to perform some else stuff. Sometimes, some students have to select between job and speeches essay paper doing. In this situation, I strictly recommend to see the experienced free essay writing service to order the essay related to this good topic at.

21. I learned a lot from this

I learned a lot from this useful information, thanks for sharing with us.

search marketing services

22. position

free sign up bonus bingo uk

The chair is fitted with optimised functions which include gas height adjustment and tilt mechanism, to allow for greater comfort and allowing you to find your ideal position.

23. RE

Great cron job to automate this task. Ab Belt

24. Thanks for Article

Thanks for sharing such a superb piece of code. This was very much helpful.
Regards
Carpet Cleaning Service

25. This is useful code for us

This is useful code for us,You always share Such a nice info in the posts.Thanks admin to made this great blog.

Justin Bieber
 
 
 

26. hi

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. phentermine without prescription

27. Hi

Clean website. Do you ever accept guest posts? I am maintaining a site on my latest hobby water filters and wanting to trade some content with good sites. I looked around your blog and you've got some good content and I was thinking our readers would both find value. Thanks!
buying wow accounts
 

28. hi

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 ..Funeral Cover

29. College Research Papers

Pretty insightful post. Never thought that it was this simple after all. I had spent a good deal of my time looking for someone to explain this subject clearly and you’re the only one that ever did that. Kudos to you! Keep it up
College Research Papers

30. best tutorial

Very useful code, you have helped me greatly in providing this tutorial. Thank you.wholesale store fixtures

31. office chair

office chairs

This ergonomic high back office chair extends the full length of the back, up to the shoulders and includes support for the head and neck. Our chairs are crafted to perfection and designed to the bodies natural shape, you will find complete comfort with its PU leather material and padded arm rests.

The chair is fitted with optimised functions which include gas height adjustment and tilt mechanism, to allow for greater comfort and allowing you to find your ideal position.

Our executive range of office chairs are built to be Safe, to last for years and cannot be beat in any head to head comparison in its class. Easy assemble, a strong nylon base and 360° swivel, top this PU leather, a fantastic executive look.
 

32. Great informative post thanks

Great informative post thanks for sharing.....
Term Paper | Research Paper | Essay Writing | Thesis Writing | Dissertation Writing
 

33. I just want to emphasize the

I just want to emphasize the good work on this
custom writing | course work
 

34.  I've been looking for this

 I've been looking for this code all day, thanks! Online Degree

35. Hi all

This ergonomic high back office chair extends the full length of the back, up to the shoulders and includes support for the head and neck. Our chairs are crafted to perfection and designed to the bodies natural shape, you will find complete comfort with its PU leather material and padded arm rests.

The chair is fitted with optimised functions which include gas height adjustment and tilt mechanism, to allow for greater comfort and allowing you to find your ideal position.

Our executive range of office chairs are built to be Safe, to last for years and cannot be beat in any head to head comparison in its class. Easy assemble, a strong nylon base and 360° swivel, top this PU leather, a fantastic executive look.
 

36. RE

can not implement this part
<?php
// Call the nmap command
$output = shell_exec('nmap -sP 192.168.1.*');
any tips?
satellite internet service 

37. Nice cron job to automate

Nice cron job to automate this task.
office supplies

38. Very informative article

Very informative article here.I am the student of computer stream so this inforamtive article is very useful to me.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.

Monte Glider

39. sss

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 found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them. To me, you are doing the great work.

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;}

jordan II kicks
 
 

40. RE

could make a little program that pops up a console and asks you to hit a key in 10 seconds or the system will shut down that would solve the "working late" issue. DWI Lawyers

41. Therefore, we would recommend

Therefore, we would recommend your nice information. But they shoulld search for the www.btopi.com

42. I really love to read

I really love to read articles that have good information and ideas to share to each reader. I hope to read more from you guys and continue that good work that is really inspiring to us.
itunes card online

43. Great programming

Thanks for the explanation through your code! It gives me allimentatie and helps me to implement the code on my server.

44. Your blog is a great one.

Your blog is a great one. What really impresses me is that you are correctly mentioned that there are thousands of tools that are available to create a website or launch one but what matters is that you choose the right one, the one that gives you all that is actually needed.
Office Catering
 

45. 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

46. Very useful code, you have

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

47. 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

48. 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

49. 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
 

50. 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