Results 1 to 10 of 10

Thread: Execute Command When Server is Shut Down or Started?

  1. #1
    Join Date
    Dec 2004
    Location
    Waterford, MI
    Beans
    1,041
    Distro
    Kubuntu

    Execute Command When Server is Shut Down or Started?

    I tried searching for this but maybe my Google skills suck today.

    Anyway, I want the server to email me when it is going down for a graceful shutdown. I also want it to email me when it is started.

    I can handle writing the actual email scripts for both events, I don't need help with that. I just need to know where to tell Ubuntu to execute such a script on shut down and start up.

  2. #2
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Execute Command When Server is Shut Down or Started?

    You can always write a startup script to notify you and set it to run before networking goes down.

    http://wiki.debian.org/LSBInitScripts/
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Execute Command When Server is Shut Down or Started?

    You could make an upstart script or system v script and set it to run after the network is brought up or before it is shutdown. For the upstart scripts see /etc/init/. For the System V scripts see update-rc.d. I think System V is deprecated but for me it is more clear.

    Alternately, you can put a pair of scripts in /etc/network/if-up.d/ and /etc/network/if-down.d/.

  4. #4
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Execute Command When Server is Shut Down or Started?

    Quote Originally Posted by Lars Noodén View Post
    You could make an upstart script or system v script and set it to run after the network is brought up or before it is shutdown. For the upstart scripts see /etc/init/. For the System V scripts see update-rc.d. I think System V is deprecated but for me it is more clear.
    Not to be totally offtopic, but I wrote the startup script I use with VirtualBox in System V, because I had a hell of a time wrapping my head around upstart. System-V seems more intuitive to me.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  5. #5
    Join Date
    Dec 2004
    Location
    Waterford, MI
    Beans
    1,041
    Distro
    Kubuntu

    Re: Execute Command When Server is Shut Down or Started?

    Wow, it looks like those solutions may be a bit over my head. I know how to write BASH scripts fairly well. I was hoping to write a script for shutdown and one for startup and put them somewhere and have them run accordingly. I'm not sure I want to tie such scripts to the network, just in case my network goes down I don't mistakenly think my server has shut down.

  6. #6
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Execute Command When Server is Shut Down or Started?

    Well, the new way, Upstart, seems harder and less straight forward. I have to practice with them still.

    The old way, System V, still works and is just a matter of writing a plain shell script and then running a program (update-rc.d) to install it. You can look at several of the scripts in /etc/init.d/ to get an idea of how it's done. I'd say look at several to find a simple one. It's not hard if you already have the script. Here's a template:

    http://www.cyberciti.biz/tips/linux-...p-service.html

  7. #7
    Join Date
    Jun 2011
    Beans
    357

    Re: Execute Command When Server is Shut Down or Started?

    For start up, you could simply copy/paste your script into /etc/rc.local Commands in this file get run after everything else is brought up. It will be the last set of commands executed during start-up.

    As for shutdown, if you aren't comfortable working with Upstart you could probably do something like alias the poweroff command to run your script and then shutdown the system.

  8. #8
    Join Date
    Dec 2004
    Location
    Waterford, MI
    Beans
    1,041
    Distro
    Kubuntu

    Re: Execute Command When Server is Shut Down or Started?

    I have an APC UPS with a working apcupsd configuration, is there a way to use that? I Googled around for a way to do it, but I don't see how. I have it emailing me when the power goes down, but I don't see an option in the config to make it email when the server shuts down, unless I missed it. Perhaps that's a possibility?

  9. #9
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Execute Command When Server is Shut Down or Started?

    That wouldn't work for normal shutdowns.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  10. #10
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: Execute Command When Server is Shut Down or Started?

    IIRC putting a script in /etc/rc0.d/ will make it execute at shutdown (when entering runlevel 0). Another option for running it at boot is to have a @reboot crontab line for it (see crontab(5)).

    EDIT: I just tried, seems to do the trick.
    Last edited by Bachstelze; August 30th, 2012 at 05:00 AM.
    「明後日の夕方には帰ってるからね。」


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •