LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-05-2013, 10:32 AM   #1
clmowers
Member
 
Registered: Mar 2005
Location: upstate ny
Posts: 42

Rep: Reputation: 15
Script run as root


Hello all, I have a quick question and i can't seem to find a straight answer.

I want to run a script to change the I/O scheduler on my hard drive to cfq for better performance. Im able to change it with

Code:
echo cfq > /sys/block/sda/queue/scheduler
That works perfectly. The problem is i need to run that as root, sudo doesn't seem to work correctly, so running su and that script works. The thing is i have to run this every time i log into the computer.
So is there a way to either change that so it will be perm set so when i log in, its already set, or be able to run that as a login script with roots permissions.

I do not want to add root password to a script unless its encrypted and even then I still wouldn't like having it in the script its self. How can i go and get this thing going?

Mint 15 x64
 
Old 09-05-2013, 10:44 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,985

Rep: Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315Reputation: 4315
Quote:
Originally Posted by clmowers View Post
Hello all, I have a quick question and i can't seem to find a straight answer.
I want to run a script to change the I/O scheduler on my hard drive to cfq for better performance. Im able to change it with
Code:
echo cfq > /sys/block/sda/queue/scheduler
That works perfectly. The problem is i need to run that as root, sudo doesn't seem to work correctly, so running su and that script works. The thing is i have to run this every time i log into the computer.
So is there a way to either change that so it will be perm set so when i log in, its already set, or be able to run that as a login script with roots permissions.

I do not want to add root password to a script unless its encrypted and even then I still wouldn't like having it in the script its self. How can i go and get this thing going?
Things like this are why sudo was invented, so I'm surprised it's not working for you. Can you tell us what you mean by "doesn't seem to work correctly"?? Without details, there's not much we can tell you.

You can set sudo up to not require a password, which may be where you're hitting a snag. Put a user (or more), into the "wheel" group on your system (see man page on usermod). Run visudo (as root), and look for lines like these..add them if they're not there:
Code:
# Uncomment to allow people in group wheel to run all commands
# %wheel        ALL=(ALL) ALL

# Same thing without a password
%wheel  ALL=(ALL) NOPASSWD: ALL
Log out/back in. Now, if you're a member of the wheel group, you can just type in "sudo <command>" to run it as root.
 
1 members found this post helpful.
Old 09-05-2013, 10:46 AM   #3
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
If you want this command to be executed every time you boot just put it in your rc.local file (located somewhere in /etc, depending on the distribution, IIRC).
This script is run as root every time you boot.
 
1 members found this post helpful.
Old 09-05-2013, 10:57 AM   #4
clmowers
Member
 
Registered: Mar 2005
Location: upstate ny
Posts: 42

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by TobiSGD View Post
If you want this command to be executed every time you boot just put it in your rc.local file (located somewhere in /etc, depending on the distribution, IIRC).
This script is run as root every time you boot.
Perfect, exactly what I was looking for and worked like a charm Thanks
 
Old 09-05-2013, 10:59 AM   #5
clmowers
Member
 
Registered: Mar 2005
Location: upstate ny
Posts: 42

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by TB0ne View Post
Things like this are why sudo was invented, so I'm surprised it's not working for you. Can you tell us what you mean by "doesn't seem to work correctly"?? Without details, there's not much we can tell you.
Not sure what the problem is with it. When i run with sudo it was telling me permission denied, but if i did a su and logged in as root and ran it, it worked fine. Not totally sure. This was the only time ive ever find sudo not working the way it was intended to work
 
Old 09-05-2013, 12:49 PM   #6
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839Reputation: 4839
It is because of the redirection. Sudo will only work for the command before the redirection, since the shell first tries to open the file you want to write before launching the commands on the left hand side. At this point you are still the normal user and have no write permission for that file.

Last edited by TobiSGD; 09-05-2013 at 02:21 PM. Reason: confused right with left
 
Old 09-05-2013, 01:55 PM   #7
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
what TobiSGD said.. both the rc.local and the reason it failed



I still 'forget' when using sudo and redirection

but this would be a way to do it

Code:
sudo bash -c "echo cfq > /sys/block/sda/queue/scheduler"
you are executing bash, ( as root ) which then executes your command with redirection, as expected.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Run a Script as root matt_thumper Linux - General 8 07-24-2010 11:29 AM
How to run a shell script as root (when user is not root) taylorkh Linux - Newbie 10 09-12-2008 06:05 PM
Want to run script without being root, possible? webaccounts Linux - Newbie 8 06-11-2008 08:52 PM
How can I have a script owned as root and run as root by a user: setuid? stickey bit? abefroman Linux - Newbie 9 04-19-2008 05:15 PM
Is a script, run at boot time from init.d, run with root authority? tmbrwolf53 Linux - Server 2 03-31-2007 08:15 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration