Sign up ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to be able to launch a script in a separate screen command session as a separate user when I run a .sh script.

Here is what I think it may be in the script:

#I am also starting xampp here, as that is what runs the webserver on this server
sudo /opt/lampp/lampp start 
screen sudo -u minecraft /home/minecraft/mc1/
screen sudo -u minecraft /home/minecraft/mc2/

As you can see, in this script there are several instances of 'sudo'. Sudo requires that I input a password, I am planning on making this script automatically run when the server starts up. The other issue is that screen may have a login prompt.

More information: Operating system: Ubuntu server 12.04 LTS

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Just let the minecraft user start the session itself when you start the machine.

  1. Run sudo -u minecraft crontab -e to edit the users crontab.

  2. Add @reboot screen /home/minecraft/mc1/ to make cron start the screen session for the user when the system restarts.

How to handle that login prompt depends on the prompt itself. Without more information this will be hard to answer and it should probably have its own question.

To address your questions from the comments:

  1. No, this doesn't require you to log in as the minecraft user. You're just setting it up so that when the system starts, the system itself will take care of starting the screen session as the minecraft user.

    By using sudo -u minecraft, you're executing a command as the minecraft user. So, just as you've edited the users cron table, you can then later re-open the screen session by running sudo -u screen -r.

    You see, the screen session will then be started by the minecraft user. When previously you started the screen session yourself and then, inside the screen session, started your script as the minecraft user.

    Now, you're simply switching into the minecraft user context a step earlier.

  2. No, this behavior isn't exclusive to restarts. There isn't really a difference between a "restart" and a "normal startup" anyway ;)

share|improve this answer
What's crontab, and cron? I have heard the words before, but I do not know what they mean. The login prompt is just a regular login prompt that shows up when I open a new screen using screen – john01dav Mar 31 '14 at 12:42
cron is a system to run tasks at specific times (or events such as a reboot). crontab is the tool that allows you access to the table of cron entries for a given user. screen itself will not prompt for a log. sudo will prompt for your password, and you're running that as the first command in your new screen session. Starting the session through cron makes this obsolete. – Oliver Salzburg Mar 31 '14 at 12:44
Ok, does this mean that I will need to log in as the minecraft user? I currently have it setup so that several users (one for minecraft, webserver, mc panel, etc) all can only read/write in their own directory, but there is one account that can read and write in any of these directories (used for ftp managing, and when I access the system via the terminal), I want to still be able to use this user. Also you say "when the system restarts" does this only include a restart, or does it also include normal startups? – john01dav Mar 31 '14 at 21:58
@john01dav I added some more information to the answer – Oliver Salzburg Apr 1 '14 at 11:59

The proper way to start a daemon at startup is to use init scripts in /etc/rc[0-6].d (found in /etc/rc.d/rc[0-6].d on some systems). A cron job with @reboot will work, but cron is intended for scripts that execute once and then exit rather than for spawning daemons.

In your example, you're trying to start LAMPP from cron. Instead you should make symbolic links from /etc/rc?.d pointing to /opt/lampp/lampp, as follows:

sudo ln -s /opt/lampp/lampp /etc/rc0.d/K90lampp
sudo ln -s /opt/lampp/lampp /etc/rc1.d/K90lampp
sudo ln -s /opt/lampp/lampp /etc/rc2.d/S90lampp
sudo ln -s /opt/lampp/lampp /etc/rc3.d/S90lampp
sudo ln -s /opt/lampp/lampp /etc/rc4.d/S90lampp
sudo ln -s /opt/lampp/lampp /etc/rc5.d/S90lampp
sudo ln -s /opt/lampp/lampp /etc/rc6.d/K90lampp

Now LAMPP will be started with /opt/lampp/lampp start whenever you enter runlevels 2 (the default startup state on Ubuntu), 3, 4, and 5; it will be stopped with /opt/lampp/lampp stop when you enter single user mode (rc1), shut down (rc0), or reboot (rc6). This is determined by the first letter of the filename: S causes "start" to be passed to the linked script; K runs the script with "stop" instead.

An in-depth explanation of runlevels is beyond the scope of a SU explanation, but you can find more info here:

And, for the sake of completeness, details on the ln command:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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