Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Linux Forums > Linux - General
User Name
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.


  Search this Thread
Old 04-04-2005, 07:07 PM   #1
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):


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,

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

Rep: Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657
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
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.


Last edited by DTParanoia; 04-05-2005 at 10:41 AM.
Old 04-05-2005, 05:58 PM   #4
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



# Define the server start commands.

# retain the pid of the server in order to kill
# it if a stop|restart is needed.

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

# 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"

# reset status of this service

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


echo $! > $PID_FILE

rc_status -v


echo "Usage: $0 {start|stop|restart}"
exit 1

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


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 05:26 AM.

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