<Solved> Run script on startup and after suspend

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read this

<Solved> Run script on startup and after suspend

Postby comradnoel on Wed Feb 15, 2012 2:36 pm

Hey all,

I heard a clicking sound coming from my new computer. Found out it was the dread hdparm. After some research, I discovered that running
Code: Select all
sudo hdparm -B 255 /dev/sda
will stop the apm and stop the clicking. How can I make this into a script that will run every time I start my computer and every time my computer wakes up from a suspend? I've done some searching around, but all the answers to this question involved too much computer knowledge :( I'm running Linux Mint Devian 3. Thanks!
Last edited by comradnoel on Mon Feb 20, 2012 3:10 pm, edited 1 time in total.
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Linux Mint is funded by ads and donations.
 

Re: Run script on startup and after suspend

Postby chipbuster on Thu Feb 16, 2012 2:21 am

http://www.debian-administration.org/articles/28


So if I understand this correctly (and keep in mind that I may not and you may break something), this is what you would enter in terminal, one line at a time (choose any name for stopclick that you want).

Code: Select all
su -
[enter password to gain superuser]

echo 'sudo hdparm -B 255 /dev/sda            # update-rc.d -f  stopclick remove      to remove script' >>  /etc/init.d/stopclick

chmod 755 /etc/init.d/stopclick

update-rc.d stopclick defaults


You don't have to include the hashmark, but I thought it'd be nice to remind yourself how to remove it if you no longer need it someday.
This is totally not experience speaking, I'm just trying to interpret the documentation for you.
chipbuster
Level 4
Level 4
 
Posts: 229
Joined: Wed Feb 08, 2012 1:34 am

Re: Run script on startup and after suspend

Postby comradnoel on Thu Feb 16, 2012 2:24 pm

Thanks for the link! I looked through it and tried to follow what it said. I created a script called disable-apm within /etc/init.d. Added the code

Code: Select all
 hdparm -B 255 /dev/sda 

to the script.
Ran

Code: Select all
su -

echo 'sudo hdparm -B 255 /dev/sda           

chmod 755 /etc/init.d/disable-apm

update-rc.d disable-apm defaults


And it sort of worked!
When I reboot the computer, APM is now off. However, if I shut down the computer, then boot it up, APM is still on. Same if I suspend: APM is still running. :(
What next?
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Re: Run script on startup and after suspend

Postby chipbuster on Thu Feb 16, 2012 5:32 pm

Wait, when you reboot it's okay, but when you shut down and start up its broken? Try a couple power cycles--both reboot and shutdown, and see what you get.

While you're doing that, if it clicks at you, do the following: go ahead and enter runlevel. It should spit back a number. Take that number and put it into an init command. Here's an example

Code: Select all
chipbuster@lpc1$ runlevel
runlevel 4
chipbuster@lpc1$ sudo init 4


Enter the runlevel you get after init.

That should cause the computer to reexecute the init.d script we made last time/

If that doesn't fix it, go ahead and use other runlevels. Command will be init [1-5] (e.g. "init 1" "init 2" init 3" .....). Keep in mind that "init 0" will cause a shutdown and "init 6" will cause a reboot. If you get stuck in a shell, you can use sudo reboot to reboot your computer. Do a little futzing around and let us know what you find.

My hope is that changing/defining the runlevel will cause the script to execute....but I don't know if that's the case. Theoretically, that script should execute every time you turn the computer on.

Oh, and I'm not sure how Mint handles runlevels. It's possible that executing certain runlevels will turn off your X window manager that manages the graphical desktop, so be sure to remember how to reboot the computer if that happens. (sudo reboot)
chipbuster
Level 4
Level 4
 
Posts: 229
Joined: Wed Feb 08, 2012 1:34 am

Re: Run script on startup and after suspend

Postby comradnoel on Thu Feb 16, 2012 7:24 pm

Code: Select all
sudo runlevel
N 2


I tried init 1, 2, 3, 4, & 5, but each time the same thing happened.

Code: Select all
sudo hdparm -B /dev/sda
[sudo] password for philip:

/dev/sda:
 APM_level   = 96


Also I checked out my previous statement. The script never runs; it's just that if I disable hdparm manually, then reboot, hdparm stays disabled. If I disable hdparm, then shut down, when I boot back up hdparm is running.

I've added my computer specs:

Code: Select all
CPU[-Dual core Intel Core i3-2330M (-HT-MCP-) clocked at 800.000 Mhz-] Kernel[-3.0.0-1-amd64 x86_64-] Up[-14 min-] Mem[-545.9/3877.6MB-] HDD[-820.2GB(22.1% used)-] Procs[-212-] Client[-Shell-] inxi[-1.4.23-]
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Re: Run script on startup and after suspend

Postby chipbuster on Thu Feb 16, 2012 9:28 pm

Well then, seems like that's not working :(

I pulled the instructions off of the Debian documentation, so there's a small chance that it wouldn't have worked in the first place

http://embraceubuntu.com/2005/09/07/add ... at-bootup/

Take a look at comment #5. He mentions adding the line to the file /etc/init.d/rcS.sh

Try adding that line to the file. Command would be

Code: Select all
su -
echo "hdparm -B 255 /dev/sda " >> /etc/init.d/rcS.sh


By the way, I noticed that the code box you provided had the line
echo 'sudo hdparm -B 255 /dev/sda


in it. Is that how you entered it verbatim? If that's not a typo, then the command was likely not actually saved into the script. Fix it with
Code: Select all
echo 'sudo hdparm -B 255 /dev/sda' > /etc/init.d/disable-apm
chipbuster
Level 4
Level 4
 
Posts: 229
Joined: Wed Feb 08, 2012 1:34 am

Re: Run script on startup and after suspend

Postby comradnoel on Fri Feb 17, 2012 3:52 pm

Good catch!

I input the code verbatim as you wrote in your first post and now the script works at bootup! Thanks for that.

How can I make the script run when I start up from suspending the computer? When I suspend, APM is back on :(
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Re: Run script on startup and after suspend

Postby chipbuster on Fri Feb 17, 2012 6:37 pm

I've found a possible fix for you, but I'll need to do some poking around on my own Debian machine before I can confirm it for you (I'm on a friend's laptop right now). Look for a fix between 2-4 hours from now. Sorry about the delay, I just dont want to brick your computer on accident by putting a bad script into the system ("Linux assumes you know exactly what you're doing")

It has something to do with placing a particular script into /etc/pm/sleep.d/ to execute on resume. I just need to figure out whether that directory still exists in Debian Wheezy and how to get it to execute on resume (and not suspend).
chipbuster
Level 4
Level 4
 
Posts: 229
Joined: Wed Feb 08, 2012 1:34 am

Re: Run script on startup and after suspend

Postby comradnoel on Fri Feb 17, 2012 7:07 pm

There is an etc/apm/suspend.d folder
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Re: Run script on startup and after suspend

Postby chipbuster on Fri Feb 17, 2012 8:28 pm

EDIT2: I initally posted to not try these instructions because they weren't working. The reason for that is because my stupid arse forgot to make the script executable. Be sure to run chmod 755 on the script after you make it, otherwise the log will read successful execution, but nothing will happen. I successfully created a script that writes a file to my home folder upon resuming from suspend; these instructions should be good for anyone with LMDE UP3

----------------------

Okay, with comradenoel's post, I'm really not sure anymore....

Lets start off with trying pm-suspend, since it appears that there are more folders there.

Open up a superuser text editor with "gksudo gedit" if you're on GNOME/Xfce or "kdesudo kate" for KDE.

Once you have that open, copy the following into the editor:

Code: Select all
#!/bin/bash
case "$1" in
    hibernate|suspend)
        echo "We are suspending/hibernating..."
        ;;
    thaw|resume)
        echo "We are thawing/resuming"
        hdparm -B 255 /dev/sda
        ;;
esac


Now save it. You can either save it as /etc/pm/sleep.d/5noclick, or save it to your Documents folder and move it. Command to move it from Documents is
Code: Select all
sudo mv ~/Documents/[filename] /etc/pm/sleep.d/5noclick


Make sure the file is executable, otherwise you're going to run in to the same stupid problem I did. Command to make the file executable is
Code: Select all
chmod 755 /etc/pm/sleep.d/5noclick


Once you've done that, try suspending your computer from command line with
Code: Select all
sudo pm-suspend
. If that works, then try suspending it from the GUI. If either one fails, let us know.
chipbuster
Level 4
Level 4
 
Posts: 229
Joined: Wed Feb 08, 2012 1:34 am

Re: Run script on startup and after suspend

Postby comradnoel on Mon Feb 20, 2012 3:10 pm

Seems to work like a charm! Thanks so much for your help!
comradnoel
Level 1
Level 1
 
Posts: 12
Joined: Thu Feb 09, 2012 2:12 pm

Linux Mint is funded by ads and donations.
 

Return to Newbie Questions

Who is online

Users browsing this forum: No registered users and 5 guests