Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to create a service in linux as we do in windows. which i can start or stop with help of a script. the service has to launch a gaming application which has some dependencies associated with it.

Basically i want a script executing which creates a service in linux, this service will basically launch a gaming application(launching is also done through a script) , so its like the service has to launch a script.

Purpose of the service: i will like the service to auto restart the application in case it crashes. Got to know that auto restart of application can be done through services

please help me on this i am new to linux :(

share|improve this question
    
some more information would help –  Bruno Pereira Oct 7 '11 at 5:25
    
updated question.... –  Varun Oct 7 '11 at 5:29
add comment

1 Answer 1

I have a feeling that you only mention "service" because on Windows it's possible to configure a service to auto-restart when it crashes. I even think it wouldn't be easy to make an arbitrary "game application" to work as a service on Windows because services run with limited privileges.

If you need a script which auto-restarts an application if the latter exits with a non-zero error code (i.e. when it crashes), check Anycorn's answer to a similar question on stackoverflow. It's very simple and it does not involve any services.

If you want step-by-step instructions, here what I would do:

create a directory called bin in your home folder:

mkdir ~/bin

create a script "startgame" in that directory:

touch ~/bin/startgame

open it in a text editor:

gedit ~/bin/startgame

enter the following text:

#!/bin/bash
while [ 1 ]; do /path/to/game/executable && break; done

save the file;

set executable bit on it:

chmod +x ~/bin/startgame

execute the script by typing in the terminal

startgame

All of the above can be achieved without terminal - i.e. create a folder using Nautilus, create a file, make it executable; create a desktop shortcut etc.

If you're interested how the script works:

while [ 1 ]; do ... done

is an infinite loop

/path/to/game/executable

starts the application and waits for it to finish (obvously, you need to change it to the actual path of your application)

command1 && command2

executes command1 and then executes command2 only if command1 terminated with a zero return code (which indicates success). I.e. the loop will only stop ('break' statement) if the program terminated without an error, otherwise it'll be restarted.

share|improve this answer
    
ya you got me right its basically for auto restart stuff... but i am unable to understand the solution been provide at this link... –  Varun Oct 7 '11 at 5:52
    
@Varun: I updated the answer –  Sergey Oct 7 '11 at 6:40
    
If you replace while [ 1 ]; with something like while [ ! -e /var/run/stop_the_loop ]; you can break the loop by creating the file /var/run/stop_the_loop. That can be useful sometimes. –  Florian Diesch Oct 7 '11 at 8:03
    
@FlorianDiesch the original "recipe", which usage I tried to explain, mentioned a 2-second sleep before restarting the program - in this case a second Ctrl-C would allow to break the loop, which probably is even easier then creating a file in /var/run/ –  Sergey Oct 7 '11 at 8:38
    
@Sergey - wat if i want to stop the application... i had another script for stopping it... will share the both script in sometime.. feels like a big noob :( –  Varun Oct 7 '11 at 10:00
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.