Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Alright so I have a Minecraft server running for a couple of friends and I. Right now every time my CentOS box restarts, which isnt a lot, (the computer the server runs off of) I need to manually run the .sh script that starts the server. I want this make this process automatic, however I do not want to make it a service and I also want to be able to see the server console whenever I want to.
What I want-
I would like to create a .sh script to run on startup which would make a terminal open when logged in, then execute another .sh script (this other .sh script is the script I currently use the start the server manually)
How would I do this? Thanks.
For the portion of your question concerning being able to see the console whenever you want to, screen would be a great program to look into for this. I keep meaning to learn how to do more with it.
Quote:
What is GNU Screen? it is a terminal multiplexer and you can run multiple console-based applications simultaneously. The best part of it is that you can leave it running on remote machines and come back to pick up your console sessions.
Here is an example of using it to start the program top, name the session foo, and immediately detach the screen.
Code:
screen -d -m -S foo top
-d -m starts screen in detached mode
-S foo names the session foo
top name of program running
to reattach to the screen session.
Code:
screen -r foo
to detach it again, ctrl A-D.
*edit* added this:
A less powerful, but simpler way, would be to background the process and redirect output to a file, you could view output by viewing the file, but you would be unable to interact with the console. In this particular situation, it may be better, because there is no need to interact with the minecraft script after its started, but its a good excuse to learn screen.
you can put commands in /etc/rc.local and they will run when the system boots up.
or you can put stuff in ~/.profile or ~.bash_profile and they will run when that user logs in.
or you can put an @reboot entry in your crontab and the command will run after every reboot.
So I did not have a .profile folder so I made my own, I assume now I just drop in a .sh file to tell it to execute the other sh script? Is that correct?
Quote:
Originally Posted by nd7rmn8
For the portion of your question concerning being able to see the console whenever you want to, screen would be a great program to look into for this. I keep meaning to learn how to do more with it.
Here is an example of using it to start the program top, name the session foo, and immediately detach the screen.
Code:
screen -d -m -S foo top
-d -m starts screen in detached mode
-S foo names the session foo
top name of program running
to reattach to the screen session.
Code:
screen -r foo
to detach it again, ctrl A-D.
*edit* added this:
A less powerful, but simpler way, would be to background the process and redirect output to a file, you could view output by viewing the file, but you would be unable to interact with the console. In this particular situation, it may be better, because there is no need to interact with the minecraft script after its started, but its a good excuse to learn screen.
That sounds really cool, at the moment I do not have too much time to experiment with it though, maybe in the future.
So I did not have a .profile folder so I made my own, I assume now I just drop in a .sh file to tell it to execute the other sh script? Is that correct?
...
Yes .bash_profile I have. I do not know if I specified but I am using CentOS 6.5 (sorry if I didnt). I just realized that what I asked to do above would still prompt me to not be able to access the console. I want to make a .sh file and inside that .sh file have commands that open the terminal and then paste in code into the terminal (if possible). Is the way I am trying to do it hacky/not recommended? Should I find some time to work with screen?
not really sure what you are trying to do (or why ?) but if all you want to do is have a terminal started when you login try putting this in the last line of .bash_profile:
Code:
/usr/bin/gnome-terminal &
and then you can manually copy-paste whatever commands you need into that window.
not sure what the syntax is for gnome-terminal, but for xterm, if you wanted to open up a new terminal window and run a command in it, lets use top for this example, it goes like this.
Code:
xterm -e top &
so just replace top with the startup script for minecraft
Sorry for not being clear. To start my server, I usually launch up a terminal then type in a java command which starts the server (I also sometimes launch a terminal, cd to the directory where my .sh script and then in the terminal run the .sh script). I am basically trying to do this but automatically, would screen be a better option?
not sure what the syntax is for gnome-terminal, but for xterm, if you wanted to open up a new terminal window and run a command in it, lets use top for this example, it goes like this.
Code:
xterm -e top &
so just replace top with the startup script for minecraft
Code:
/usr/bin/gnome-terminal -e top &
i wasnt sure from the op's description if that is what they wanted.
Sorry for not being clear. To start my server, I usually launch up a terminal then type in a java command which starts the server (I also sometimes launch a terminal, cd to the directory where my .sh script and then in the terminal run the .sh script). I am basically trying to do this but automatically, would screen be a better option?
heres an example of what i do to automatically start things on my pc:
Code:
[schneidz@hyper ~]$ cat /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
# rm /etc/X11/xorg.conf
# ifconfig eth3 down && ifconfig eth3 up
#/home/schneidz/nfs-w/leaderboard.ksh &
note: it is commented out because it no longer runs but you get the idea.
screen could be overkill for this particular situation. I know I suggested it earlier, and it is useful, but since there is no need to interact with the script after its started... here is a simpler minecraft server startup script for an example. It is actually what I use on mine.
the "-Xmx1024M -Xms1024M" tell java how much ram to commit to this server. 1GB in my case.
the "&" at the end backgrounds the process
the ">> /path/to/the/mc.log 2>&1" sends all output to a file (/path/to/the/mc.log)
After this is started, you can view the output of the server by either of these 2 following ways.
Code:
cat /path/to/the/mc.log
or
tail -f /path/to/the/mc.log
"cat" just displays everything that is in the file.
"tail -f" will update the display in the terminal window whenever new text is added.
With this script, I would just start it on boot by placinig it in rc.local or whatever your distro uses. Then, anytime you want, you can monitor the output of the server by typing one of the above commands in a terminal.
screen could be overkill for this particular situation. I know I suggested it earlier, and it is useful, but since there is no need to interact with the script after its started... here is a simpler minecraft server startup script for an example. It is actually what I use on mine.
the "-Xmx1024M -Xms1024M" tell java how much ram to commit to this server. 1GB in my case.
the "&" at the end backgrounds the process
the ">> /path/to/the/mc.log 2>&1" sends all output to a file (/path/to/the/mc.log)
After this is started, you can view the output of the server by either of these 2 following ways.
Code:
cat /path/to/the/mc.log
or
tail -f /path/to/the/mc.log
"cat" just displays everything that is in the file.
"tail -f" will update the display in the terminal window whenever new text is added.
With this script, I would just start it on boot by placinig it in rc.local or whatever your distro uses. Then, anytime you want, you can monitor the output of the server by typing one of the above commands in a terminal.
This seems to be a pretty good method, thanks, also, because the computer isnt constantly outtputting the text into terminal, does this save CPU usage or no because it needs to still get written to file?! I am starting to get interested in screen though, but im not actually sure what it is. Is it something where I just install the package and keeps SSH sessions connected even while exiting the client? I have seen some Garry's Mod Server tutorials recommend to get screen but I never saw the point of it. I still dont see the point of ME personally using it as my servers dont really run 24/7 (except for Minecraft) although I know it is a somewhat helpful tool.
Question: how would I properly turn off the server, I dont want to shut it off with some hacky method. Should I learn how to use screen?
it saves some cpu usage and ram, mostly just because you don't have to have a terminal window open. More importantly you dont have to worry about accidentally closing out the terminal and crashing the server.
The best way to shutdown a minecraft server is to issue the commands /save-all and then /stop from within the game. You need to have administrator privileges though. In order to gain administrator priviliges, add your name to the ops.txt file in the games directory and restart. Here are the 2 relevant web pages. http://minecraft.gamepedia.com/Administrator http://minecraft.gamepedia.com/Comma...-only_Commands
To force a process to quit, you can find the process's pid and then kill it using the kill command. But, this doesn't kill programs gracefully, it may not save all changes before it exits. Here is an example of what it would look like on my server if I was to shut down minecraft this way. I use the grep command to filter out any process that doesn't have java in the name. If the kill command doesn't work, you may have to add sudo in front of it or use "kill -9" in order force it to stop.
screens main benefit in my opinion, is having multiple terminals available in one. Also the ability to open up that screen in another terminal at a later date and from another computer or session. Not necessary for minecraft servers as the server's console is not interactive (as far as I know), but say the server's console was interactive and you needed to log in remotely and type in a command inside that console, screen would allow you to do that.
im sure i messed up some of the jargon with that, hope you get the point though.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.