PDA

View Full Version : [ubuntu] >:O auto starting things in BASH



B34ST1Y
August 17th, 2010, 04:02 PM
Excuse the grumpy face in the title, but I am pulling my HAIR OUT trying to get a few simple commands to automatically issue when my Ubuntu machine starts up.

I have checked other threads, and tried several things, from editing /etc/rc.local to allowing sudo without a PW for a specific script - nothing is seeming to work.

I have several miscellaneous commands that need to issue when the computer boots, and they need to be run as different users - some as root, and some as a different user.

Can someone help me out? Or at least point me to a tried and tested tutorial? I don't want to have to use the GUI at all. There is a way, I know there is.

Zorgoth
August 17th, 2010, 04:26 PM
to run a command as another user, you can use sudo -u.

B34ST1Y
August 17th, 2010, 04:29 PM
Zorgoth - How can I achieve this without user interaction? I need (for example) a VNC server to startup with the user "owner" automatically when the server starts up, with no user interaction.

B34ST1Y
August 18th, 2010, 05:10 PM
Does anyone know?? It needs to be 100% unmanned and start up automatically with the system.

nebileix
August 18th, 2010, 05:19 PM
Try under System->Preferences->Startup Applications

Click add and enter command in there.

B34ST1Y
August 18th, 2010, 05:22 PM
Nebileix -- I need to do this all without a gui, please :)

nebileix
August 18th, 2010, 05:25 PM
Nebileix -- I need to do this all without a gui, please :)

Then try upstart.

It is the default to run services during boot for lucid.

http://upstart.ubuntu.com/getting-started.html

or

http://wiki.frugalware.org/index.php/Upstart_Job_HOWTO

B34ST1Y
August 18th, 2010, 05:27 PM
Will this also work for Red Hat? I'm looking for the generic Linux autostart-a-script 100% unmanned. There has to be one....

nebileix
August 18th, 2010, 05:33 PM
Will this also work for Red Hat? I'm looking for the generic Linux autostart-a-script 100% unmanned. There has to be one....

If you're using ubuntu lucid, then it is upstart.

As for redhat, i think they are still using rc to run jobs at different runlevel.(Not very sure thou)

B34ST1Y
August 18th, 2010, 05:39 PM
ok, and then how can I get a script to run as userX without asking for a password?

nebileix
August 18th, 2010, 05:46 PM
ok, and then how can I get a script to run as userX without asking for a password?

Please refer to Zorgoth reply or read the man page by


$ man sudo

P1ta
August 18th, 2010, 06:19 PM
Nebileix -- I need to do this all without a gui, please :)

add your script to /etc/rc.local

B34ST1Y
August 19th, 2010, 03:25 PM
add your script to /etc/rc.local


Do I just dump the path to it into the rc.local file anywhere? I see a lot of what looks like code.

B34ST1Y
August 19th, 2010, 04:35 PM
Ok, I dug up more of a reason why it "wasnt running" ....I dumped the output of the startup commands to a log file, and now its showing something like this (Tightvncserver output):


tightvncserver: The HOME environment variable is not set.


Now, I know rc.local is already running commands as root, correct? So it's trying to run tightvncserver, but bombing out because it cant load a profile right? How do I load a profile in the script? :-D

B34ST1Y
August 20th, 2010, 02:57 PM
Any help on this last issue would help my overall problem a lot :)

AlphaLexman
August 20th, 2010, 03:39 PM
The HOME environment variable is the current users home directory.

If I have followed the thread correctly, the script is run without any user input, and each user will have a different value of the HOME variable.

If you just need the script to execute for a certain user, you could edit the script and change the HOME variable to [ /home/username/ ].

B34ST1Y
August 20th, 2010, 03:42 PM
Can you explain more? My "script" is one line of code - "tightvncserver" and that's it.

B34ST1Y
August 20th, 2010, 04:04 PM
this is the contents of my script:



#!/bin/bash
#!/usr/bin/tightvncserver
HOME=/home/vncuser
tightvncserver




I appended the HOME line to it, but after rebooting and checking the log -- it is still showing the same error. This script is being called from /etc/rc.local and it it the ONLY line in the script. /etc/rc.local looks like this:


#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
sh /etc/init.d/tightvnc.sh > /var/tightvnclog.log 2>&1

Spice Weasel
August 20th, 2010, 04:07 PM
Why don't you simply add the commands to your bashrc?

It's either at "~/.bashrc" for one user or "/etc/bash.bashrc" for global.

B34ST1Y
August 20th, 2010, 04:12 PM
Why don't you simply add the commands to your bashrc?

It's either at "~/.bashrc" for one user or "/etc/bash.bashrc" for global.


For the sake of my original need, there will most likely be things running as multiple users. Wouldn't this create a conflict when the home path is hard coded to another user's path?

Spice Weasel
August 20th, 2010, 04:18 PM
For the sake of my original need, there will most likely be things running as multiple users. Wouldn't this create a conflict when the home path is hard coded to another user's path?

This is why I would recommend using different user's bashrc to launch the programs that you need to run on that user. I'm not so sure how it would work out since I haven't ever tried something like this before, so I'm not sure about conflicts.

I'd try doing this in a VM first if you're unsure.

B34ST1Y
August 20th, 2010, 04:21 PM
The server isn't in a production environment yet, so it's still OK to test things out. I added the line


HOME=/home/vncuser

to /etc/bash.bashrc (at the top), but it's still giving the same error in /var/tightvncserver.log :mad::mad::mad:

Spice Weasel
August 20th, 2010, 04:24 PM
I'm not sure what you're trying to do, but what about "su vncuser" in replace of changing the home directory?

E: This is in the script, of course. Putting "su" in your bashrc is a bad idea. :P

B34ST1Y
August 20th, 2010, 04:37 PM
well, adding that got rid of the 'no HOME path' error --- but now there's a whole new slew of things showing in the error log


keyctl_search: Required key not available
Perhaps try the interactive 'ecryptfs-mount-private'
/bin/sh: /bin/sh: cannot execute binary file
keyctl_search: Required key not available
Perhaps try the interactive 'ecryptfs-mount-private'

Spice Weasel
August 20th, 2010, 04:40 PM
That looks something like a permissions error, but I've got no idea what for.

B34ST1Y
August 23rd, 2010, 05:27 PM
It's probably something specific to the TightVNC program, but it's the standard APT package that comes with the distro. :(

nebileix
August 24th, 2010, 05:12 PM
Sorry been busy lately.

Why not use different tty for different user and run the command in their individual .bashrc.

Try HERE (http://www.linuxadda.com/2010/01/enable-automatic-login-in-ubuntu-server.html)