Truly transparent background processes

Write your "How To" regarding anything you are comfortable with and you feel it will help the forum members.

NOTE :: All threads started here will appear only after the approval from Administrator
Post Reply
leegao
Posts: 15
Joined: Thu Aug 10, 2006 3:46 am
Contact:

Truly transparent background processes

Post by leegao »

Presumptions:
You have a VPS or a Dedicated Server with SSH access and (at least) BASH on it. Also the OS is some flavor of either Linux or some other UNIX based OS (Ie: BSD)

Also it would be good if you have Python installed on the system as I'm going to talk about Pseudo-Persistent process management later on which requires python as the backend language.



Introduction:

Many of you may have noticed in your server procs that there are background services or processes running. A few of the notable programs may include Apache Server, TinyDNS Server, some sort of Mail Server, Print Server, and a variety of other servers.

That's right, the majority of the time, the need to create a background process is usually to accommodate some types of persistent server. In my case it was a custom made game server.

Anyways, you could go with the traditionally used Screen command but there's two main problems with that:

1. You'll have to either manually kill the server when you want to kill it or create a more sophisticated program to kill it after finding the PID of the screen file as well. Or you could just let the screen utility go out of hands with the large amounts of unused screens

2. The screen utility still provides an interactive stdin for the program. There are some programs out there which determines how it should run itself by determining if it has a non-blocking stdin stream. (Mine for example)



Solution:

Rather then to programmatically implement this by double forking the process to the system, I decided to go the easy way.

(python ./pidServer.py > /home/admin/*******.info/server.log 2>&1 &) &



Explanation:
This technique uses Double Ampersand to create the background service. The 2 stream routes means that the stdout is routed to the said path and that stderr (2) is routed to where the stdout is (be sure to use &1 in this case)

This is actually a really neat capability of the BASH but is often not used due to Screen. However, when the problems above are significant or when you want to create dynamic background services, screen is powerless and it’s always better to know how to do things the long way.


SHAdmin
Posts: 2089
Joined: Sat Dec 18, 2004 11:28 am
Contact:

Post by SHAdmin »

Hi,
Thank you for sharing that very useful 'how to' with the community.

Your account has been credited with 30 points for your contribution.
Post Reply