Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Run Script as specific User on Startup (Server 18.04)

  1. #1
    Join Date
    Jun 2018
    Beans
    4

    Run Script as specific User on Startup (Server 18.04)

    Hello, I need to have a specific script run from a users home directory, kind of like having a service startup, that I do not want run as root. It needs to be run on startup, and I'm having one heck of a time figuring out a way to do it in Ubuntu. Ideally I'd like to be able to take over it's terminal if I login as that user or another user, so I can interact with the script when need be directly. Anyone got any solution? Thanks.

  2. #2
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Run Script as specific User on Startup (Server 18.04)

    Welcome to the forums.

    boot and logins aren't connected. Booting can happen 50 hrs before anyone logs in.

    What this means is that you have to pick - non-interactive scripting or interactive which is launched by a user AFTER they login. Pick and there are solutions for both.

  3. #3
    Join Date
    Jun 2018
    Beans
    4

    Re: Run Script as specific User on Startup (Server 18.04)

    Quote Originally Posted by TheFu View Post
    Welcome to the forums.

    boot and logins aren't connected. Booting can happen 50 hrs before anyone logs in.

    What this means is that you have to pick - non-interactive scripting or interactive which is launched by a user AFTER they login. Pick and there are solutions for both.
    What I am aiming for is to get an application running at boot without any user intervention. Ideally I'd like a user to be able to access the terminal it's running it later if they login and take control of it, but this isn't strictly necessary if it cannot be done (which would surprise me if that were the case). So I would say that I'm looking for non-interactive.

  4. #4
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Run Script as specific User on Startup (Server 18.04)

    Quote Originally Posted by grimmspector View Post
    What I am aiming for is to get an application running at boot without any user intervention. Ideally I'd like a user to be able to access the terminal it's running it later if they login and take control of it, but this isn't strictly necessary if it cannot be done (which would surprise me if that were the case). So I would say that I'm looking for non-interactive.
    If you just want to run a script at boot, it cannot be interactive and the userid cannot have encrypted HOME directory, then you can just use anacron @reboot to run. Scripts of this sort shouldn't assume **any** environment variables. HOME will not be set. The PATH and LD_LIBRARY_PATH will be minimal. Don't expect any other env to be set. https://askubuntu.com/questions/3356...oot-in-crontab has examples. Inside the script, you can use su -l {userid} -c {command} to run any process under a different userid. Check the manpage for more details.

    Key thing is, there cannot be any GUI programs involved. They will not work.

  5. #5
    Join Date
    Feb 2009
    Location
    Dallas, TX
    Beans
    7,790
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Run Script as specific User on Startup (Server 18.04)

    Hi grimmspector. Welcome to the forum

    It is easier for us to help you if you give us some specifics.

    I think this may help: https://ubuntuforums.org/showthread.php?t=1809501

    Take a look at post #14 for part of the solution. Use crontab with the special string '@reboot' if you want the session to start at boot time.

    Let us know how it goes. If these does not help, please give us more information, examples, etc.
    Best Regards.

  6. #6
    Join Date
    Jun 2018
    Beans
    4

    Re: Run Script as specific User on Startup (Server 18.04)

    Quote Originally Posted by TheFu View Post
    If you just want to run a script at boot, it cannot be interactive and the userid cannot have encrypted HOME directory, then you can just use anacron @reboot to run. Scripts of this sort shouldn't assume **any** environment variables. HOME will not be set. The PATH and LD_LIBRARY_PATH will be minimal. Don't expect any other env to be set. https://askubuntu.com/questions/3356...oot-in-crontab has examples. Inside the script, you can use su -l {userid} -c {command} to run any process under a different userid. Check the manpage for more details.

    Key thing is, there cannot be any GUI programs involved. They will not work.
    No need for GUI anything, in fact the server doesn't have GUI anything, as it's a server install with no GUI added. Everything is command line.

    The issue here is that the su command always asks for a password when trying to run something as a user, even if I'm in root. So how would that work at boot?


    Quote Originally Posted by papibe View Post
    Hi grimmspector. Welcome to the forum

    It is easier for us to help you if you give us some specifics.

    I think this may help: https://ubuntuforums.org/showthread.php?t=1809501

    Take a look at post #14 for part of the solution. Use crontab with the special string '@reboot' if you want the session to start at boot time.

    Let us know how it goes. If these does not help, please give us more information, examples, etc.
    Best Regards.
    I don't know how much more specific I can be beyond saying I want to run a script under a specific user, and ideally want to be able to then interact with that console later if possible (like the screen command for instance).

  7. #7
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: Run Script as specific User on Startup (Server 18.04)

    Did you look at the links? There are clear examples.

    Root won't be asked for a password to run any local process as another userid. If you try to run a script over NFS, then root is blocked for security, by default.

    Perhaps if you post what you've put into the crontab AND tell us which crontab you are using (there are at least 3 different places), then we might be able to help.

    If you want to interact with a running program, that program will need to allow remote control/commands in some manner. That isn't too hard, just open a FIFO (named pipe?) and have the process read from it with a simple parser for the specific commands. Then you can make another tool that writes to the FIFO with approved commands ... or any process like "cat" can be used as well. If you want non-local control, which is usually bad idea due to security, you can do the same with sockets.

  8. #8
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Run Script as specific User on Startup (Server 18.04)

    Root can run commands as other users via the "su" command. I believe if you create or edit /etc/rc.local and add this line to it:

    Code:
    su -c /path/to/your/script username
    it will run the script as "username". Commands place in /etc/rc.local are executed at the end of the boot sequence. The script must be marked executable by that user, of course.
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

  9. #9
    Join Date
    Jun 2018
    Beans
    4

    Re: Run Script as specific User on Startup (Server 18.04)

    Quote Originally Posted by SeijiSensei View Post
    Root can run commands as other users via the "su" command. I believe if you create or edit /etc/rc.local and add this line to it:

    Code:
    su -c /path/to/your/script username
    it will run the script as "username". Commands place in /etc/rc.local are executed at the end of the boot sequence. The script must be marked executable by that user, of course.
    Ok, well that won't allow the interactivity, so before I go that route I just have to ask then, is there a way I can on boot have it autologin a specific user and have that user in their terminal initiate a script? Then I can setup a user that has no ability to do anything but use that script and it should be fine, and if I remotely connect to that specific terminal session I can interact with it.

  10. #10
    Join Date
    Nov 2008
    Location
    Boston MetroWest
    Beans
    16,326

    Re: Run Script as specific User on Startup (Server 18.04)

    Is the "interactivity" limited to a specific set of commands? If so you could include an "expect" script in your main script to respond to the prompts.

    Is there no way to start this process so that it doesn't require console input?
    If you ask for help, do not abandon your request. Please have the courtesy to check for responses and thank the people who helped you.

    Blog · Linode System Administration Guides · Android Apps for Ubuntu Users

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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