LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 10-22-2011, 11:15 AM   #1
Linuxstudent
LQ Newbie
 
Registered: Jul 2011
Posts: 19

Rep: Reputation: Disabled
Help needed with auto start scripts for java server program


Hi All,

I'm very new to Linux scripting so I'm hoping someone can help me out here. I want to start a java program on startup as user "user". I have 2 questions.

1. Does rc.local run as root? If so is there any way to su - user within the script or to tell rc.local to launch something as user?

2. What is wrong with my script below? It echos fine but won't start the program daemon. (it works fine when I type the command directly into bash)

#! /bin/bash

echo "/usr/bin/java -jar /home/user/mekwars/Server/MekWarsServer.jar & " >/home/user/Desktop/mekwars_startup_log

/usr/bin/java -jar /home/user/mekwars/Server/MekWarsServer.jar & >> /home/user/Desktop/mekwars_startup_log

Permissions are set as
-rwx------ 1 user user 187 Oct 22 17:04 mekwarsStart.sh


Thanks for any help on this, I know it's probably something stupid with the script.
 
Old 10-22-2011, 04:10 PM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by Linuxstudent View Post
1. Does rc.local run as root?
Yes. All SysV init files run as root.

Quote:
Originally Posted by Linuxstudent View Post
If so is there any way to su - user within the script or to tell rc.local to launch something as user?
Yes, using su or sudo or a custom launcher (typically a small binary written in C).

Quote:
Originally Posted by Linuxstudent View Post
2. What is wrong with my script below? It echos fine but won't start the program daemon. (it works fine when I type the command directly into bash)
I believe it does start the daemon, but the daemon is killed when the session exits.

I would recommend you used something like the following:
Code:
#!/bin/bash

# If run as root, instead run as username
[ "`id -u`" = "0" ] && exec su -c "'$0'" username

# If not run as username, fail with an error
if [ "`id -un`" != "username" ]; then
    echo "This script can only be run by username or root." >&2
    exit 1
fi

# Redirect all output to /home/username/Desktop/mekwars_startup_log
exec &> /home/username/Desktop/mekwars_startup_log

# Output date and the command
date
echo "Running '/usr/bin/java -jar /home/username/mekwars/Server/MekWarsServer.jar'"
echo ""

# Start the java process in a separate session, completely detached
( cd /home/username/mekwars/Server
  setsid /usr/bin/java -jar MekWarsServer.jar & )
The main point you might see is how the command is run. The parentheses start a new subshell. The command is run in the background of that subshell. The setsid makes it run in a new session. All this causes the java process to be detached from the current process entirely.

The cd command is just a guess, just in case the MekWarsServer needs to be started in that directory. Note that the cd command here has no side effects: the script caller will not "see" the cd in any way.

This method means that if you for example run my script in a terminal, then close the terminal, the java process will continue running on the background. However, if you used your script, the java process would get killed (due to hangup signal) as the terminal is closed.
 
Old 10-23-2011, 08:30 AM   #3
Linuxstudent
LQ Newbie
 
Registered: Jul 2011
Posts: 19

Original Poster
Rep: Reputation: Disabled
Hi Nominal,

Thanks a million for all your help. The script worked a treat and it's shown me some nice little tricks I'll have to commit to memory.

I have another question or two if you wouldn't mind -

1.The subshell is started under my username and the process is daemonised from that subshell. Does this mean that if were to run the subshell command from within a session that the process would be stopped on logout?

This doesn't apply with rc.local but I'd like to understand more about the inner workings now that I'm running a server.

2. For the sake of security should I create a nologin user called java with no sudo access rights other than the java process and the directory in which it keeps its logs or would this be considered overkill?

I'd be grateful if you could also recommend a site or a book to help me transition from being concerned with "just making things work for now" to understanding what is happening underneath the hood with regard to security, scripting and general administration.

Thanks once again, I do really appreciate the time you've taken to answer my questions on this. I'm marking the thread as solved since my initial questions and problems were solved.

Last edited by Linuxstudent; 10-23-2011 at 08:31 AM.
 
Old 10-23-2011, 10:17 PM   #4
avinc
LQ Newbie
 
Registered: Apr 2011
Posts: 10

Rep: Reputation: 0
Very useful script
 
Old 10-24-2011, 12:04 AM   #5
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by Linuxstudent View Post
1.The subshell is started under my username and the process is daemonised from that subshell.
Correct.

Quote:
Originally Posted by Linuxstudent View Post
Does this mean that if were to run the subshell command from within a session that the process would be stopped on logout?
No. The subshell and setsid cause the process to be detached. This means it will no longer be related to your session or login at all, and thus will not be killed when you log out.

Quote:
Originally Posted by Linuxstudent View Post
2. For the sake of security should I create a nologin user called java with no sudo access rights other than the java process and the directory in which it keeps its logs or would this be considered overkill?
I recommend creating local (as in plain old UNIX accounts, not in LDAP or AD or any centralized user database) no-login user accounts for each service.

I do not understand what you mean by 'sudo access rights'? If you mean the user would only have access to the java process it runs, and to the log files, then yes: I consider that a good idea. It is certainly not overkill, in my opinion.

For starting Java services, I use a small C program I wrote, user-daemon. You can use it to start the Java service under a specific user account (without using su or sudo), from a normal service script. user-daemon will daemonize the desired service, monitor it for a few seconds to make sure it started up correctly, then exit; it is thus perfectly suited for a service script. It's licensed under GPLv2 or later (same as the Linux kernel) and is therefore open source; feel free to use it if you like. I haven't bothered packaging it, but if you want, I could whip it up into a .rpm or .deb file. To compile and install as it is now, just download user-daemon-1.03.tar.gz and run tar -xzf user-daemon-1.03.tar.gz && cd user-daemon-1.03 && sudo make install . It even has a manpage; see man user-daemon after installing for further info.

Quote:
Originally Posted by Linuxstudent View Post
I'd be grateful if you could also recommend a site or a book to help me transition from being concerned with "just making things work for now" to understanding what is happening underneath the hood with regard to security, scripting and general administration.
Unfortunately, it seems my approach to administration and security is much stricter than most, as I've yet to find book or a site that I could recommend without reservations.

A major change to my own viewpoint occurred when I learned of and moved to using qmail, djbdns and daemontools. Nowadays there are other alternatives to these, but I still find Bernsteins approach elegant.

In general, I've found that keeping systems as modular as possible, and shying away from overarching frameworks, is an excellent long-term strategy. This is nothing new; it is just a variant of the old UNIX philosophy.

Another major change to my approach in system administration occurred when I realized most administrator tasks could be done without su and sudo, by judicious use of local administrator groups, and group access controls. POSIX ACLs (extended access controls) make that even more powerful and versatile. Similarly, most services are better run as independent entities (one or more local users and groups); many access rights problems can be solved simply by arranging proper group memberships. Personally, I only delved into this when cooperating with many other administrators on the same servers; therefore I'm not surprised these features are unknown or at least underutilized by most Linux administrators.

As to scripting, there is really only one thing I'd like to insist on: Learn the full quoting and escaping rules as early and as completely as possible. Apply them always, even when not strictly required. I personally prefer Bash, but I also use POSIX shell (dash in many distributions) for startup and system scripts. I guess this is typical for Linux administrators. Bash and POSIX shell quoting rules are very similar, too. If you use Bash, I recommend you write your scripts so that they support all possible file names, even those with whitespace in them.

But, as I said, I hope others will point you some good resources.
 
  


Reply

Tags
bash scripting, java


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How to auto-start a program on storage device plug? 10110111 Linux - Software 6 05-08-2010 11:17 AM
auto start a program in XFree on Fedora? MCSquared Fedora 22 09-12-2007 05:55 PM
Auto start a program in GNOME 2.6 rusty0412 Linux - Newbie 1 05-23-2004 02:47 AM
Wondering if I should use JAVA or UNIX scripts to develop a program pedrosan Programming 4 05-22-2004 07:35 AM
auto start a program jrockey Linux - Newbie 1 05-26-2001 07:12 AM


All times are GMT -5. The time now is 09:20 PM.

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