Search code examples
bashubuntu-12.04startupubuntu-server

Run a bash script on startup, before login as a user


I am trying to have my Ubuntu 12.04 LTS server run a bash script I have to start a Minecraft server on start up, prior to log in but as user minecraft. I can have it run as root by placing the following in /etc/rc.local

bash /path/to/script/script.sh

which runs the script as root, I have tried the following in /etc/rc.local

su -c `bash /path/to/script/script.sh` minecraft

but to no avail. Can anyone tell me what I am doing wrong or should be doing instead? The first line of my script is

#!/bin/bash

in case it is important. Thanks much!


Solution

  • Try

    su minecraft -c '/bin/bash /path/to/script/script.sh &'
    
    • The user should be the first argument to su.
    • You should use quotes and not ticks for the command argument (-c)
    • You may want to consider using su -l minecraft to have the script run in an environment which would be similar to that if the user minecraft logged in directly.

    Give this a shot and let me know if it works.