-1

I'm running a gameserver and want to start it on a screen on system start, but screen is user specific, so I have to run the command as 'user'. So I wrote this short service script which starts itself as 'user':

#!/bin/sh
#/etc/init.d/gameserver                                                                                                                     

case "$1" in
  -user)
    sleep 1
    case "$2" in
      start)
        echo "Starting server..."
        screen -dmS scr1 /home/user/game/run.sh
        echo "Server started"
        ;;
      stop) #definitely unfinished 
        echo "Stopping Server..."
        #screen -S scr1 say Server is going down for a halt now!
        #sleep 1
        #screen -S scr1 stop
        echo "Server stopped"
        ;;
      *)
        echo "Usage: /etc/init.d/gameserver {start|stop}"
        exit 1
        ;;
    esac
    ;;
  *)
    su - user  -c "bash -c  '/etc/init.d/gameserver -user $1'"
    ;;
esac

exit 0

and after sudo update-rc.d gameserver defaults the command sudo service gameserver start runs fine, but sadly the server isnt started on system startup.. I'm running Ubuntu 12.04 x64

edit: I've tried replacing su -c with setuid but still no change.

yspreen
  • 161
  • 2
  • 11

1 Answers1

0

You need to enable it to be started at boot time

update-rc.d gameserver enable

or if you are doing it manually; you need symlink in most probably (/etc/rc2.d) to /etc/init.d/gameserver

ln -s /etc/init.d/gameserver /etc/rc2.d/S99gameserver

and to stop it at shutdown and reboot these two

ln -s /etc/init.d/gameserver /etc/rc0.d/K99gameserver

ln -s /etc/init.d/gameserver /etc/rc6.d/K99gameserver

edit

explicitly set PATH in your init script; it is likely failing because it cant find where 'screen' 'bash' and 'su' you are trying to run there are

to set path do;

export PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin

set this just after #!/bin/bash in your script

Hrvoje Špoljar
  • 5,162
  • 25
  • 42
  • I forgot to mention the argument 'defaults' on the update-rc.d command, edited the post now. There are all these links already there, except its S/K20.. not S/K99.. – yspreen Nov 09 '14 at 14:58
  • is init script set as executable ? chmod 0755 /etc/init.d/gameserver ? – Hrvoje Špoljar Nov 09 '14 at 15:04
  • I ran a chmod +x on it. Also tried chmod 0755 as you suggested, but it didnt change anything – yspreen Nov 09 '14 at 15:08
  • can you add something like touch /tmp/foo in your init script and check when computer boots if /tmp/foo exists; this will tell you if your init script ran at all... trying to narrow down – Hrvoje Špoljar Nov 09 '14 at 15:25
  • also; try set path; I think path is not set and screen can't be found when you run your script at boot time... whereas when you run it from shell it inherits PATH you currently have set – Hrvoje Špoljar Nov 09 '14 at 15:29
  • I have replaced the 'starting' and the 'started' echo's with touch 1.test and touch 2.test and after reboot both files were created. How would you set the path? Also, I did include the delay of one second to let things like screen load, wouldnt that be a possible fix for the problem you mentioned? – yspreen Nov 09 '14 at 15:32
  • I've updated answer with details – Hrvoje Špoljar Nov 09 '14 at 15:35
  • I've added the PATH export line and nothing has changed – yspreen Nov 09 '14 at 15:44
  • can you add some touch /tmp/foo to /home/user/game/run.sh to check if that is ran? – Hrvoje Špoljar Nov 09 '14 at 15:55
  • I did, and it doesnt run. That's also what I would expect, since the game-server-process doesnt show up on top. edit: To check I ran the service manually and the file was created. – yspreen Nov 09 '14 at 16:04
  • I suppose it ends up in case 'echo "Usage: /etc/init.d/gameserver {start|stop}"' can you try put some touch statement there to confirm? – Hrvoje Špoljar Nov 09 '14 at 16:07
  • The file isnt created, so I guess it doesnt end up there.. – yspreen Nov 09 '14 at 16:12