javalinuxmavendaemon

Environmental variables not being picked up in Linux service


I am creating a Linux (AWS Linux) startup service to run a Jar file in /etc/init.d. It's basically a maven project using Spring boot. I initially generate Jar file by running sudo -E ./mvnw clean package and then start a service which run the Jar file forever as a service. This Jar file will be running as a daemon service. However none of the environmental variables are being picked from the service and I am actually getting null for those variables. I am setting my environmental variables in ~/.profile and later I run source ~/.profile. I can see all the environmental variables being picked if I run Java -jar osuserver.jar directly in the project path but If I run the same as the service, it is not able pickup environmental variables.

Sequence: I first set the environmental variables in ~/.profile --> run source ~/.profile --> I create Jar file by running maven package - sudo -E ./mvnw clean package--> later I will start the service (sudo service Osuserver start).

I even tried giving Sudo -E service osuserver start but it didn't help and I still got null values for environmental variables.

Here is the syntax of my OSU start script that I am saving in init.d

#!/bin/sh
# chkconfig: 35 99 10
SERVICE_NAME=osuserver
PATH_TO_JAR=/home/ec2-user/osuserver/target/osu-server-0.1.0.jar
PID_PATH_NAME=/tmp/osuserver-pid
case $1 in
    start)
        
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
            touch /var/lock/subsys/osuserver
        else
            echo "$SERVICE_NAME is already running ..."
            touch /var/lock/subsys/osuserver
        fi
    ;;
    stop)  
 
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
            rm -f /var/lock/subsys/osuserver
        else
            echo "$SERVICE_NAME is not running ..."
            rm -f /var/lock/subsys/osuserver
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac

So basically application is running properly with service but the only issue is with picking up the environmental variables.So do I need to set the environmental variables directly from the service?. I am not sure where I am going wrong.

---->>>>>>Editing my Post<<<<<<<<---

I wrote a script which will create osuscript.sh under /etc/profile.d which has all the export of environment variables but still facing same issue. However same is working if I run Java -jar osuserver.jar directly in the project path


Solution

  • Daemon script under /etc/init.d mostly runs as root user, some of them can be made auto start on system boot. Daemon are singleton, meanly one daemon script control one daemon service (start/stop/status/restart).

    Environment in ~/.profile is personal configuration, not system. You should put environment under /etc folder and add source command to daemon script to read the environments.