Find the answer to your Linux question:
Results 1 to 5 of 5
I want to run a bash script when my pc shut down. Suppose the script is myscript.sh How can I do this ?...
  1. #1
    Just Joined!
    Join Date
    Dec 2008
    Posts
    18

    Exclamation Running a script when the pc shut down

    I want to run a bash script when my pc shut down.
    Suppose the script is myscript.sh
    How can I do this ?

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    I don't know what the best solution is, but the script /etc/rc.d/init.d/halt is executed when the system is shut down or rebooted. You could put a call to your script there. Test if it exists, and if so, then execute the script. This would occur before the system terminates and then kills running processes.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jun 2009
    Posts
    3
    #Perhaps you have to edit your crontab.
    #Use the command
    crontab -e

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by jahangircsebd View Post
    #Perhaps you have to edit your crontab.
    #Use the command
    crontab -e
    What does cron have to do with shutting down the system? Confusion here...
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jul 2008
    Location
    Kenya
    Posts
    26
    This has nothing to do with cron. Cron schedules jobs at specified time.

    if you need to run the script everytime you shutdown, then include a call to your script in /etc/rc.d/init.d/halt

    or
    including shutdown command inside the myscript.sh so that when you run your script it will shutdown the machine after execution.

Posting Permissions

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