2

I wrote this service in/etc/systemd/system called nexusiq.service:

[Unit]
Description=Nexus IQ Service
After=network.target

[Service]
Type=simple
ExecStart=/etc/init.d/nexus-iq-server start
ExecStop=/etc/init.d/nexus-iq-server stop
User=root
Restart=on-abort
TimeoutSec=600

[Install]
WantedBy=multi-user.target

The script nexus-iq-server in /etc/init.d that is invoked in ExecStart and ExecStop, depending on whether I give "start" or "stop" as argument does the following:

NEXUS_IQ_SERVER_HOME=/apps/nexus/nexus-iq-server
VERSION=1.127.0-01
JAVA_OPTIONS="-Xmx4096m -XX:+UseG1GC"
RUN_AS_USER=root

do_start()
{
    cd $NEXUS_IQ_SERVER_HOME
    su -m $RUN_AS_USER --command "java $JAVA_OPTIONS -jar nexus-iq-server-$VERSION.jar server config.yml 2> stderr.log &"
    echo "Started nexus-iq-server"
}

do_console()
{
    cd $NEXUS_IQ_SERVER_HOME
    su -m $RUN_AS_USER --command "java $JAVA_OPTIONS -jar nexus-iq-server-$VERSION.jar server config.yml 2> stderr.log"
}

do_stop()
{
    pid=`ps aux | grep nexus-iq-server | grep '.jar server' | grep -vE '(stop|grep)' | awk '{print $2}'`
    kill $pid
    echo "Killed nexus-iq-server - PID $pid"
}

do_usage()
{
    echo "Usage: nexus-iq-server [console|start|stop]"
}

case $1 in
    console)
        do_console
        ;;
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    *)
        do_usage
        ;;
esac

I have enabled the service.

When I run the command: systemctl start nexusiq the command assigned to ExecStart is correctly invoked. But, if i run the command systemctl stop nexusiq, the command assigned to ExecStop is not invoked.

Furthermore, if I run the command systemctl status nexusiq, looks like the service is inactive:

● nexusiq.service - Nexus IQ Service
   Loaded: loaded (/etc/systemd/system/nexusiq.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Wed 2021-11-17 12:56:03 CET; 16s ago
  Process: 14806 ExecStop=/etc/init.d/nexus-iq-server stop (code=exited, status=0/SUCCESS)
  Process: 14800 ExecStart=/etc/init.d/nexus-iq-server start (code=exited, status=0/SUCCESS)
 Main PID: 14800 (code=exited, status=0/SUCCESS)

Nov 17 12:56:03 nexusiq2 systemd[1]: Started Nexus IQ Service.
Nov 17 12:56:03 nexusiq2 su[14801]: (to root) root on none
Nov 17 12:56:03 nexusiq2 nexus-iq-server[14800]: Started nexus-iq-server

Can anyone explain to me what is causing this, and how to fix it?

1

1 Answer 1

2

You need Type=forking instead of Type=simple.

Man page says:

  • If set to simple (the default if ExecStart= is specified but neither Type= nor BusName= are), the service manager will consider the unit started immediately after the main service process has been forked off. It is expected that the process configured with ExecStart= is the main process of the service. In this mode, if the process offers functionality to other processes on the system, its communication channels should be installed before the service is started up (e.g. sockets set up by systemd, via socket activation), as the service manager will immediately proceed starting follow-up units, right after creating the main service process, and before executing the service's binary. Note that this means systemctl start command lines for simple services will report success even if the service's binary cannot be invoked successfully (for example because the selected User= doesn't exist, or the service binary is missing).
  • If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main service process, and the service manager will consider the unit started when the parent process exits. This is the behavior of traditional UNIX services. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can reliably identify the main process of the service. systemd will proceed with starting follow-up units as soon as the parent process exits.

In summary, When /etc/init.d/nexus-iq-server start concludes, systemd things your service is finished. So it considers it inactive (stopped). systemd will see your residual java process as something that needs to be cleaned up and kill it. systemctl stop will not do anything because the unit is already stopped.

Using Type=forking tells systemd that ExecStart= is expected to spawn a process and exit. That spawned process becomes the main PID that is tracked by systemd.

1
  • 1
    Hi, thanks for the reply. Unfortunately setting Type=forking did not solve the problem. The ExecStop still doesn't work. And systemctl status nexusiq keeps returning inactive (dead). Also I wanted to clarify that even in the case of Type=simple the ExecStart works, the java process is not killed. Commented Nov 22, 2021 at 10:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .