1

I need to run a script at startup, the script needs to run under a certain user, not as root, i tried adding the script to /etc/rc.local like this:

#!/bin/sh -e
#
# rc.local
#
#!/bin/sh
su - tue -c "/home/tue/main"
su - tue -c "/opt/craftbukkit/start.sh"

The first line starts a program main in /home/tue, this is working fine. The second line is my script, but for some reason this isn't being run?

1

Take into account that:

  1. second command could only start when the first terminates (or go to background);

  2. -e option will stop the execution if a command returns a nonzero exit code.

1

Try something like this:

#!/bin/sh -e
#
# rc.local
#
#!/bin/sh
su - tue -c "/home/tue/main" &
su - tue -c "/opt/craftbukkit/start.sh" &

This will start both processes in the background, so that the rest of the script can continue normally. If this isn't what you want, please explain your situation more fully.

0

Are you sure that your command isn't being run? In my situation, I found that it was being run, but there was an error which caused it to stop. You can figure out if you're getting an error by using something like nohup:

nohup bash /home/ubuntu/myscript.sh > /home/ubuntu/error_log.txt 2>&1 &

...where bash /home/ubuntu/myscript.sh is the actual command I want run. Check the error log and see what you find.

You might be running something like java or a utility like drush, and your $PATH variable doesn't know how to find it at the time /etc/rc.local is run, which was my problem. The $PATH variable is very minimal at the time of running this script.

If you want to see what's in your $PATH variable at the time of running it, put this in your /etc/rc.local file:

echo $PATH > /home/rc_local_path.log

..And check the log file after rebooting. In my case (and probably yours), the path variable was the following:

/sbin:/usr/sbin:/bin:/usr/bin

The error I got was along the lines of drush - command not found ... so I typed command -v drush and figured out that it was installed into /usr/local/bin, which was not in the $PATH at the time of running the script, so I made a symlink to it via ln -s /usr/local/bin/drush /usr/bin/drush. Now my script runs fine without error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.