PDA

View Full Version : Run Program as Service/Daemon?



Pawel Kowalski
05-11-2008, 11:57 AM
I have the following program I need to have start with the server and incase it crashes have it restart automatically:



./hlds_run -game dod -autoupdate +maxplayers 32 +map dod_avalanche +ip 69.73.160.23 > /dev/null 2>&1 &


In Windows I could run it as a service. Is something like this possible in linux?

Ron
05-11-2008, 01:08 PM
Yes.

RCs and of course cron jobs. Perhaps you should set up a monitoring service (I think you get some on a VPS?) to watch for and start/restart it automagically?

This might be dated info and/or UNIX version specific, so it might not be perfect for you.

Pawel Kowalski
05-12-2008, 08:35 AM
Hi Ron,

I'm not sure I understand what an RC is, tried to google it but couldn't find much info. Cron jobs are set up to run at a certain time aren't they? Or is there a way to make them run on start up?

The only control panel I got is virtuozzo and I don't see any option there for monitoring a process, just the ability to kill one.

jason
05-12-2008, 09:24 AM
RC stands for "run commands." In *nix environments there is usually a directory where you can store references to (symlinks) commands that should start up automatically. This is usually in /etc/rc.d. Inside rc.d there is usually several more directories, such as rc#.d (where # is an actual number) that correspond to different runlevels (or system states). (here is a good explanation of runlevels (http://www.linux.com/articles/114107))

Inside the rc#.d directory (most system services, including Apache and most other Internet sevices will start at runlevel 3 (or rc3.d) and greater) you will find several symlinks to scripts (usually in rc.d/init.d) with names similar to S85httpd. These are executed in alphabetical order as they system comes up, so the numbers are used to differentiate that one script get run before another--for example, on my VPS, S64mysqld will start before S85httpd. There are also files that start with "K" that indicate the kill order of the processes when the system is being shutdown or rebooted. Pay close attention to these numbers if your process requires other processes to be running or you could have problems.

Runlevel 6 (rc6.d) is generally the reboot runlevel (entered as the system begins the reboot process) and is used to gracefully stop the services you've started at other runlevels.

The init.d script is a simple bash script, owned by root, that will receive one of three parameters: start, stop, or restart. Look at some of the other scripts in that directory to get an idea of how to write your own.

Hope this helps.

--Jason

Ron
05-12-2008, 12:02 PM
^^^ Wow. What Jason (da man) said.

About cron jobs:
You could set up a cron job, a shell script, that looks for a certain process and if it doesn't find it, starts it. You could set that up to run every X minutes. Pick a number for X.

Also:
The monitoring service -- doesn't that come with a VPS? If not specifically, couldn't you use some of your free administration time to ask them to write that quick script? (I thought you used to get Level2 management which comes with 5 monitored processes, but the new pages don't talk about it that way. Open a ticket to support and/or sales and ask?)

Pawel Kowalski
05-13-2008, 06:34 PM
Thanks Jason, I finally got a chance to read through this. I think I can get it working, I'll see if I run in to any issues.

Ron
05-13-2008, 06:51 PM
I'm officially chopped liver.

Pawel Kowalski
05-14-2008, 08:34 AM
Ron, sorry I didn't respond to you yesterday. I didn't get a chance to look in to shell scripts until this morning and didn't really want to ask you any stupid questions before reading up on it myself (I do that sometimes). :)

For obvious reasons having cron restart my process if it crashes would be more worth while than just starting the process at boot time. Based on what you said I found the following tutorial which I think will get me going in the right direction to write the shell script:

http://www.freeos.com/guides/lsst/

But what command would I actually use to see if a process is currently running? Thanks.

Ron
05-14-2008, 10:39 AM
LOL I really wasn't offended, I just thought that would be funny.

I used complex products (AutoSys, Tivoli) to monitor my system processes in a production environment. I am really a long time out of writing shell scripts, although I think this would be very easy to do, so let's try:

Here is a script that might do what you need, where the jobname is hlds_run

#!/bin/bash

ps -ef|grep hlds_run|grep -v grep >/dev/null
if [[ $? -ne 0 ]]
then
# Job not found, so start it
./hlds_run -game dod -autoupdate +maxplayers 32 +map dod_avalanche +ip 69.73.160.23 > /dev/null 2>&1 &
fi
Call this file anything other than hlds_run and run it from cron every X minutes.

Nosing around the 'net I found some info on inittab which might do exactly what you're looking for as well. I don't know if inittab is available for your VPS. If it is, it looks like a line like

Myhlds_run:2:respawn:/path/to/script/hlds_run -game dod -autoupdate +maxplayers 32 +map dod_avalanche +ip 69.73.160.23 > /dev/null 2>&1
in /etc/inittab might do the trick
If inittab is not available, perhaps procmon is available on your VPS.

Ron
05-14-2008, 10:57 AM
PS -- These methods don't do anything if there's a problem causing your process to crash over and over again. They just restart them when they go down.

Assuming that you can't modify the code for the game server to say when and/or why it is going up or down, in the cron job script you might wanna stick a line in that writes startup info to a log.
In the inittab version you might want to put that long command line into a script file, and also put a log write into that. At least you'll know how often a script is being restarted.

Pawel Kowalski
05-14-2008, 11:25 AM
Sweet, thanks. I'll give this a shot once I get the server working properly.