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

catkin 11-17-2010 01:21 PM

Quote:

Originally Posted by jmdennis1987 (Post 4162100)
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 :)

If we knew which desktop you were using we might know what programs you had available. There will be a GUI text editor; use that. The file doesn't need a .sh extension or to be executable so just save it in the same place as named after the --rcfile option. You can test the xterm -e bash --rcfile your_file_directory_and_name at the command prompt in a GUI terminal emulator.

As for setting it up to autostart it will be somewhere under the menu -> settings maybe with a name like session.

MrUmunhum 11-17-2010 06:29 PM

Quote:

Originally Posted by jmdennis1987 (Post 4161282)
hey thanks.

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?

Post the output of these commands:
ls -lrt /etc/rc5.d
cat /etc/rc5.d/S99mystarts
dmesg | grep mystarts
The startup script should not be in you home dir.

If you wish to test the script, in a terniaml window:
enter /etc/rc5.d/S99mystarts
enter dmesg | grep mystart

catkin 11-17-2010 08:20 PM

Quote:

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

No and yes. Use ./file.sh. That means "run the file called file.sh in this directory". Yours means "run the file called .file.sh in the / directory".

catkin 11-17-2010 08:25 PM

Quote:

Originally Posted by MrUmunhum (Post 4162439)
Post the output of these commands:
ls -lrt /etc/rc5.d
cat /etc/rc5.d/S99mystarts
dmesg | grep mystarts
The startup script should not be in you home dir.

If you wish to test the script, in a terniaml window:
enter /etc/rc5.d/S99mystarts
enter dmesg | grep mystart

jmdennis1987 wants terminal windows open as administrative consoles for the games servers so a boot script is not the right solution. If it was we do not yet know which distro so it may not be using /etc/rc<n>.d directories.

jmdennis1987 11-17-2010 09:53 PM

If we knew which desktop you were using we might know what programs you had available. There will be a GUI text editor; use that. The file doesn't need a .sh extension or to be executable so just save it in the same place as named after the --rcfile option. You can test the xterm -e bash --rcfile your_file_directory_and_name at the command prompt in a GUI terminal emulator.

I am useing opensuse 11.3 and am useing KDE GUI it auto logs into user joe and starts.



No and yes. Use ./file.sh. That means "run the file called file.sh in this directory". Yours means "run the file called .file.sh in the / directory".



Ok i see the difference thanks for explaining. ill try ./file.sh tomorrow on lunch.


jmdennis1987 wants terminal windows open as administrative consoles for the games servers so a boot script is not the right solution. If it was we do not yet know which distro so it may not be using /etc/rc<n>.d directories.

well the pc starts without admin i thaught. considering admin is root i am probly wrong so please tell me. but it starts up in KDE as user joe and if i whant to go into yast or udate the system anything that takes root asks for a password. my servers run without root.

thanks every one for your help. i hope this helps you help me :)

one question real quick i write this code like

#!/bin/sh

#
# server script
#

echo "Starting server 1"

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'

echo "Starting server 2"

xterm -e 'cd hlds1/orangebox;./srcds_run -game tf +ip 10.1.10.70 -port 27025 +maxplayers 4 +map ctf_bball2 sv_pure 1 -autoupdate'

echo "Starting server 3"

xterm -e 'cd l4d2/l4d2;./srcds_run -game tf +ip 10.1.10.70 -port 27023 +maxplayers 4 -autoupdate'

#
# End
#
is that correct will that work.

Thanks

catkin 11-18-2010 02:07 AM

Quote:

Originally Posted by jmdennis1987 (Post 4162602)
I am useing opensuse 11.3 and am useing KDE GUI it auto logs into user joe and starts.

IDK KDE but it looks as if you can make a script run when KDE starts by putting it (or a link to it) in ~/.kde/Autostart/ or ~/.kde4/Autostart/. Some info here and here.
Quote:

Originally Posted by jmdennis1987 (Post 4162602)
one question real quick i write this code like

Code:

#!/bin/sh

#
# server script
#

echo "Starting server 1"

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'

echo "Starting server 2"

xterm -e 'cd hlds1/orangebox;./srcds_run -game tf +ip 10.1.10.70 -port 27025 +maxplayers 4 +map ctf_bball2 sv_pure 1 -autoupdate'

echo "Starting server 3"

xterm -e 'cd l4d2/l4d2;./srcds_run -game tf +ip 10.1.10.70 -port 27023 +maxplayers 4 -autoupdate'

#
# End
#

is that correct will that work.

Almost. Like that it will wait until server 1 finishes and you close the terminal before starting server 2. Add an & at the end of each xterm command and it will do what you want:
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' &
KDE's terminal emulator is Kconsole so you might like to try using kconsole instead of xterm.

jmdennis1987 11-18-2010 10:35 AM

Almost. Like that it will wait until server 1 finishes and you close the terminal before starting server 2. Add an & at the end of each xterm command and it will do what you want:

so it wont move on to the next command tell the xterm for the first command closses. ok i see since my server dont close the terminal. is eir a wait command can put in between to wait like maybe 30 sec before starting the next server?

IDK KDE but it looks as if you can make a script run when KDE starts by putting it (or a link to it) in ~/.kde/Autostart/ or ~/.kde4/Autostart/. Some info here and here.


thanks ill read on this a lil later.

thanks, u guys have been great help. :)

catkin 11-18-2010 11:08 AM

Quote:

Originally Posted by jmdennis1987 (Post 4163247)
so it wont move on to the next command tell the xterm for the first command closses. ok i see since my server dont close the terminal. is eir a wait command can put in between to wait like maybe 30 sec before starting the next server?

It will move on if you put the & at the end of the command.

sleep 30 will do what you want.

Presumably it would be nice to have all four terminals open as administrative consoles for each server ... ? EDIT: maybe even program them to change background colour if a fault is detected so you could tell at a glance ... ?

jmdennis1987 11-18-2010 11:19 AM

Presumably it would be nice to have all four terminals open as administrative consoles for each server ...

im confused. i dont think i need administrative permissions. i run the servers in user mode. i believe. unless im miss understanding your question. thanks.

sleep 30 will do what you want.

so the 30 stands for seconds so if i wanted to add more time just add it in seconds like sleep 120 for 2 mins?

IDK KDE but it looks as if you can make a script run when KDE starts by putting it (or a link to it) in ~/.kde/Autostart/ or ~/.kde4/Autostart/. Some info here and here.

hey i cant find the folder auto start or anything like n these. but is their a way to see waither i am using gnome or kde. it gave me the choice im sure im using kde but could be wrong. how can i check? thanks

ok here is my script
#!/bin/sh

#
# server script
#

echo "Starting server 1"

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' &

sleep 60

echo "Starting server 2"

xterm -e 'cd hlds1/orangebox;./srcds_run -game tf +ip 10.1.10.70 -port 27025 +maxplayers 4 +map ctf_bball2 sv_pure 1 -autoupdate' &

sleep 60

echo "Starting server 3"

xterm -e 'cd l4d2/left4dead2;./srcds_run -game left4dead2 +ip 10.1.10.70 -port 27023 +maxplayers 4 -autoupdate'

#
# End
#

i saved it
but when i try to run it to check that it works it tells me permission denied.
what is the proper way to execute a shell?

catkin 11-18-2010 11:45 AM

Quote:

Originally Posted by jmdennis1987 (Post 4163297)
im confused. i dont think i need administrative permissions. i run the servers in user mode. i believe. unless im miss understanding your question. thanks.

so the 30 stands for seconds so if i wanted to add more time just add it in seconds like sleep 120 for 2 mins?

hey i cant find the folder auto start or anything like n these. but is their a way to see waither i am using gnome or kde. it gave me the choice im sure im using kde but could be wrong. how can i check? thanks

"Administrative" with respect to the games servers, not the operating system. IDK games servers either so there's probably a more colourful term like "Game Lord"! :)

Yes, seconds. You can also do minutes with sleep 2m. The man sleep command gives details.

Not sure about how to check. On an old Gnome system, in the System menu, there was an About GNOME option but that was an ubuntu system and they do a lot of customisation so it may not be standard. You could try a ps -ef command and see if any of the processes have kde or gnome in their names.

jmdennis1987 11-18-2010 12:30 PM

Not sure about how to check. On an old Gnome system, in the System menu, there was an About GNOME option but that was an ubuntu system and they do a lot of customisation so it may not be standard. You could try a ps -ef command and see if any of the processes have kde or gnome in their names.

i am useing KDE

also what is the proper way ro run a shell?
do i chmod -x it and do i need to give it 755 access?
cuz i can copy and past my script in a terminal and it works like i want.
but to run it with out copy and paste it into a terminal i dont know how todo.
cus when i try to run it, it says access denied.
thanks.

was playing around found start up folder runs at start up but i think my code is messed up.
it said starting server 1 and nothing starting server 2 nothing starting server 3 nothing.
so what i think is wrong is the fil dir of location.
so i guess what im asking know is what would be the code to tell it to look in a different folder then where the script is running from. the script is in auto start and i think i got the code setup to look for hlds folder in autostart folder. so how do i tell it to look in another folder then the one its in to run. hope that makes since.
thanks again.

OMG THANKS EVERYONE FOR ALL YOUR HELP
i got the code writin had to add home/joe/ to the dir got that figured out and it starts at startup like i want.
so this issue is salved and thanks for all your help.

catkin 11-18-2010 01:44 PM

Glad you got it working and hope you enjoyed the adventure of figuring it out. The Thread Tools menu can be used to mark the thread SOLVED.

jmdennis1987 11-18-2010 02:24 PM

Glad you got it working and hope you enjoyed the adventure of figuring it out. The Thread Tools menu can be used to mark the thread SOLVED.

Hey thanks again yes it was really fun opened a few more doors for me to play around with shell and bash. now that i see how easy it is and how it works. ill change this thread to solved
thanks for all the help


All times are GMT -5. The time now is 12:06 AM.