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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-04-2005, 07:07 PM
|
#1
|
|
LQ Newbie
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3
Rep:
|
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
|
|
|
|
04-05-2005, 01:19 AM
|
#2
|
|
Moderator
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
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.
|
|
|
|
04-05-2005, 10:37 AM
|
#3
|
|
LQ Newbie
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3
Original Poster
Rep:
|
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.
|
|
|
|
04-05-2005, 05:58 PM
|
#4
|
|
LQ Newbie
Registered: Oct 2004
Location: In my happy place
Distribution: mandrake
Posts: 3
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:45 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|