## Running as an OS service

JReport Server can be configured as an OS service. This section shows you how to run JReport Server as a service of Windows, Unix and Linux.

### Running as a Windows service

The tool JRservice.exe saved in `<install_root>\bin` is provided to assist you in installing JReport Server as a Windows service.

You can get the following information if you run JRservice.exe without any options:

    Usage:

    JRService -install [-interactive] to install the service
    JRService -remove                 to remove the service

        -interactive to enable JReport Server service
                     to interact with the desktop

    StartServiceCtrlDispatcher being called.
    This may take several seconds. Please wait.

- JRService -install
  Running JRservice.exe with the `-install` option will install JReport Server as a Windows service. If you open the Services item in the Control Panel, you will find a service named JReport Server in the list.
- JRService -install -interactive
  If you use the `-interactive` option together with `-install`, the service installed will be run in interactive mode. That is, when you start the service, a Command Prompt window will pop up. However, if you don't specify this option when you install the service, the Command Prompt window will not be displayed when you start the service. After you have installed JReport Server as a service, you will then be prompted to restart your computer for the service installation to take effect.
- JRService -remove
  Running JRservice.exe with the `-remove` option removes the Windows service of JReport Server from Windows. However, before you run this, you should stop the service.

#### Configuring the service

The parameters for the Windows service of JReport Server are read from the file NTService.ini in `<install_root>\bin`. In this file, you can find three parameters specified, as shown in the following example:

    JavaVM="C:\jdk1.6.0_17\jre\bin\java.exe"
    StartArg= "-Dinstall.root=C:\JReport\Server"
    -classpath "C:\JReport\Server\lib\JREntServer.jar;
    C:\JReport\Server\lib\JREngine.jar;C:\JReport\Server\lib\servlet.jar;C:\TEMP"...
    -Djava.compiler=NONE -Dreporthome=C:\JReport\Server jet.server.JREntServer
    ShutdownArg= "-Dinstall.root=C:\JReport\Server"
    -classpath "C:\JReport\Server\lib\JREntServer.jar;
    C:\JReport\Server\lib\JREngine.jar;C:\JReport\Server\lib\servlet.jar;C:\TEMP" ...
    -Djava.compiler=NONE
    -Dreporthome=C:\JReport\Server jet.server.CommandSender localshutdown

- JavaVM
  The path of the Java VM.
- StartArg
  The Java command line for launching JReport Server as an independent web application server. This will be called when the service is started.
- ShutdownArg
  The Java command line for shutting down JReport Server. This is called when the service is stopped.

#### Starting the service

There are two ways to start the JReport Server service.

- After the server has been installed as a service, it is by default configured to start automatically each time. In other words, without otherwise modifying the Control Panel, the service will start automatically each time Windows is started.
- You can directly start the service through the Services item in the Control Panel. Open the Services list, find JReport Server on the list, select it and click the Start button.

You can change the options in the file NTService.ini in `<install_root>\bin` before you start the service (for more information on the options available, see Starting JReport Server using Java). In the example above, there are no options specified in StartArg. If you would like to set all error log levels to WARN and trace log levels to INFO, you will need to append -logall at the end, as in the following example:

    ...
    StartArg= "-Dinstall.root=C:\JReport\Server"
    -classpath "C:\JReport\Server\lib\JREntServer.jar;
    C:\JReport\Server\lib\JREngine.jar;C:\JReport\Server\lib\servlet.jar;
    C:\JReport\Server\lib\log4j-1.2.8.jar;C:\TEMP" -Djava.compiler=NONE
    -Dreporthome=C:\JReport\Server jet.server.JREntServer -logall
    ...

#### Stopping the service

There are three ways for you to stop the JReport Server service:

- Open the Control Panel, go to Administrative Tools, double-click the Services item, select JReport Server, and then click the Stop button if it is not disabled.
- Run the batch file CmdSender.bat in `<install_root>\bin` with the localshutdown argument, for example: `<install_root>\bin\CmdSender.bat localshutdown`.
- Use the Shut Down the Server button on the JReport Administration page.

Notes:

- All ODBC data sources used by the JReport Server Service belong to the System DSN. System data sources can be used by all users on a computer, and are visible to all users on the computer and system-wide services, such as Microsoft Windows services. User data sources can only be used by the current user and are visible only to that user. To establish JReport Server as a service, you should choose System Data Source. That is, define the data source in the System DSN. To do this, open Data Source (ODBC) in Control Panel, and add the data source used by JReport Server to the System DSN panel. Also, remove any old ones from the User DSN.
- When using NT service to start JReport Server, the mapped disk in path cannot be accessed due to JVM limitation. You should use UNC path (for example, `\\127.0.0.1\public_write`) instead of the mapped disk it is mapped to (for example, `Z`).
- When using the Local System account to run JReport Server as a Windows service, under some circumstances writing to a UNC path (for example, `\\127.0.0.1\public_write`) is not allowed. To solve this problem, you need to start the service by using a normal Windows user.

### Running as a service on Unix

Assume that JReport Server has been installed to `/user/report/jns`,

1. Write a script `/etc/init.d/jrserver` as follows, and make it executable.

       #!/bin/sh
       mode=$1
       if [ ! -d /user/report/jns ]
       then # JReport not installed
       exit 1
       fi
       case "$mode" in
       'start')
       if [ -d /user/report/jns ]
       then
       echo "Starting JReport Server"
       /user/report/jns/bin/NJRServer.sh &
       fi
       ;;
       'stop')
       if [ -d /user/report/jns ]
       then
       echo "Stopping JReport Server"
       /user/report/jns/bin/CmdSender.sh localshutdown &
       fi
       ;;
       *)
       echo " Usage: "
       echo " $0 start (start JReport Server)"
       echo " $0 stop (stop JReport Server)"
       exit 1
       ;;
       esac
       exit 0

2. Create a soft link to `/etc/rc2.d/S99jrserver`.

   ln `-s /etc/init.d/jrserver /etc/rc2.d/S99jrserver`

3. Create a soft link to `/etc/rc0.d/K99jrserver`.

   ln `-s /etc/init.d/jrserver /etc/rc0.d/K99jrserver`

### Running as a service on Linux

Running JReport Server as an OS service on Linux is more or less the same as with running on Unix. Here it is assumed that your default start up rc is rc5.

#### Setting up XVFB

1. Install XVFB.
2. Write a script `/etc/init.d/xvfb` as follows, and make it executable.

       #!/bin/sh
       mode=$1
       case "$mode" in
       'start')
       echo "start xvfb "
       if [ -f /usr/X11R6/bin/Xvfb ]
       then
       /usr/X11R6/bin/Xvfb :1 -screen 0 1152x900x8 &
       fi
       ;;
       *)
       echo " Usage: "
       echo " $0 start (start XVFB)"
       echo " $0 stop (stop XVFB not support)"
       exit 1
       esac
       exit 0

3. Create a soft link to /etc/rc5.d/S97xvfb.

   `ln -s /etc/init.d/xvfb /etc/rc5.d/S97xvfb`

#### Using rc to run JReport Server as a service

Assume that JReport Server has been installed to `/JReport/Server`.

1. Write a script `/JReport/Server/bin/JRServer` as shown below, and make it executable. Here it is assumed that JReport Server is running on a machine with IP address 127.0.0.1.

       #!/bin/sh
       DISPLAY=127.0.0.1:1.0
       export DISPLAY
       /JReport/Server/bin/JRServer -silent "$@"

2. Write a script `/etc/init.d/jrserver` as follows, and make it executable.

       #!/bin/sh
       mode=$1
       if [ ! -d /JReport/Server ]
       then # JReport not installed
       exit 1
       fi
       case "$mode" in
       'start')
       if [ -d /JReport/Server ]
       then
       echo "Starting JReport Server"
       cd /JReport/Server/bin/;
       JRServer -silent &
       fi
       ;;
       'stop')
       if [ -d /JReport/Server ]
       then
       echo "Stopping JReport Server"
       /JReport/Server/bin/CmdSender localshutdown &
       fi
       ;;
       *)
       echo " Usage: "
       echo " $0 start (start JReport Server)"
       echo " $0 stop (stop JReport Server)"
       exit 1
       ;;
       esac
       exit 0

3. Create a soft link to /etc/rc5.d/S98jrserver.

   `ln -s /etc/init.d/jrserver /etc/rc5.d/S98jrserver`

4. Create a soft link to /etc/rc5.d/K98jrserver.

   `ln -s /etc/init.d/jrserver /etc/rc5.d/K98jrserver`

If all has been carried out successfully, the installation of the service will now have finished. JReport Server is now ready to run as a daemon process.