I want to create a crontab entry so that it starts screen, starts a gameserver and detaches. This is for in case the server is rebooted and I want it to automatically start this for me.

0 0 0 0 0 (command)

should run upon startup.

It runs a shell file located at ~/cube/server.sh

link|improve this question
Are you a user on this machine, or do you have access to init scripts? – Corey S. Feb 9 '11 at 3:36
2  
Please see Process Management. – Dennis Williamson Feb 9 '11 at 4:46
@Dennis: Yep, I like that. But we can't assume the game server he's running doesn't have some sort of console that he'd need to access (i.e. always runs in the foreground). Else why would he bother with screen? Unless of course, he's not aware of nohup and backgrounding. – Corey S. Feb 9 '11 at 5:42
I am a user on this machine. – Victor Feb 13 '11 at 3:42
feedback

3 Answers

up vote 3 down vote accepted

Something like this should work. This example spawns a screen and runs "top":

screen -d -m top

In your crontab, as indicated, you'd want to do something like this:

@reboot /usr/bin/screen -dmS gameserver-screen /opt/mycoolgame/bin/gameserver

Of course, if the game server requires a "normal" environment set, you can get closer by:

@reboot (. ~/.profile; /usr/bin/screen -dmS gameserver-screen /opt/mycoolgame/bin/gameserver)
link|improve this answer
what does the . ~/.profile part do? – Victor Feb 13 '11 at 1:54
It forcibly sets the environment for the cron entry. Without it, you just get a couple very specific entries. (See man crontab for details) – Corey S. Feb 13 '11 at 2:43
I am have a .sh file that executes the game server, so would this work? @reboot (. ~/cube; /usr/bin/screen -dmS gameserver-screen ./server.sh) – Victor Feb 13 '11 at 3:39
That's probably the best way to do it. If that works, you probably not need to worry about the profile, but if you need a full $PATH, etc, you could always source in the .profile from the server.sh script. – Corey S. Feb 13 '11 at 14:08
feedback

This should be sufficient...run

$ crontab -e

Then enter:

@reboot screen -dmS Victor
link|improve this answer
feedback

I had a similar situation, but due to other unreasonable restrictions, I could not use crontab. I actually had inittab call screen. ( replaced some names to obscure information):

XXX:5:respawn:/bin/su - useraccount -c "screen -D -m -c /home/xxxxxx/file.screenrc"

In 'file.screenrc' is where I setup a few options:

sessionname obscuresessionname
multiuser on
cd
screen /home/xxxxxxx/programtostart

This way it started on boot, and if the program died or screen closed it would re spawn. It may not have been considered conventional, but I had to work a round a few odd environment requirements. If we needed to take it down though, we would have to comment that line out, and kill session. Then when ready to bring it back up, uncomment, and init q.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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