Solved

Opensuse 13.1 - create service file for script

Posted on 2014-10-14
12
381 Views
Last Modified: 2014-11-08
setup:
OPENSUSE 13.1
firewall is DISABLED

Hi,

  I have a working script below that I am trying to get to run at startup with sudo privileges.

location:  /opt/dcm4chee-2.18.0-mysql/bin/radarch4.sh

#!/bin/sh
#
# dcm4chee Control Script
#
# To use this script
# run it as root - it will switch to the specified user
# It loses all console output - use the log.
#
# Here is a little (and extremely primitive)
# startup/shutdown script for SuSE systems. It assumes
# that JBoss lives in /usr/local/dcm4chee, it's run by user
# 'pacs' and JDK binaries are in /usr/java/jdk/bin. All
# this can be changed in the script itself.
#
# Either amend this script for your requirements
# or just ensure that the following variables are set correctly
# before calling the script.
#
### BEGIN INIT INFO
# Provides:       dcm4chee
# Required-Start: $PACS_DB
# Required-Stop:
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the DCM4CHEE DICOM Image Manager
### END INIT INFO

#define where jboss is - this is the directory containing directories log, bin, conf etc
JBOSS_HOME=${JBOSS_HOME:-"/opt/dcm4chee-2.18.0-mysql"}

#make java is on your path
JAVAPTH=${JAVAPTH:-"/usr/java/jdk1.7.0_67/bin"}

#define the classpath for the shutdown class
JBOSSCP=${JBOSSCP:-"$JBOSS_HOME/bin/shutdown.jar:$JBOSS_HOME/client/jnet.jar"}

#configuration to use, usually one of 'minimal', 'default', 'all'
JBOSS_CONF=${JBOSS_CONF:-"default"}

# JMX console credentials
JBOSS_ADMIN_USER=${JBOSS_ADMIN_USER:-"admin"}
JBOSS_ADMIN_PASS=${JBOSS_ADMIN_PASS:-"admin"}
ADMIN_USER_OPT="-u $JBOSS_ADMIN_USER"
ADMIN_PASS_OPT="-p $JBOSS_ADMIN_PASS"


#define the script to use to start jboss
JBOSSSH=${JBOSSSH:-"$JBOSS_HOME/bin/run.sh -c $JBOSS_CONF"}

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - misc error
# 2 - invalid or excess args
# 3 - unimplemented feature (e.g. reload)
# 4 - insufficient privilege
# 5 - program not installed
# 6 - program not configured
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

if [ -n "$JBOSS_CONSOLE" -a ! -d "$JBOSS_CONSOLE" ]; then
  # ensure the file exists
  touch $JBOSS_CONSOLE
fi

if [ -n "$JBOSS_CONSOLE" -a ! -f "$JBOSS_CONSOLE" ]; then
  echo "WARNING: location for saving console log invalid: $JBOSS_CONSOLE"
  echo "WARNING: ignoring it and using /dev/null"
  JBOSS_CONSOLE="/dev/null"
fi

#define what will be done with the console log
JBOSS_CONSOLE=${JBOSS_CONSOLE:-"/var/log/jboss/jboss.log"}

#define the user under which jboss will run, or use RUNASIS to run as the current user
JBOSSUS=${JBOSSUS:-"root"}

CMD_START="cd $JBOSS_HOME/bin; $JBOSSSH"
CMD_STOP="java -classpath $JBOSSCP org.jboss.Shutdown --shutdown $ADMIN_USER_OPT $ADMIN_PASS_OPT"

if [ "$JBOSSUS" = "RUNASIS" ]; then
  SUBIT=""
else
  SUBIT="su - $JBOSSUS -c "
fi

if [ -z "`echo $PATH | grep $JAVAPTH`" ]; then
  export PATH=$PATH:$JAVAPTH
fi

if [ ! -d "$JBOSS_HOME" ]; then
  echo JBOSS_HOME does not exist as a valid directory : $JBOSS_HOME
  exit 1
fi

case "$1" in
start)
    echo -n "Starting JBoss application server: "
    cd $JBOSS_HOME/bin
    if [ -z "$SUBIT" ]; then
        eval $CMD_START >${JBOSS_CONSOLE} 2>&1 &
    else
        $SUBIT "$CMD_START >${JBOSS_CONSOLE} 2>&1 &"
    fi

    # Remember status and be verbose
    rc_status -v
    ;;
stop)
    echo -n "Shutting down JBoss application server: "
    if [ -z "$SUBIT" ]; then
        $CMD_STOP
    else
        $SUBIT "$CMD_STOP"
    fi

    # Remember status and be verbose
    rc_status -v
    ;;
restart)
    $0 stop
    $0 start

    # Remember status and be quiet
    rc_status
    ;;
*)
    echo "usage: $0 (start|stop|restart|help)"
esac

Open in new window


I have also created a service file here --> '/usr/lib/systemd/system/radarch4.service'

contents of that file:

[Unit]
Description=Dcm4chee Archive service on RADARCH4

[Service]
Type=oneshot
ExecStart=/opt/dcm4chee-2.18.0-mysql/bin/radarch4.sh start
ExecStop=/opt/dcm4chee-2.18.0-mysql/bin/radarch4.sh stop
ExecRestart=/opt/dcm4chee-2.18.0-mysql/bin/radarch4.sh restart
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

Open in new window


The service DOES runs at startup, but does not run as 'root' at startup.  What the 'radarch4.sh' script does is it runs the '$JBOSS_HOME/bin/run.sh' script as well which needs to run as 'root' because it needs to listen on TCP 104, Which is a port below 1024 and needs sudo for it to be open.

The system autologs in as the user 'pacs' and after the service starts, I can kill it and then run it as 'sudo systemctl start radarch4.service' and it starts with the open port of 104.

How can I get around this?
0
Comment
Question by:doc_jay
  • 6
  • 6
12 Comments
 
LVL 62

Expert Comment

by:gheist
ID: 40380984
You dont need to do anything
put script in /etc/init.d
chmod+x
chkconfig -add

and systemd will run your SystemV startup script just fine.
0
 

Author Comment

by:doc_jay
ID: 40381280
do i even need the 'radarch4.service' file?  How will I start/stop my script when needed?

thanks
0
 
LVL 62

Expert Comment

by:gheist
ID: 40381456
no you do not need .service file ;)


sysv services will work with upstart in ubuntu just like with systemd you see here....
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:doc_jay
ID: 40382015
okay, I'll give it a shot in Opensuse.  How can I 'deregister' my 'radarch4' service after I have already made it aware of it?  Is just removing the radarch4.service file enough for it to not be aware of it any longer?

thanks
0
 
LVL 62

Expert Comment

by:gheist
ID: 40382507
I think it is systemctl daemon-reload to reload all disk configuration
0
 

Author Comment

by:doc_jay
ID: 40383167
this didn't work out as I had hoped.  The script did run, just as before with the .service file, but still, port 104 was NOT open.
0
 
LVL 62

Expert Comment

by:gheist
ID: 40383231
Does the script /opt/dcm4chee-2.18.0-mysql/bin/radarch4.sh run manually?
And with bash -x ?
0
 

Author Comment

by:doc_jay
ID: 40383346
yes, it does run manually, but it needs sudo.  Sorry, when you say 'bash -x' do you mean have 'execute' priv, then yes.  It does have that privilege.  Its owned by 'root' for the user/group.
0
 
LVL 62

Expert Comment

by:gheist
ID: 40383427
bash -x traces all lines.

Does it start in single mode?
Maybe it requires mysql or network?
0
 

Author Comment

by:doc_jay
ID: 40385253
This does require mysql and network, as it needs to talk to mysql over 3306 and open up port 104 for listening.
0
 
LVL 62

Accepted Solution

by:
gheist earned 500 total points
ID: 40385307
So it requires network

Add to
Required-Start: network
Required-Stop: network

Without dollar sign *see inside /etc/init.d/network for answer why
0
 

Author Comment

by:doc_jay
ID: 40430352
I ended up moving to centos 7 and using authbind.  Works as expected.    I appreciate all of the comments/suggestions.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question