LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Write a script to open a terminal and run a few commands. (https://www.linuxquestions.org/questions/linux-general-1/write-a-script-to-open-a-terminal-and-run-a-few-commands-844426/)

jmdennis1987 11-15-2010 02:40 PM

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.

paulsm4 11-15-2010 02:53 PM

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

jmdennis1987 11-15-2010 03:18 PM

Quote:

Originally Posted by paulsm4 (Post 4159808)
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.

trey85stang 11-16-2010 08:21 AM

sounds like you need to make a service/init script...

catkin 11-16-2010 09:10 AM

Quote:

Originally Posted by jmdennis1987 (Post 4159790)
i will be putting this script in my startup items to run on start up.

Boot scripts or desktop autostart?

jmdennis1987 11-16-2010 04:38 PM

Quote:

Originally Posted by catkin (Post 4160739)
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.

MrUmunhum 11-16-2010 05:07 PM

Quote:

Originally Posted by jmdennis1987 (Post 4161142)

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?

jmdennis1987 11-16-2010 07:12 PM

Quote:

Originally Posted by MrUmunhum (Post 4161168)
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.

catkin 11-16-2010 09:00 PM

Quote:

Originally Posted by jmdennis1987 (Post 4161142)
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?

jmdennis1987 11-16-2010 10:55 PM

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.

catkin 11-16-2010 11:59 PM

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.

jmdennis1987 11-17-2010 09:38 AM

Quote:

Originally Posted by catkin (Post 4161464)
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.

linus72 11-17-2010 09:47 AM

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...

catkin 11-17-2010 10:23 AM

Quote:

Originally Posted by linus72 (Post 4161959)
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.

jmdennis1987 11-17-2010 12:06 PM

"(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. :)


All times are GMT -5. The time now is 07:42 AM.