Page 1 of 3 123 LastLast
Results 1 to 10 of 24

Thread: run a script when shutdown computer

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

  9. #9
    Join Date
    Oct 2007
    Beans
    1,914
    Distro
    Lubuntu 12.10 Quantal Quetzal

    Re: run a script when shutdown computer

    Quote Originally Posted by linuxnovice View Post
    umm..i am not sure i understand what you mean..?
    He/She means that you just copy your commands to a script, append the command to shutdown the computer and in the future use this script to shotdown the computer instead of the usual GNOME System->Quit menu command or whatever.

    For adding your script to the boot sequence, look at http://ubuntuforums.org/showthread.p...ghlight=bootup
    this post by me - It tells you how to install a custom script to the bootup&shutdown sequence. It also tells you how to revert these changes. Note that your script will then be run at bootup&shutdown. However, you can check what is the case. So your code should look like:
    PHP Code:
    #!/bin/bash

    if [ "$1" "stop" ]
    then 
      
    ....
    fi 

  10. #10
    Join Date
    Sep 2007
    Location
    Miami, FL
    Beans
    143

    Smile Re: run a script when shutdown computer

    no longer participating in ubuntuforums.org
    Last edited by MarkieB; April 16th, 2012 at 01:37 AM.

Page 1 of 3 123 LastLast

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
  •