LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-15-2010, 02:40 PM   #1
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Rep: Reputation: 0
Write a script to open a terminal and run a few commands.


Hi im looking for someone who can direct me in the right derection i have a simple script i would like to write and dont know were to start.

This is what i am trying to do.
open a terminal.
go to directory of a file.
run another shell but with attributes at the end.

this is to start my game servers i have incase of a power failure. i will be putting this script in my startup items to run on start up.

this is how i do this when i set it up by myself no script.
open a terminal
cd to a file
then run a file with variables max players map exc.

non of this is for a root user.

just looking for help on writing a script to do this for three servers.
if anyone can help i would appreciate it.
thanks have a good day.
 
Old 11-15-2010, 02:53 PM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Are you really sure you need the "open terminal" part?

If so, the command would be "xterm -3 SOME_SHELL_SCRIPT":

http://www.linuxforums.org/forum/mis...e-command.html

Last edited by paulsm4; 11-15-2010 at 02:54 PM.
 
Old 11-15-2010, 03:18 PM   #3
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by paulsm4 View Post
Are you really sure you need the "open terminal" part?

If so, the command would be "xterm -3 SOME_SHELL_SCRIPT":

http://www.linuxforums.org/forum/mis...e-command.html
i would imagine i would. i wont be by the comp to open a terminal from the gui interface.

and also how do i write the script? i know im a noob. ive been looking at scripts for a couple of hours. ive wrote basic batch files for windows but linux is different.

i do belive it will need to be a bash script if that makes since.
i am a noob to linux. any more help would be appreciated.
 
Old 11-16-2010, 08:21 AM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,090

Rep: Reputation: 41
sounds like you need to make a service/init script...
 
Old 11-16-2010, 09:10 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by jmdennis1987 View Post
i will be putting this script in my startup items to run on start up.
Boot scripts or desktop autostart?
 
Old 11-16-2010, 04:38 PM   #6
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Boot scripts or desktop autostart?
hey thanks for your replies. i am running a free version of suse and i use the gui. im slowly learning linux i only use it for my game servers so i havent had mch time to actually sit and learn how to use linux w/out the gui.

what i am doin is using suse to run three game servers i keep the pc at my dads shop to use his internet. every now and then the power goes out and i have to go start the pc and start the servers. Im trying to avoid this. ill have the bios reboot after power failure but need a script to run the couple of comands in a terminal to start my servers. and to make it easier for when i resatrt the pc they will restart on their own.

i am hopeing that i or some one will help. can write up some kind of script that when the pc starts and the gui loads (like windows) that it will start up a terminal ( i useually start three individual terminals and start each server from each one)and run a few commands one for each server to start. after a terminal is open its two commands and the server runs. so i need a script to open a terminal. go to derectory. run a shell script. so im wondering can this be done?

i need it to open a terminel
cd to derectory
run a shell that starts my server from in that derectory.

here are the commands i use to run my server in a terminal:
cd hlds/orengebox (hit enter)
/.srcds_run -game tf -ip 000.000.000.000 -port 00000 -maxplayers 24 -map ctf_2fort -autoupdate (hit enter)

server starts and is running... sounds easy but i dont know what to do.

repeat three times but with three dif derectories one for each server. and a seperate terminal for each server.

if this is posible i would pay some one to help me or write one for me. asking alot i know.
all i have is paypal

thanks everyone.
 
Old 11-16-2010, 05:07 PM   #7
MrUmunhum
Member
 
Registered: May 2006
Location: Mt Umunhum, CA, USA
Distribution: Debian/ Fedora
Posts: 409

Rep: Reputation: 33
Quote:
Originally Posted by jmdennis1987 View Post

here are the commands i use to run my server in a terminal:
cd hlds/orengebox (hit enter)
/.srcds_run -game tf -ip 000.000.000.000 -port 00000 -maxplayers 24 -map ctf_2fort -autoupdate (hit enter)
You need to know at what init level your servers are running in?
grep initdefault /etc/inittab
# 0 - halt (Do NOT set initdefault to this)
# 6 - reboot (Do NOT set initdefault to this)
id:5:initdefault:
This shows my default init level to be 5.

Now create a file called /etc/rcx.d/S99mystarts:
#!/bin/bash
/usr/bin/logger $*
(cd hlds/orengebox;/.srcds_run -game tf -ip 000.000.000.000 -port 00000 -maxplayers 24 -map ctf_2fort -autoupdate&)
Where rcx is your default init level.

Make file executable:
chmod +x /etc/rcx.d/S99mtstarts
That should do for a start. You may need to adjust the user ID to get the environment set up right?
 
Old 11-16-2010, 07:12 PM   #8
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MrUmunhum View Post
You need to know at what init level your servers are running in?
grep initdefault /etc/inittab
# 0 - halt (Do NOT set initdefault to this)
# 6 - reboot (Do NOT set initdefault to this)
id:5:initdefault:
This shows my default init level to be 5.

Now create a file called /etc/rcx.d/S99mystarts:
#!/bin/bash
/usr/bin/logger $*
(cd hlds/orengebox;/.srcds_run -game tf -ip 000.000.000.000 -port 00000 -maxplayers 24 -map ctf_2fort -autoupdate&)
Where rcx is your default init level.

Make file executable:
chmod +x /etc/rcx.d/S99mtstarts
That should do for a start. You may need to adjust the user ID to get the environment set up right?
hey thanks.

ok so i did what u said my my init level is 5.

i copied and pasted your code and pasted it in kword or something one of the word docs on suse. saved it like u said but it doesnt save no where.

this is what i did when i went to save i was in home dir. and just copied and pasted /etc/5.d/S99mystarts: in the save location thinking it would creat the folder/ dir needed then the file.

i hit enter and the text disapered but cant find the file any were.
now i was playing around do i need to put .sh at the end of the name so it will save it as a shell file?

after i did that i tried like u wrote to run it but couldnt acces it or find it. it said. so obviously im doin something wrong.

i was also playing around but couldnt get the script to run when i loaded it manually from the home dir.

saved the scipt in my home dir and
went to the home dir
"dir" hit enter. seen it was in the folder
chmod -x file
then /.file.sh to run it
but it does nothing do i have the wrong idea about this am i doing it wrong?

and also ill need it to open its own terminal and run those cammands one for each server. if u can get me goin in the right derection i can figure it out im sure just a lil lost on writing the code.

thanks again for all the halp.
 
Old 11-16-2010, 09:00 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by jmdennis1987 View Post
when the pc starts and the gui loads (like windows) that it will start up a terminal ( i useually start three individual terminals and start each server from each one)and run a few commands one for each server to start. after a terminal is open its two commands and the server runs. so i need a script to open a terminal.
Do you really need a terminal or do you just want a script to run that will start the games servers and restart them if they stop?
 
Old 11-16-2010, 10:55 PM   #10
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
Hey thanks. i might be using the wront word terminal. When i start the pc it goes to user joe @comcast. In gui. I click on the i guess the start botton like were on windows and i go and open a terminal the terminal opens in a white terminal that i can drag around the screen. And has joe@comcast> then a curser and i enter to commands to start my server. I do this for all three servers. I hope this awnsers your question.
Thanks for your help.
 
Old 11-16-2010, 11:59 PM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Terminal (more precisely terminal emulator) is the right word for what you are describing but you can do the same thing without a terminal using the boot scripts mechanism. You would not want to use that if there is output on the terminal that you want to see, for example messages about what the games server is doing and any problems it is encountering. That would, of course, only be useful if there was someone there to see them!

If starting the games servers from a boot script the same messages could be written to a log which has the advantage of preserving them after the terminal is closed. The boot script option is also more secure against, for example the games servers being shut down from the terminal.
 
Old 11-17-2010, 09:38 AM   #12
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Terminal (more precisely terminal emulator) is the right word for what you are describing but you can do the same thing without a terminal using the boot scripts mechanism. You would not want to use that if there is output on the terminal that you want to see, for example messages about what the games server is doing and any problems it is encountering. That would, of course, only be useful if there was someone there to see them!

If starting the games servers from a boot script the same messages could be written to a log which has the advantage of preserving them after the terminal is closed. The boot script option is also more secure against, for example the games servers being shut down from the terminal.
Hey thanks. I see terminal emulater.
Ok so yes i would like to keep the terminal emulater
Up so i can see its running and be able to change map. Se status. Exc.

So what i need then is a script that will open a terminal emulater. Amd start my servers as if i was doing it myself. And stay open so i can see the output messages. Im sure its a easy script.

Script.
Open termnal emululater
Cd to dir.
Run this command, /.srcds_run -game tf -ip 000.000.0.0 port 00000 -map ctf_2fort -maxplayers 24 -autoupdate.

Thanks again.
 
Old 11-17-2010, 09:47 AM   #13
linus72
Guru
 
Registered: Jan 2009
Location: Gordonsville-AKA Mayberry-Virginia
Distribution: PocketWriter/MinimalX
Posts: 5,057

Rep: Reputation: 328Reputation: 328Reputation: 328Reputation: 328
depending on what desktop your running you could start it when the desktop starts...

Quote:
Open termnal emululater
Cd to dir.
Run this command, /.srcds_run -game tf -ip 000.000.0.0 port 00000 -map ctf_2fort -maxplayers 24 -autoupdate.
so, example .bashrc

Code:
xterm -e 'cd /path-to/whatever && ./srcds_run -game tf -ip 000.000.0.0 port 00000 -map ctf_2fort -maxplayers 24 -autoupdate'
that should work maybe...
 
Old 11-17-2010, 10:23 AM   #14
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,524
Blog Entries: 27

Rep: Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175Reputation: 1175
Quote:
Originally Posted by linus72 View Post
that should work maybe...
I think xterm will close after executing that command and it may not be convenient to put the game server start command in .bashrc because it will start every time you log on which my not be convenient.

A better solution would be to start bash and tell it to use an runtime customisation file (rcfile) which has the game server start command in it:
Code:
xterm -e bash --rcfile ~/start_gs_1
and the contents of ~/start_gs_1:
Code:
cd <as required, presumably different for each of the 4 games servers>
/.srcds_run -game tf -ip 000.000.0.0 port 00000 -map ctf_2fort -maxplayers 24 -autoupdate
(that command does not look right; it requires .srcds_run to be a hidden file in the root directory; should it start with ./srcds_run ?)

@jmdennis1987: xterm is probably not the terminal you usually run and not as easy to use. Which Linux and/or desktop are you running? When we know that someone will be able to advise how to set up the autostart and which terminal emulator to start.
 
Old 11-17-2010, 12:06 PM   #15
jmdennis1987
LQ Newbie
 
Registered: Nov 2010
Location: Vancouver, WA
Posts: 28

Original Poster
Rep: Reputation: 0
"(that command does not look right; it requires .srcds_run to be a hidden file in the root directory; should it start with ./srcds_run ?)"

yes you are right. i have the code saved so i copy and past it to start my servers. and i was on my phone last night when i replied.

"@jmdennis1987: xterm is probably not the terminal you usually run and not as easy to use. Which Linux and/or desktop are you running? When we know that someone will be able to advise how to set up the autostart and which terminal emulator to start"

i run the latest version of suse and no i dont use xterm. i have that option. i use the one that sais terminal. it opens up to user joe@comcast which is the user i use to run my servers. so no root is required. hope that helps. thanks.

thanks both of you i am at work but as soon as i get a min ill go to the comp and do as you suggested.

one more thing. what text doc should i use and how should i save it? like do i put . sh at the end and save it in the desired dir?
thanks so much guys

ok im on my lunch and was playing around.
i saved both files as a ,sh and saved them inmy home dir. for user joe. the only user besides root.
i chmod -x them which i think need to be done. correct me if im wrong.
and it told me access was denied when i tryed to run them. am i going to need root user access? and where should i save the files that im creating?
and im looking around linux doesnt just have a start up area to place thing to start up. any help on that would be appreciated. thanks.

ok been playing some more.
what i did is opened terminal emulater
typed this code in and hit enter (xterm -e 'cd hlds/orangebox;./srcds_run -game tf +ip 10.1.10.70 -port 27015 +maxplayers 24 +map ctf_2fort sv_pure 1 -autoupdate)
and it opened a xterm and started my server. yay. thanks on the code guys.
so this is the code i need to have put into a script that will run on start up.
im lost on this.

code:
xterm -e 'cd hlds/orangebox;./srcds_run -game tf +ip 10.1.10.70 -port 27015 +maxplayers 24 +map ctf_2fort sv_pure 1 -autoupdate'
so what i need to know now is how to write that in bash, or shell or what ever i need. then get that placed into the right dir and add it to the start up list of things.
thanks again everyone.

Last edited by jmdennis1987; 11-17-2010 at 02:56 PM. Reason: anwser a question i missed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When i Write commands in terminal it does nothing Ciaran Linux - Newbie 1 09-29-2010 05:06 PM
How to write a script with auto login ssh and then run commands? keber314 Linux - Server 13 02-16-2009 03:37 PM
making a script that opens terminal and enter commands into that terminal Cinematography Linux - General 8 12-16-2008 10:34 AM
Write a script to run several commands ??? wahaha Linux - Software 1 04-13-2007 08:20 PM
Help to create a script to open a terminal and run commands in it. Brian1 Linux - Software 6 02-07-2005 06:46 PM


All times are GMT -5. The time now is 11:04 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration