Results 1 to 10 of 24

Thread: run a script when shutdown computer

Hybrid View

  1. #1
    Join Date
    Apr 2006
    Location
    Laos
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    run a script when shutdown computer

    Hello all!!

    Hope I am in right forum and so.

    Well. I have created a small script I can run sometimes to free some space from computer.

    I have created a file "rensa.bin"
    Code:
    rm -rf /home/erik/.thumbnails/normal/*
    rm -rf /home/erik/.thumbnails/fail/gnome-thumbnail-factory/*
    rm -rf /home/erik/.Trash/*
    rm -rf /home/erik/.mozilla/firefox/pl7hilo9.default/Cache/*
    I have made it executable on the desktop. It runs a command "sh rensa.bin"
    I can use this script and it erase the contents in these directories.
    This is not so important but I think its fun to make something automatic.

    I wonder how do I integrate this script when computer shutdown?
    So I dont need to run from desktop...

  2. #2
    Join Date
    Dec 2005
    Location
    Lithuania
    Beans
    1,103
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: run a script when shutdown computer

    On shutdown: don't really know, sorry
    My advise would be to setup cron to execute that script let's say, every 2 hours.
    https://wiki.ubuntu.com/CronHowto

  3. #3
    Join Date
    Oct 2004
    Location
    Berkeley
    Beans
    86
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: run a script when shutdown computer

    In the UNIX world, such shell scripts normally begin with a "shebang" line:

    Code:
    #!/bin/sh
    and the filename gets the suffix '.sh'. Then you can just mark it executable:

    Code:
    chmod +x rensa.sh
    and you can just run it as is (if it exists somewhere in your command path) without having to say 'sh rensa.sh'.

    To have it run at shutdown, this should do it:

    Code:
    sudo cp rensa.sh /etc/init.d
    sudo ln -s /etc/init.d/rensa.sh /etc/rc0.d/K10rensa.sh
    sudo ln -s /etc/init.d/rensa.sh /etc/rc6.d/K10rensa.sh
    For some background as to why this works:

    An introduction to run-levels

  4. #4
    Join Date
    Apr 2006
    Location
    Laos
    Beans
    30
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: run a script when shutdown computer

    It works! Thank you.

  5. #5
    Join Date
    Jan 2006
    Beans
    292
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: run a script when shutdown computer

    Is it necessary to put the script in int.d for running it on shutdown?
    Custom Build
    WIndows 7 64-bit and Ubuntu 13.04 64-bit

  6. #6
    Join Date
    Aug 2008
    Location
    Amsterdam, the Netherland
    Beans
    51
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: run a script when shutdown computer

    Can't you put in a script to reboot or shutdown the computer? like sudo reboot/ sudo shutdown?
    Nothing in the grey matters

  7. #7
    Join Date
    Jan 2006
    Beans
    292
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: run a script when shutdown computer

    umm..i am not sure i understand what you mean..?
    Custom Build
    WIndows 7 64-bit and Ubuntu 13.04 64-bit

  8. #8
    Join Date
    Jul 2008
    Beans
    1,491

    Re: run a script when shutdown computer

    Quote Originally Posted by xouns View Post
    Can't you put in a script to reboot or shutdown the computer? like sudo reboot/ sudo shutdown?
    Yes. For a generic command (which jumps to various levels of services, 6 being the default session; 0 being power off...)

    Code:
    sudo init <value>
    Alternatively:
    Power off after shutdown:
    Code:
    sudo shutdown -h -P now
    Reboot:
    Code:
    sudo shutdown -r
    EDIT: For more information regarding the shutdown command type:
    Code:
    shutdown --help

  9. #9
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: run a script when shutdown computer

    Quote Originally Posted by linuxnovice View Post
    Is it necessary to put the script in int.d for running it on shutdown?
    The script has to be in /etc/rc{0,6}.d to run on shutdown and reboot. Convention is for the script itself to live in /etc/init.d and for there to be symlinks to it from the proper rc*.d directories. You should follow the convention unless you have a very good reason for doing otherwise.

    In Ubuntu, there's another method available: Upstart. Upstart is event-based, and theoretically more flexible. I've never messed with it, though, so I couldn't tell you how to use it.

  10. #10
    Join Date
    Apr 2008
    Beans
    286

    Re: run a script when shutdown computer

    What's wrong with running the script on startup? It's essentially the same thing, isn't it? And it's way easier to implement, just go to System -> Preferences -> Sessions
    Then in the "Startup Programs" add your script as a new program to run.

    Rouslan

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
  •