Automatic login to TTY1 on Ubuntu 10.10

Ask questions about dedicated servers here and we and other users will do our best to answer them. Please also refer to the self-help section for tutorials and answers to the most commonly asked questions.
Rhythmatic
A regular
A regular
Posts: 31
https://www.youtube.com/channel/UC40BgXanDqOYoVCYFDSTfHA
Joined: Tue Feb 22, 2011 9:32 am

Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

I've heard it has something to do with /etc/init/tty1.conf but I've been unable to figure it out. I want the server to come back from a scheduled (or unscheduled) restart and immediately log back in ready to execute scripts and programs. I don't want to be required to interact with the terminal in the event of a reboot or crash.
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

It looks like this would be a way of doing that: http://linux.koolsolutions.com/2009/04/ ... sole-mode/

But, you don't need it to auto-login to start your scripts and programs. You can edit /etc/rc.local, a script that is run automatically on each boot.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

So I could put my minecraft server script in rc.local and it would start up the script, ready for logins? I'll have to give that a try.
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

Yes, you should be able to do that. In the script, make sure to have everything that you would normally do manually, such as changing to the right folder.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

Sick. That should work then. Do I need to append an ampersand to the end of the line in the script so that it runs in the background, or is it implied, since it is starting before login, that is is run in the background?
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

If you use screen, you can tell it to start the screen detached, like this:

Code: Select all

screen -AmdS screenname yourcommandline
Otherwise, you would need the ampersand, yes.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

I just looked up the options that you are using there, and I'm a bit confused about the significance of each one. What would happen if I omitted them? Inversely, what occurs by including those arguments?

Code: Select all

-A             Adapt all windows to the new display width & height.
-m            ignore $STY variable, do create a new screen session.
-d             Detach the elsewhere running screen (and reattach here).
-S sockname   Name this session <pid>.sockname instead of <pid>.<tty>.<host>.
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

Together, these options mean that you are forcing a new screen session to start, giving it a name, and starting it in the detached state. If you resize the window (such as after re-attaching to it in an SSH session), it will also adjust itself to match the new dimensions.

"man screen" talks about the options separately, and also talks about "-d -m" in combination:

Code: Select all

       -d -m   Start  screen  in "detached" mode. This creates a new session but doesn't attach to it. This is
               useful for system startup scripts.
You could leave off "A" or "S" and it would still work (if you also eliminated the screen name, when removing "S"). The "md" bit is required.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

That clears it up a good bit. Thanks.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

As what user is the script executed if it is run via rc.local? The root user? Something else?
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

It executes as root, but you could make it execute as a different user by using "su" or "sudo".
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

So then at the beginning of the script just have a "su - otheruser" and everything will go as expected? What about passwords and whatnot?
User avatar
Edge100x
Founder
Founder
Posts: 12957
Joined: Thu Apr 18, 2002 11:04 pm
Location: Seattle
Contact:

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Edge100x »

I'd recommend doing something more like:

Code: Select all

su user -l -c "screen -AmdS yourscreen /whateverpath/whateverscript"
Where "user" is the user you wish the script to run as, "yourscreen" is what you want the screen to be called under that user, and "/whateverpath/whateverscript" is the path to the script that will start your server.

You could then add further lines, potentially to run other things, as other users.

As the root user, you do not need to know the user's password to run something under that user.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

Thanks. That helped. I'm writing up my Minecraft server backup, restore, and restart scripts. I'll post them up for the benefit of the community once I finish them.
Rhythmatic
A regular
A regular
Posts: 31
Joined: Tue Feb 22, 2011 9:32 am

Re: Automatic login to TTY1 on Ubuntu 10.10

Post by Rhythmatic »

I'd appreciate constructive critique, if you don't mind.


Main Script
Starts the server, and backs up and restores the map in the event of a hardware crash.

Code: Select all

#!/bin/sh
##Script for rc.local to be run at VDS start.
##Starts server in a screen, restores most recent
##backup, and makes a backup when shutting down.


MC_PATH=/home/nathan/craftbukkit
OVERWORLD=world
NETHER=nether
AETHER=aether
BACKUP_PATH=backup
DATE=$(date +%Y%m%d%H%M)
FILENAME=$DATE.tar.gz
SCREEN_NAME="minecraft"
MAX_MEMORY=1.5

CreateBackup() {
        ##Create the backup directory if it does not exist
        if [ ! -e $MC_PATH/$BACKUP_PATH ]
        then
                mkdir $MC_PATH/$BACKUP_PATH
        fi
        ##Create a tarball containing world files, inventory data, and other tidbits
        tar -zcf $MC_PATH/$BACKUP_PATH/$FILENAME $MC_PATH/$OVERWORLD/region $MC_PATH/$OVERWORLD/players $MC_PATH/$OVERWORLD/level.dat
}

RestoreBackup() {
        ##Check to see if there are any existing backups
        if [ -e $MC_PATH/$BACKUP_PATH/*.tar.gz ]
        then
                ##If one or more exist, remove existing map
                rm -rf $MC_PATH/$OVERWORLD/*
                ##and then restore the backup
                tar -zxf $MC_PATH/$BACKUP_PATH/`ls -tr | tail -1` $MC_PATH/$OVERWORLD/
        fi
}

StartServer() {
        cd $MC_PATH; screen -AmdS $SCREEN_NAME java -Xincgc -Xmx${MAX_MEMORY}G -jar craftbukkit.jar nogui; sleep 1
}


RestoreBackup
StartServer
CreateBackup
Backup Script
Backs up the server once per hour. Includes announcements to the server.

Code: Select all

#!/bin/sh
##Script to periodically backup the world folder.
##To be used within a cron job.
##Executes "backup warning", "save-off", "save-all",
##backup, "save-on", "completion notice" in that order.

DATE=$(date +%Y%m%d%H%M)
MC_PATH=/home/nathan/craftbukkit
BACKUP_PATH=backup
FILENAME=$DATE.tar.gz

##Create the backup directory if it does not exist
if [ ! -e $MC_PATH/$BACKUP_PATH ]
then
        mkdir $MC_PATH/$BACKUP_PATH
fi
##Prepare the server for backing up
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-off\r"`"
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-all\r"`"
##Announce the backup
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say [AUTOMATED] Creating a backup of the map.\r"`"
##Create a tarball containing world files, inventory data, and other tidbits
tar -zcf $MC_PATH/$BACKUP_PATH/$FILENAME $MC_PATH/$OVERWORLD/region $MC_PATH/$OVERWORLD/players $MC_PATH/$OVERWORLD/level.dat
##Return server to normal
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "save-on\r"`"
##Announce completion
screen -S $SCREEN_NAME -p 0 -X stuff "`printf "say [AUTOMATED] Backup has been completed.\r"`"
rc.local
Starts the Main Script when the hardware comes online.

Code: Select all

#!/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.
su nathan -l -c "/home/nathan/main.sh"
exit 0
Post Reply