LinuxQuestions.org
Review your favorite Linux distribution.
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 12-31-2012, 05:04 PM   #1
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Rep: Reputation: 13
Startup Script Needs to Run as Root


Hi all,

I've got a script I'd like to run as root when I log in. What's the best way to achieve what I'd like?

Should I have A or B.

A) Within 'Startup applications' have the machine run 'sudo ./myscript'. If so, will the machine grant it root rights to the startup applications list to run said script?

B) Within 'Startup applications' have the machine run './myscript'. Within myscript have each of the commands prefixed by 'sudo'? Again, will my machine allow the script to execute the commands as root even though I've not typed in my password?

I hope I've explained my problem clearly.

Thanks
 
Old 12-31-2012, 05:55 PM   #2
Habitual
LQ Addict
 
Registered: Jan 2011
Location: Youngstown, Ohio
Distribution: LM17.1/Xfce4.11.8
Posts: 7,166
Blog Entries: 10

Rep: Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979Reputation: 1979
Neither A nor B...

rc.local
no sudo necessary in the script or rc.local.
It will run as root and after all system service daemons have started and regardless if anyone logs in.

"Startup Applications" implies a desktop.

Last edited by Habitual; 12-31-2012 at 05:57 PM.
 
1 members found this post helpful.
Old 12-31-2012, 07:04 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Do you really mean 'when I login', or do you actually mean at boot time? If the former, you can add almost anything to your shell login script, probably $HOME/.bashrc. If it needs root privileges, you can either use sudo, or su. If you mean at boot time, then you can add code to /etc/rc.local, which always runs as root.

--- rod.
 
Old 01-01-2013, 02:23 AM   #4
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Hi,

The computer automatically logs in as a user, so to be honest, boot and login have no differential at this time to me.

I'll read up and see which best suits my purpose. Thanks for the guidance
 
Old 01-01-2013, 02:57 AM   #5
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Hi,

I added the commands from my script into /etc/rc.local, as it seemed the most appropriate, however it doesn't work. I then changed rc.local so that it executes the script by having the line: sh /path/to/my/script.sh. No joy

I know the script itself works, I just need it to be executed at start up or boot.

Thanks
 
Old 01-01-2013, 03:03 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,956

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
> The computer automatically logs in as a user

No it doesn't

> I then changed rc.local so that it executes the script by having the line: sh /path/to/my/script.sh.

Try to debug:

Code:
exec >/tmp/debug.log 2>&1
sh -x /path/to/my/script.sh
 
Old 01-01-2013, 03:13 AM   #7
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by NevemTeve View Post
> The computer automatically logs in as a user

No it doesn't
I hope I don't seem petulant, I certainly don't mean to come across this way, but from my understanding, it does. I switch the computer on and without striking a single key or clicking a button it arrives at the Desktop ready to use. Do we have two different understanding of what's taking place?

Quote:
Originally Posted by NevemTeve View Post
Try to debug:

Code:
exec >/tmp/debug.log 2>&1
sh -x /path/to/my/script.sh
I copied and pasted this into a command line and it appeared to freeze the screen? In a new terminal I executed the script:

Code:
$sh -x /path/to/my/script.sh
+ xset s off
+ xset -dpms
Is that what you're expecting to see?

Thanks

Last edited by NotAComputerGuy; 01-01-2013 at 03:16 AM.
 
Old 01-01-2013, 03:27 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,956

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
1. okay, you're right, so your system autmagically starts a session for a predefined user (hmmm... without asking for username and password?)

2. those line should go into the rc.local, too:

Code:
...
exec >/tmp/debug.log 2>&1
sh -x /path/to/my/script.sh
3. But in this case rc.local is not the right place, because it might run before the GUI starts.

4. So you might want to go back to sudo (btw: what does your script actually do, that requires root access?)
 
1 members found this post helpful.
Old 01-01-2013, 05:29 AM   #9
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Hi,

Script contains this to prevent the display being put to sleep.
Code:
#!/bin/bash
xset s off
xset -dpms
As the computer is used only for watching Videos, it's annoying to have the screen turn off every 10 minutes. It works fine as long as I execute it as root.
 
Old 01-01-2013, 05:51 AM   #10
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Results of /tmp/debug.log
Code:
$cat /tmp/debug.log
+ xset s off
xset:  unable to open display ""
+ xset -dpms
xset:  unable to open display ""
I then tried adjusting it slightly.
Code:
xset -display :0 s off
xset -display :0 -dpms
Which gave a new error:
Code:
$cat /tmp/debug.log
+ xset -display :0 s off
No protocol specified
xset:  unable to open display ":0"
+ xset -display :0 -dpms
No protocol specified
xset:  unable to open display ":0"
Any suggestions or translations as to what it means?

Last edited by NotAComputerGuy; 01-01-2013 at 06:21 AM.
 
Old 01-01-2013, 07:49 AM   #11
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
dpms is only controllable by the user ... AFTER login.

It is not controllable by root - the X server restarts after a user is started and wipes out any such changes that MIGHT have been applied.

"No protocol specified" is an incomplete X server or application.

"Unable to open display" has two possibilities - no X server, or no access key. If this is running as root, I would suspect lacking access keys.

It should go in the autostart list.

Of course, that also depends on the environment - in some, the screen lock/blanking is done external to the X server, so it can depend on what you are using for a desktop.
 
1 members found this post helpful.
Old 01-01-2013, 07:57 AM   #12
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
I have no idea what much of that means. I've removed it from rc.local and added it (sudo sh /path/to/my/script.sh) to the list of startup applications within the environment itself. I'm using MATE, I think that was your question...

Unfortunately, despite the script in the list of things to be run at startup, it appears for whatever reason, it doesn't work. This is really frustrating. If I SSH into the computer and type 'sudo sh /path/to/my/script.sh' it works, but why can my computer not do that automatically for me?

Last edited by NotAComputerGuy; 01-01-2013 at 08:12 AM.
 
Old 01-01-2013, 08:26 AM   #13
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,956

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
Are you sure that it doesn't work if it is executed as user?
Use the debugging method I've shown, to see the error messages:

Code:
$ cat myscript
#!/bin/sh
exec >/tmp/myscript.log 2>&1
set -x
xset s off
xset -dpms
$ ./myscript
$ cat /tmp/myscript.log
 
1 members found this post helpful.
Old 01-01-2013, 08:33 AM   #14
NotAComputerGuy
Member
 
Registered: Jun 2012
Distribution: Linux Mint - Debian Edition
Posts: 348

Original Poster
Rep: Reputation: 13
Quote:
Originally Posted by NevemTeve View Post
Are you sure that it doesn't work if it is executed as user?
Code:
$cat /tmp/myscript.log
+ xset s off
+ xset -dpms
That's the output of the log. I'm just downloading an ISO at the moment, but as soon as that's done, I'll reboot the machine and check to see if it works. I will post back.
 
Old 01-01-2013, 08:46 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 2,956

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
I think the lack of the error messages means success. Now try this from different contexts: with/without sudo, from terminal/from startup etc.
 
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
[SOLVED] Run command as root on startup demencia Linux - Newbie 6 05-14-2011 06:28 PM
Adding a startup script to be run at bootup - no root rights matar73 Linux - Newbie 2 09-18-2009 11:14 AM
Run Python script as root at startup StuL Linux - General 2 03-05-2008 06:33 PM
How do I run a startup script as a user other than root? Questor21 Linux - Software 4 04-08-2006 12:51 PM
How to run startup script/daemon as a user other than root? Akhran Debian 2 02-04-2006 02:20 AM


All times are GMT -5. The time now is 08:53 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