LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 04-04-2005, 07:07 PM   #1
DTParanoia
LQ Newbie
 
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3

Rep: Reputation: 0
shell script? : usage of $! with su -c option


I have a script that uses $! and su as follows (runs as root and java is in the path):

JAVA_USER_HOME=/home/javauser
SECURITY_SERVER_LOG=$JAVA_USER_HOME/logfile
PID_FILE=$JAVA_USER_HOME/server.pid

su javauser -c "java -jar $JAVA_USER_HOME/Server.jar > $SECURITY_SERVER_LOG 2>&1 &"
echo $! > $PID_FILE

Initialy I expected $! to contain the pid of the command passed to su, however, when the value is blank its clearly not what is happening.

So the question is, what can I do to still make sure that the java virtual machine runs under javauser as opposed to root, and still obtain the pid of the VM?


Thanks in advance,

DT
 
Old 04-05-2005, 01:19 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
I think that the problem is that the command doesn't run. Unless you don't have permission to write to a file in the /home/javauser/ directory. You might want to try it without the su -c part to see if it starts. Also saving the value of $? in a variable immediately after the the java command might supply some info. What does the $JAVA_USER_HOME/logfile indicate?

Also check that the javauser has a default shell entry in /etc/passwd.
 
Old 04-05-2005, 10:37 AM   #3
DTParanoia
LQ Newbie
 
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3

Original Poster
Rep: Reputation: 0
I'll make sure and check the exit status before I cause any more trouble , but I don't expect it to indicate failure as the command does in fact run, and the log reflects normal operation of the program I expect to run. The only problem is that when I echo the PID to the pid file its empty. I modified the script so that it no longer uses su and the PID reappears, so it has to be something with the combination of $! and su. You'll have to forgive me for not posting the real script up to this point as I don't have it until I get to work, but when I do, I'll post the whole thing and hopefully something shakes out.


DT

Last edited by DTParanoia; 04-05-2005 at 10:41 AM.
 
Old 04-05-2005, 05:58 PM   #4
DTParanoia
LQ Newbie
 
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3

Original Poster
Rep: Reputation: 0
OK here is a trimmed down (trimmed to only the relevant sections to avoid post bloat) script that may or may not illustrate the isuue I am having. This is an init script to be placed into /etc/init.d and linked to by the various run levels, so it runs as root. When java is started I would very much like it to be run under a different user with lower privledge. I also need to retain the PID of the JVM, and this is where I am having trouble. I am doing it as follows, but I obviously need an alternative because its not working. I think its because there is a subshell involved and $! has no meaning to the shell in which I echo the value Fell free to review and correct me in any mistakes you see.

Just to summarize, the question is:


How do I run java as another user (other than root) and still retain the PID and write it (as root) to /var/run/ ?


# Set default values
#
JAVA_HOME=/opt/j2sdk1.4.2_06
JAVA_BIN=$JAVA_HOME/bin

JAVA_USER=javauser
JAVA_USER_HOME=/home/$JAVA_USER
SECURITY_SERVER_LOG=$JAVA_USER_HOME/console_log

SU_PREFIX="su $JAVA_USER -c "

# Define the server start commands.
#
SERVER_START="$JAVA_BIN/java -jar $JAVA_USER_HOME/Server.jar"


# retain the pid of the server in order to kill
# it if a stop|restart is needed.
PID_FILE=/var/run/$0.pid

# if the log file does not exist then create it.
#
if [ -n "$SECURITY_SERVER_LOG" -a ! -e "$SECURITY_SERVER_LOG" ]; then
$SU_PREFIX "touch $SECURITY_SERVER_LOG"
fi

# if the existing log file does not exist or is not a regular file
# then send console output to /dev/null
#
if [ -n "$SECURITY_SERVER_LOG" -a ! -f "$SECURITY_SERVER_LOG" ]; then
echo "WARNING: ${SECURITY_SERVER_LOG} is not a valid log file."
echo "WARNING: The log will be sent to /dev/null"
$SECURITY_SERVER_LOG=/dev/null
fi

# reset status of this service
rc_reset

case "$1" in
start)
echo -n "Starting Service "

cd $SECURITY_SERVER_HOME
$SU_PREFIX "$SERVER_START > $SECURITY_SERVER_LOG 2>&1 &"

echo $! > $PID_FILE

rc_status -v
;;
stop)

;;
restart)

rc_status
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
;;
esac
rc_exit

Last edited by DTParanoia; 04-05-2005 at 06:47 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script inside shell script treotan Linux - General 4 02-19-2009 06:34 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 02:14 AM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 05:32 PM
How do I add a Shell option to grub DiSGuiZ Linux - Newbie 4 04-15-2005 03:28 PM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


All times are GMT -5. The time now is 10:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration