0

When executing this script

#!/bin/sh
CLIENT=/home/pi/epaper2/HttpClient.jar
USER=pi
OUT=/home/pi/client.log
case "$1" in
start)
    echo "starting node: $CLIENT $OUT"
    java -jar -Djava.awt.headless=true $CLIENT > $OUT 2>$OUT &
    ;;
stop)
    killall HttpClient.jar
    ;;
*)  echo "usage: $0 (start|stop)"
esac
exit 0

on startup the variable System.getProperty("user.dir") inside code will give me / as path, but not the path where the .jar is located. But excectuting this script in the home directory of $USER will set System.getProperty("user.dir") right.
I have also tried to start the jar as the pi user with sudo -H -u $USER java -jar -Djava.awt.headless=true $CLIENT > $OUT 2>$OUT & . In this case the script won´t start at all.

My Question is now why is System.getProperty("user.dir") set to / and how i can fix it.

  • it is likely set for the user in their .bashrc or .profile – rob May 29 '14 at 10:09
1

The most probable reason the user.dir is set to "/" is because the script is running as root. That would be normal if the script is being started at boot time. Setting the variable "USER=pi" does not change the user id that is running the script, it simply (re)sets a variable named "USER" to the value "pi".

You can check this by looking at the output of the ps command when running after startup and then again after you started it in the $USER directory. Most likely you were logged in as user pi when you ran it the second time. Try

ps -ef | grep java

If you wish the java process to run as user "pi", there are several ways to accomplish that. Look up the "su" command and try something like "su --login pi --comand /path/to/script" as user root. If that works like you wanted, then your next task is to figure out how the script is getting started at boot time. Check to see if there is a file or link under /etc/init.d that is calling your script. If a sym link, replace it with a script containing the su command.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.