VManoj
VManoj

Reputation: 75

Forked Service getting restarted as soon as it starts?

I am having the below systemd script as below

[Unit]
Description= TaskParticipant Service

[Service]
Type=forking
RemainAfterExit=no
ExecStart=/bin/bash bin/start-participant.sh
ExecStop=/bin/bash bin/stop-participant.sh
Restart=on-failure
WorkingDirectory=/opt/taskparticipant
User=javauser
Group=javauser
PrivateTmp=true
TimeoutSec=90
SuccessExitStatus=1

[Install]
WantedBy=multi-user.target

After doing the systemctl start tp

I am getting the below error

[centos@mmanthena bin]$ sudo systemctl status tp.service 
● tp.service - TaskParticipant
   Loaded: loaded (/etc/systemd/system/tp.service; enabled; vendor preset: disabled)
   Active: inactive (dead) since Tue 2019-08-06 07:20:19 GMT; 2s ago
  Process: 6635 ExecStop=/bin/bash bin/stop-participant.sh (code=exited, status=1/FAILURE)
  Process: 6504 ExecStart=/bin/bash bin/start-participant.sh (code=exited, status=0/SUCCESS)
 Main PID: 6577 (code=exited, status=1/FAILURE)

Aug 06 07:20:17 manthena systemd[1]: Starting TaskParticipant...
Aug 06 07:20:17 manthena bash[6504]: **************************************
Aug 06 07:20:17 manthena bash[6504]: Starting TaskParticipant
Aug 06 07:20:17 manthena bash[6504]: **************************************
Aug 06 07:20:18 manthena systemd[1]: Started - TaskParticipant.
Aug 06 07:20:19 manthena bash[6635]: Stopping Participant
#!/bin/bash

# JVM ARGUMENTS
jvm_min_heap_size="128m"
jvm_min_heap="-Xms${jvm_min_heap_size}"
jvm_max_heap_size="256m"
jvm_max_heap="-Xmx${jvm_max_heap_size}"


# GC Configuration
jvm_gc_options="-XX:+UseG1GC -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/acds/var"
jvm_gc_log_option="-XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -Xloggc:${logs_dir}/task-participant-gc.log"

jvm_arg_line="${jvm_min_heap} ${jvm_max_heap} ${jvm_gc_options} ${jvm_gc_log_option} -Dlog4j.configuration=task_participant_log4j.conf"

config_file_option="-cf ${conf_dir}/acds_taskmanager.conf"
host_name_option="-H ${HOSTNAME}"

java_arg_line="${config_file_option} ${host_name_option}"

# Run java
main_class=io.manoj.acds.taskparticipant.TaskParticipant
cmdline="java -cp ${cp} ${jvm_arg_line} ${main_class} ${java_arg_line} $*"

cd $root_dir

nohup $cmdline &
tp_pid=$(jps -l | grep io.manoj.acds.taskparticipant.TaskParticipant | awk '{print $1}')
echo ${tp_pid} > ${tp_pid_file}

My Expectation was that the process should start and run in the background as the parent is the bin/start-participant.sh and it starts a java process but I see that the process is getting restarted on starting with the systemctl

On starting manually it's working as expected (i.e Running the Script directly works fine)

Upvotes: 0

Views: 563

Answers (1)

lw0v0wl
lw0v0wl

Reputation: 674

The problem is with the Work directory I believe.

If you defined variables in bash_profiles or bashrc that not get used by the systemd unit. You could use EnvironmentFile= section for that.

Most likely the $root_dir variable is empty so cd $root_dir change the working director from what systemd unit defined ( WorkingDirectory=/opt/taskparticipant ) to javauser home directory. I think this is not the intended behavior.

This can be verified if you change line cd $root_dir to cd ${root_dir:?} this way script exit with error if $root_dir variable is empty.

Upvotes: 1

Related Questions