Skip to content
Search

Service

lastmod 2025-12-23created 2016-06-245 edits

How to register

Add a script file to the /etc/init.d directory, such as /etc/init.d/eclipse-che. Then add execute permission with chmod 755 eclipse-che.

if [[ $(which java) == "" ]]; then
JAVA_HOME=/usr/local/jdk/
PATH=$JAVA_HOME/bin:$PATH
fi

CHE_HOME=/usr/local/eclipse-che
CHE_BIN=$CHE_HOME/bin/che.sh

# See how we were called
case "$1" in
    start)
        echo "Start eclipse-che."
        $CHE_BIN start
        ;;

    stop)
        echo "Stop eclipse-che."
        $CHE_BIN stop
        ;;

    restart)
        echo "Restart eclipse-che."
        $CHE_BIN start
        $CHE_BIN stop
        ;;

    *)
        echo  "Usage: $0 {start|stop|restart}"
        exit 1
esac
exit 0

Register the service:

## Ubuntu:
$ sudo update-rc.d eclipse-che defaults

## CentOS:
$ sudo chkconfig --add eclipse-che

Service commands:

$ sudo service eclipse-che (start|stop|restart)

Check the Service status:

$ sudo service eclipse-che status

Win32 Service install & uninstall

#pragma comment(lib, "advapi32.lib")
LRESULT InstallService ( TSTR Path, TSTR ServiceName, TSTR DisplayName, DWORD StartType )
{
    SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_CREATE_SERVICE);
    if ( schSCManager != 0 )
    {
        SC_HANDLE schService = CreateService(schSCManager, ServiceName.c_str(), DisplayName.c_str(), SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, StartType, SERVICE_ERROR_NORMAL, Path.c_str(), NULL, NULL, NULL, NULL, NULL);
        if ( schService != 0 )  {
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            return TRUE;
        }
        CloseServiceHandle(schSCManager);
    }
    return FALSE;
}

LRESULT RemoveService ( TSTR Name )
{
    SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( schSCManager != 0 )
    {
        SC_HANDLE schService = OpenService(schSCManager, Name.c_str(), SERVICE_ALL_ACCESS);
        if ( schService != 0 )  {
            DeleteService(schService);
            CloseServiceHandle(schService);
            CloseServiceHandle(schSCManager);
            return TRUE;
        }
        CloseServiceHandle(schSCManager);
    }
    return FALSE;
}

LRESULT StartService ( TSTR Name )
{
    SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( schSCManager != 0 )
    {
        SC_HANDLE schService = OpenService(schSCManager, Name.c_str(), SERVICE_ALL_ACCESS);
        if ( schService != 0 )
        {
            if(StartService(schService, 0, (const TCHAR**)NULL))
            {
                CloseServiceHandle(schService);
                CloseServiceHandle(schSCManager);
                return TRUE;
            }
            CloseServiceHandle(schService);
        }
        CloseServiceHandle(schSCManager);
    }
    return FALSE;
}

LRESULT StopService ( TSTR Name )
{
    SC_HANDLE schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
    if ( schSCManager != 0 )
    {
        SC_HANDLE schService = OpenService(schSCManager, Name.c_str(), SERVICE_ALL_ACCESS);
        if ( schService != 0 )
        {
            SERVICE_STATUS status;
            if( ControlService(schService, SERVICE_CONTROL_STOP, &status) )
            {
                CloseServiceHandle(schService);
                CloseServiceHandle(schSCManager);
                return TRUE;
            }
            CloseServiceHandle(schService);
        }
        CloseServiceHandle(schSCManager);
    }
    return FALSE;
}

These are functions that register, unregister, start, and stop an application as a Windows service. Administrator privileges are required.

See also

author Zer0 sitemap